package com.wall.snap.wallpaper.fragmentsnap;

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.snap.wallpaper.MysnapApp;
import com.wall.snap.wallpaper.R;
import com.wall.snap.wallpaper.acctivitysnap.WallpapersnapActivity;
import com.wall.snap.wallpaper.databinding.FragmentLikesnapBinding;
import com.wall.snap.wallpaper.mysnapbean.MysnapData;
import com.wall.snap.wallpaper.toolsnap.ComUtilssnap;
import com.wall.snap.wallpaper.toolsnap.ObectBoxManagersnap;
import com.wall.snap.wallpaper.toolsnap.ItemHelpersnap;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragmentLikesnap extends Fragment implements com.wall.snap.wallpaper.connectorsnap.OnItemsnapClick {

    private FragmentLikesnapBinding vb;
    private DataSubscription dataSubscription;

    public FragmentLikesnap() {

    }

    public static com.wall.snap.wallpaper.fragmentsnap.FragmentLikesnap newInstance() {
        com.wall.snap.wallpaper.fragmentsnap.FragmentLikesnap fragment = new com.wall.snap.wallpaper.fragmentsnap.FragmentLikesnap();
        return fragment;
    }

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


    }

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

    private void initList() {
        com.wall.snap.wallpaper.itemsnap.ItemsnapLike itemtwoLike = new com.wall.snap.wallpaper.itemsnap.ItemsnapLike(requireContext());
        dataSubscription = ObectBoxManagersnap.setLikeDataListener(new com.wall.snap.wallpaper.connectorsnap.OnLikesnapList() {
            @Override
            public void onLikeList(List<MysnapData> dataList) {
                Log.d(MysnapApp.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);
                    itemtwoLike.setDataInfo(dataList);
                }
            }
        });
        ItemHelpersnap itemHelpertwo = new ItemHelpersnap(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(itemHelpertwo);
        itemtwoLike.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(itemtwoLike);

    }

    @Override
    public void onItemClickAction(MysnapData info) {
        Intent intent = new Intent(requireContext(), WallpapersnapActivity.class);
        intent.putExtra(ComUtilssnap.key_info, info);
        startActivity(intent);
    }

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