package com.wall.photography.picture.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.picture.MyApp;
import com.wall.photography.picture.R;
import com.wall.photography.picture.Activity.Wallpaper;
import com.wall.photography.picture.databinding.FragmentLikeBinding;
import com.wall.photography.picture.tool.CommonUtils;
import com.wall.photography.picture.tool.ObjectBoxManager;
import com.wall.photography.picture.tool.GridItemDecoration;
import com.wall.photography.picture.Connect.ItemClick;
import com.wall.photography.picture.Connect.LikeList;
import com.wall.photography.picture.Data.Data;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class Like extends Fragment implements ItemClick {

    private FragmentLikeBinding vb;
    private DataSubscription dataSubscription;

    public Like() {

    }

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

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


    }

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

    private void initList() {
        com.wall.photography.picture.Project.Like itemLike = new com.wall.photography.picture.Project.Like(requireContext());
        dataSubscription = ObjectBoxManager.setLikeDataListener(new LikeList() {
            @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);
                    itemLike.setDataInfo(dataList);
                }
            }
        });
        GridItemDecoration GridItemDecoration = new GridItemDecoration(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(GridItemDecoration);
        itemLike.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(itemLike);

    }

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

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