package com.wall.photography.wallpapers.fragment;

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.wall.photography.wallpapers.MyApp;
import com.wall.photography.wallpapers.R;
import com.wall.photography.wallpapers.acctivity.WallpaperActivity;
import com.wall.photography.wallpapers.databinding.FragmentLikeBinding;
import com.wall.photography.wallpapers.tool.ComUtils;
import com.wall.photography.wallpapers.tool.ObectBoxManager;
import com.wall.photography.wallpapers.tool.ItemHelper;
import com.wall.photography.wallpapers.connector.OnItemClick;
import com.wall.photography.wallpapers.connector.OnLikeList;
import com.wall.photography.wallpapers.mybean.MyData;
import com.wall.photography.wallpapers.item.ItemLike;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragmentLike extends Fragment implements OnItemClick {

    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() {
        ItemLike itemLike = new ItemLike(requireContext());
        dataSubscription = ObectBoxManager.setLikeDataListener(new OnLikeList() {
            @Override
            public void onLikeList(List<MyData> 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);
                    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(MyData info) {
        Intent intent = new Intent(requireContext(), WallpaperActivity.class);
        intent.putExtra(ComUtils.key_info, info);
        startActivity(intent);
    }

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