package com.wall.dream.wallpapers.View;

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.dream.wallpapers.DreamWallpaper;
import com.wall.dream.wallpapers.LianXi.OnItemXianClick;
import com.wall.dream.wallpapers.R;
import com.wall.dream.wallpapers.acacctivity.WallpaperViewActivity;
import com.wall.dream.wallpapers.databinding.View1LikeBinding;
import com.wall.dream.wallpapers.GJ.ComUtilsGJ;
import com.wall.dream.wallpapers.GJ.ObectBoxManagerGJ;
import com.wall.dream.wallpapers.GJ.ItemHelperGJ;

import com.wall.dream.wallpapers.LianXi.OnLikeDeList;
import com.wall.dream.wallpapers.data.DataBase;
import com.wall.dream.wallpapers.ItemPag.ItemLikePag;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class ViewLike extends Fragment implements OnItemXianClick {

    private View1LikeBinding vb;
    private DataSubscription dataSubscription;

    public ViewLike() {

    }

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

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


    }

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

    private void initList() {
        // 动态计算item高度，与精选页面保持一致
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int padding = (int) (12 * getResources().getDisplayMetrics().density);
        int spacing = (int) (12 * getResources().getDisplayMetrics().density);
        int columnCount = 2;
        int itemWidth = (screenWidth - padding * 2 - spacing * (columnCount - 1)) / columnCount;
        int itemHeight = (int) (itemWidth * 1.15); // 使用1.15:1的宽高比，平衡高度和宽度
        
        ItemLikePag itemLikePag = new ItemLikePag(requireContext(), itemHeight);
        dataSubscription = ObectBoxManagerGJ.setLikeDataListener(new OnLikeDeList() {
            @Override
            public void onLikeList(List<DataBase> dataList) {
                Log.d(DreamWallpaper.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);
                    itemLikePag.setDataInfo(dataList);
                }
            }
        });
        ItemHelperGJ itemHelperGJ = new ItemHelperGJ(12, 12, 12);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), columnCount);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(itemHelperGJ);
        itemLikePag.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(itemLikePag);

    }

    @Override
    public void onItemClickAction(DataBase info) {
        Intent intent = new Intent(requireContext(), WallpaperViewActivity.class);
        intent.putExtra(ComUtilsGJ.key_info, info);
        startActivity(intent);
    }

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