package com.phone.wall.paper.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.phone.wall.paper.R;
import com.phone.wall.paper.WallpaperApp;
import com.phone.wall.paper.adapter.FavoriteAda;
import com.phone.wall.paper.data.DataBean;
import com.phone.wall.paper.databinding.FragFavoriteBinding;
import com.phone.wall.paper.listener.OnClick;
import com.phone.wall.paper.listener.OnLikeList;
import com.phone.wall.paper.tools.ListSpace;
import com.phone.wall.paper.tools.RoomManager;
import com.phone.wall.paper.tools.Utils;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragFavorite extends Fragment implements OnClick {

    private FragFavoriteBinding vb;
    private DataSubscription dataSubscription;

    public FragFavorite() {

    }

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

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


    }

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

    private void initList() {
        FavoriteAda favoriteAda = new FavoriteAda(requireContext());
        dataSubscription = RoomManager.setLikeDataListener(new OnLikeList() {
            @Override
            public void onLikeList(List<DataBean> dataBeanList) {
                Log.d(WallpaperApp.TAG, "------size = " + dataBeanList.size());
                if(dataBeanList.size()==0){
                    vb.layoutNoData.setVisibility(View.VISIBLE);
                    vb.listRecycler.setVisibility(View.GONE);
                }else {
                    vb.layoutNoData.setVisibility(View.GONE);
                    vb.listRecycler.setVisibility(View.VISIBLE);
                    favoriteAda.setDataInfo(dataBeanList);
                }
            }
        });
        ListSpace listSpace = new ListSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(listSpace);
        favoriteAda.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(favoriteAda);

    }

    @Override
    public void onItemClickAction(DataBean info) {
        Intent intent = new Intent(requireContext(), SetWall.class);
        intent.putExtra(Utils.key_info, info);
        startActivity(intent);
    }

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