package com.hd.wallpaper.free.walls.ui;

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 androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.hd.wallpaper.free.walls.MyWallpaper;
import com.hd.wallpaper.free.walls.adapter.AdapterPreView;
import com.hd.wallpaper.free.walls.databinding.FragmentChildBinding;
import com.hd.wallpaper.free.walls.json.Data;
import com.hd.wallpaper.free.walls.mylistener.ItemClickListener;
import com.hd.wallpaper.free.walls.utils.CommonUtils;
import com.hd.wallpaper.free.walls.utils.ItemSpace;

import java.util.List;


public class FragmentChild extends Fragment implements ItemClickListener {


    private int mIndex;


    private List<Data> userList;

    private FragmentChildBinding vb;


    public FragmentChild() {

    }

    public static FragmentChild newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(CommonUtils.key_index, type);
        FragmentChild fragment = new FragmentChild();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mIndex = getArguments().getInt(CommonUtils.key_index);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragmentChildBinding.inflate(getLayoutInflater());

        initList();
        return vb.getRoot();
    }

    private void initList() {
        ItemSpace itemSpace = new ItemSpace(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.list.addItemDecoration(itemSpace);
        vb.list.setLayoutManager(gridLayoutManager);
        AdapterPreView adapterPreView = new AdapterPreView(requireContext(),false);
        userList = MyWallpaper.getData().get(mIndex).getDataList();
        Log.d(MyWallpaper.TAG, "----------setDataInfo  mIndex="+mIndex);
        adapterPreView.setDataInfo(userList);
        adapterPreView.setInfoWallpaperListener(this);
        vb.list.setAdapter(adapterPreView);


//        DbManager.addOnAddOrRemoveListener(new OnAddListener() {
//            @Override
//            public void onAdd(Data data) {
//                int index = userList.indexOf(data);
//                Log.d(MyWallpaper.TAG, "---mIndex=" + mIndex + "----onAdd  index=" + index);
//                adapterPreView.notifyItemChanged(index);
//
//            }
//
//            @Override
//            public void onRemove(Data data) {
//                int index = userList.indexOf(data);
//                adapterPreView.notifyItemChanged(index);
//                Log.d(MyWallpaper.TAG, "---mIndex=" + mIndex + "----onRemove  index=" + index);
//            }
//        });
    }


    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(requireContext(), ActivitySet.class);
        intent.putExtra(CommonUtils.key_info, info);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Glide.get(requireContext()).clearMemory();
        Log.d(MyWallpaper.TAG, "--onDestroy-mIndex=" + mIndex);
    }
}