package com.wall.various.wallpaper.ui;

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.various.wallpaper.MyApp;
import com.wall.various.wallpaper.R;
import com.wall.various.wallpaper.databinding.FragUiLikeBinding;
import com.wall.various.wallpaper.helper.Common;
import com.wall.various.wallpaper.helper.Dbhelper;
import com.wall.various.wallpaper.helper.ItemHelper;
import com.wall.various.wallpaper.inter.ItemClickInterface;
import com.wall.various.wallpaper.inter.LikeListInterface;
import com.wall.various.wallpaper.myjson.Data;
import com.wall.various.wallpaper.recycler.RecyclerLike;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class FragUiLike extends Fragment implements ItemClickInterface {

    private FragUiLikeBinding vb;
    private DataSubscription dataSubscription;

    public FragUiLike() {

    }

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

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


    }

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

    private void initList() {
        RecyclerLike recyclerLike = new RecyclerLike(requireContext());
        dataSubscription = Dbhelper.setLikeDataListener(new LikeListInterface() {
            @Override
            public void onLikeList(List<Data> 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);
                    recyclerLike.setDataInfo(dataList);
                }
            }
        });
        ItemHelper itemHelper = new ItemHelper(10, 10, 15);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 2);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(itemHelper);
        recyclerLike.setInfoWallpaperListener(this);
        vb.listRecycler.setAdapter(recyclerLike);

    }

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

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