package com.photo.wallpaper.category.activity;

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.photo.wallpaper.category.MyApp;
import com.photo.wallpaper.category.R;
import com.photo.wallpaper.category.api.ApiItemClick;
import com.photo.wallpaper.category.api.ApiOnLike;
import com.photo.wallpaper.category.bean.Data;
import com.photo.wallpaper.category.bean.DataBaseHelper;
import com.photo.wallpaper.category.databinding.FragmentLayoutLikeBinding;
import com.photo.wallpaper.category.itemAdapter.AdFavorite;
import com.photo.wallpaper.category.utils.CommonHelper;
import com.photo.wallpaper.category.utils.ItemSpace;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragLike extends Fragment implements ApiItemClick {

    private FragmentLayoutLikeBinding vb;
    private DataSubscription dataSubscription;

    public FragLike() {

    }

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

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


    }

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

    private void initList() {
        AdFavorite adFavorite = new AdFavorite(requireContext());
        dataSubscription = DataBaseHelper.setLikeDataListener(new ApiOnLike() {
            @Override
            public void onLikeList(List<Data> dataList) {
                Log.d(MyApp.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);
                    adFavorite.setDataInfo(dataList);
                }
            }
        });
        ItemSpace itemSpace = new ItemSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(itemSpace);
        adFavorite.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(adFavorite);

    }

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

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