package com.example.wallisyn.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.example.wallisyn.MyApp;
import com.example.wallisyn.R;
import com.example.wallisyn.Activity.WallpaperActivity;
import com.example.wallisyn.databinding.FragmentWallpaperLikeBinding;
import com.example.wallisyn.tool.ComUtils;
import com.example.wallisyn.tool.ObectBoxManager;
import com.example.wallisyn.tool.ItemHelper;
import com.example.wallisyn.connector.OnItemClick;
import com.example.wallisyn.connector.OnLikeList;
import com.example.wallisyn.mybean.MyData;
import com.example.wallisyn.item.LikeWallpaperItem;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class WallpaperLikeFragment extends Fragment implements OnItemClick {

    private FragmentWallpaperLikeBinding vb;
    private DataSubscription dataSubscription;

    public WallpaperLikeFragment() {

    }

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

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


    }

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

    private void initList() {
        LikeWallpaperItem itemLike = new LikeWallpaperItem(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");
    }
}