package com.haven.wallpapers.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.haven.wallpapers.activity.DetailsActivity;
import com.haven.wallpapers.R;
import com.haven.wallpapers.Utils.MyValue;
import com.haven.wallpapers.activity.ViewActivity;
import com.haven.wallpapers.MyApp;
import com.haven.wallpapers.listener.InfoListener;
import com.haven.wallpapers.adapter.InfoAdapter;
import com.haven.wallpapers.jsonbean.Data;
import com.haven.wallpapers.jsonbean.Info;
import com.haven.wallpapers.topon.AdManager;
import com.haven.wallpapers.topon.onActionListener;

import java.util.Collections;
import java.util.List;

public class HomeFragment extends Fragment implements InfoListener {
    private RecyclerView recyclerView;

    private RelativeLayout relativeLayout;
    private ImageView imageView;

    private  List<Info> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Log.d("-------------","------------onCreateView");
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        recyclerView = view.findViewById(R.id.home_recyclerView);
        relativeLayout = view.findViewById(R.id.top_info);

        list = MyApp.getUserList();
        Collections.shuffle(list);
        Data data = list.get(0).getList().get(25);

        setRecyclerView();
        imageView = view.findViewById(R.id.top_image);
        Glide.with(requireContext())
                .load(data.getPreviewUrl400())
                .placeholder(MyApp.getPlaceImg())
                .into(imageView);
        relativeLayout.setOnClickListener(v -> {
         AdManager.showTopOn(requireActivity(), new onActionListener() {
             @Override
             public void onAction() {
                 Intent intent = new Intent(requireContext(), ViewActivity.class);
                 intent.putExtra(MyValue.key_data, data);
                 startActivity(intent);
             }
         });
        });

        return view;
    }

    private void setRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 1);
        InfoAdapter infoAdapter = new InfoAdapter(list, requireContext());
        infoAdapter.setInfoWallpaperListener(this);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(infoAdapter);
    }

    @Override
    public void onItemClickAction(String name) {

        AdManager.showTopOn(requireActivity(), new onActionListener() {
            @Override
            public void onAction() {
                Intent intent = new Intent(requireContext(), DetailsActivity.class);
                intent.putExtra(MyValue.key_info, name);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onHomeInfoClickAction(Data info) {
        AdManager.showTopOn(requireActivity(), new onActionListener() {
            @Override
            public void onAction() {
                Intent intent = new Intent(requireContext(), ViewActivity.class);
                intent.putExtra(MyValue.key_data, info);
                startActivity(intent);
            }
        });

    }
}