package com.free.life.wallpaper.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.free.life.wallpaper.MyWallpaper;
import com.free.life.wallpaper.R;
import com.free.life.wallpaper.databinding.FragmentLikeBinding;
import com.free.life.wallpaper.recycler.AdapterFavorite;
import com.free.life.wallpaper.ox.DbManager;
import com.free.life.wallpaper.bean.Data;
import com.free.life.wallpaper.mylistener.ItemClickListener;
import com.free.life.wallpaper.mylistener.OnLikeListener;
import com.free.life.wallpaper.utils.CommonUtils;
import com.free.life.wallpaper.utils.RecyclerSpace;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragmentLike extends Fragment implements ItemClickListener {

    private FragmentLikeBinding vb;
    private DataSubscription dataSubscription;

    public FragmentLike() {

    }

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

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


    }

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

    private void initList() {
        AdapterFavorite adapterFavorite = new AdapterFavorite(requireContext());
        dataSubscription = DbManager.setLikeDataListener(new OnLikeListener() {
            @Override
            public void onLikeList(List<Data> dataList) {
                Log.d(MyWallpaper.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);
                    adapterFavorite.setDataInfo(dataList);
                }
            }
        });
        RecyclerSpace recyclerSpace = new RecyclerSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(recyclerSpace);
        adapterFavorite.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(adapterFavorite);

    }

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

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