package com.house.wallpapers.myfragment;

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.house.wallpapers.App;
import com.house.wallpapers.databinding.FragmentFavoriteBinding;
import com.house.wallpapers.myactivity.MyWallpaperActivity;
import com.house.wallpapers.adapter.OnItemclick;
import com.house.wallpapers.adapter.OnLikelist;
import com.house.wallpapers.mybean.Data;
import com.house.wallpapers.myitem.ItemFavorite;
import com.house.wallpapers.mytool.Comutils;
import com.house.wallpapers.mytool.Itemhelper;
import com.house.wallpapers.mytool.ObectManager;
import com.house.wallpapers.R;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragmentFavorite extends Fragment implements OnItemclick {

    private FragmentFavoriteBinding vb;
    private DataSubscription dataSubscription;

    public FragmentFavorite() {

    }

    public static FragmentFavorite newInstance() {
        FragmentFavorite fragment = new FragmentFavorite();
        return fragment;
    }

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


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = FragmentFavoriteBinding.inflate(getLayoutInflater());
        vb.tvTitle.setText(getString(R.string.no_like_data));
        initList();
        return vb.getRoot();
    }

    private void initList() {
        ItemFavorite itemLike = new ItemFavorite(requireContext());
        dataSubscription = ObectManager.setLikeDataListener(new OnLikelist() {
            @Override
            public void onLikeList(List<Data> dataList) {
                Log.d(App.TAG, "------size = " + dataList.size());
                if(dataList.size()==0){
                    vb.layoutNoData.setVisibility(View.VISIBLE);
                    vb.listRecycler.setVisibility(View.GONE);
                }else {
                    vb.layoutNoData.setVisibility(View.GONE);
                    vb.listRecycler.setVisibility(View.VISIBLE);
                    itemLike.setDataInfo(dataList);
                }
            }
        });
        Itemhelper itemHelper = new Itemhelper(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(itemHelper);
        itemLike.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(itemLike);

    }

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

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (dataSubscription != null) {
            dataSubscription.cancel();
        }
        Log.d(App.TAG,"------favoriteFragment-onDestroyView");
    }
}