package com.wall.turing.wallpapers.turingfragments;

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.turing.wallpapers.WallpaperApplication;
import com.wall.turing.wallpapers.R;
import com.wall.turing.wallpapers.accctivities.TuringWallpaperActivity;
import com.wall.turing.wallpapers.databinding.LikePageBinding;
import com.wall.turing.wallpapers.uutils.AppUtils;
import com.wall.turing.wallpapers.uutils.DatabaseManager;
import com.wall.turing.wallpapers.uutils.AdapterHelper;
import com.wall.turing.wallpapers.connect.TuringItemClick;
import com.wall.turing.wallpapers.connect.TuringLikeList;
import com.wall.turing.wallpapers.myselfbean.Wallpaper;
import com.wall.turing.wallpapers.adapteres.LikeAdapter;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class LikePage extends Fragment implements TuringItemClick {

    private LikePageBinding vb;
    private DataSubscription dataSubscription;

    public LikePage() {

    }

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

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


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        vb = LikePageBinding.inflate(getLayoutInflater());
        
        // 添加状态栏高度适配
        vb.topTitleBar.post(() -> {
            com.wall.turing.wallpapers.uutils.AppUtils.addStatusBarPadding(vb.topTitleBar, requireContext());
        });
        
        vb.tvTitle.setText(getString(R.string.no_like_data));
        initList();
        return vb.getRoot();
    }

    private void initList() {
        LikeAdapter itemLike = new LikeAdapter(requireContext());
        dataSubscription = DatabaseManager.setLikeDataListener(new TuringLikeList() {
            @Override
            public void onLikeList(List<Wallpaper> dataList) {
                Log.d(WallpaperApplication.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);
                }
            }
        });
        AdapterHelper adapterHelper = new AdapterHelper(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(adapterHelper);
        itemLike.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(itemLike);

    }

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

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