package com.wall.wallpaper.hd.ui_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.wallpaper.hd.R;
import com.wall.wallpaper.hd.WallApp;
import com.wall.wallpaper.hd.adapter.AdapterFavorite;
import com.wall.wallpaper.hd.databinding.FragLikeBinding;
import com.wall.wallpaper.hd.javabean.Data;
import com.wall.wallpaper.hd.listener.ItemClickListener;
import com.wall.wallpaper.hd.listener.LikeListListener;
import com.wall.wallpaper.hd.tools.CommonUtils;
import com.wall.wallpaper.hd.tools.DbManager;
import com.wall.wallpaper.hd.tools.ItemSpace;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragmentLike extends Fragment implements ItemClickListener {

    private FragLikeBinding vb;
    private DataSubscription dataSubscription;

    public FragmentLike() {

    }

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

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


    }

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

    private void initList() {
        AdapterFavorite adapterFavorite = new AdapterFavorite(requireContext());
        dataSubscription = DbManager.setLikeDataListener(new LikeListListener() {
            @Override
            public void onLikeList(List<Data> dataList) {
                Log.d(WallApp.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);
                    adapterFavorite.setDataInfo(dataList);
                }
            }
        });
        ItemSpace itemSpace = new ItemSpace(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(itemSpace);
        adapterFavorite.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(adapterFavorite);

    }

    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(requireContext(), ActivitySetWallpaper.class);
        intent.putExtra(CommonUtils.key_info, info);
        startActivity(intent);
    }

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