package com.snap.wall.fragment;

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.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.snap.wall.App;
import com.snap.wall.databinding.FragmentPreferBinding;
import com.snap.wall.activity.PreviewActivity;
import com.snap.wall.adapter.OnItemclick;
import com.snap.wall.adapter.OnLikelist;
import com.snap.wall.bean.Information;
import com.snap.wall.myitem.ItemPrefer;
import com.snap.wall.mytool.Comutils;
import com.snap.wall.mytool.Itemhelper;
import com.snap.wall.mytool.ObectManager;
import com.snap.wall.R;

import java.util.List;
import java.util.WeakHashMap;

import io.objectbox.reactive.DataSubscription;

public class FragmentPrefer extends Fragment implements OnItemclick {

    private FragmentPreferBinding vb;
    private DataSubscription dataSubscription;
    private ItemPrefer itemLike;

    private static class LikeListListener implements OnLikelist {
        private final WeakHashMap<FragmentPrefer, Object> fragmentRef;

        public LikeListListener(FragmentPrefer fragment) {
            fragmentRef = new WeakHashMap<>();
            fragmentRef.put(fragment, null);
        }

        @Override
        public void onLikeList(List<Information> dataList) {
            FragmentPrefer fragment = fragmentRef.keySet().iterator().next();
            if (fragment == null || fragment.isDetached() || fragment.getView() == null) {
                return;
            }
            Log.d(App.TAG, "收藏列表数据量: " + (dataList == null ? 0 : dataList.size()));
            fragment.updateListUi(dataList);
        }
    }

    public FragmentPrefer() {
    }

    public static FragmentPrefer newInstance() {
        return new FragmentPrefer();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        vb = FragmentPreferBinding.inflate(inflater, container, false);
        initList();
        return vb.getRoot();
    }

    private void initList() {
        if (!isAdded() || getContext() == null) {
            return;
        }

        itemLike = new ItemPrefer(getContext());
        itemLike.setInfoWallpaperListener(this);
        int spanCount = 2;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), spanCount);
        vb.listRecycler.setLayoutManager(gridLayoutManager);
        vb.listRecycler.addItemDecoration(new Itemhelper(10, 10, 15));
        vb.listRecycler.setAdapter(itemLike);
        LikeListListener listener = new LikeListListener(this);
        dataSubscription = ObectManager.setLikeDataListener(listener);
    }

    private void updateListUi(List<Information> dataList) {
        if (dataList == null || dataList.isEmpty()) {
            vb.layoutNoData.setVisibility(View.VISIBLE);
            vb.listRecycler.setVisibility(View.GONE);
            vb.tvTitle.setText(getString(R.string.no_like_data));
        } else {
            vb.layoutNoData.setVisibility(View.GONE);
            vb.listRecycler.setVisibility(View.VISIBLE);
            vb.tvTitle.setText("");
            itemLike.setDataInfo(dataList);
        }
    }

    @Override
    public void onItemClickAction(Information info) {
        if (info == null || !isAdded() || getContext() == null) {
            return;
        }
        Intent intent = new Intent(getContext(), PreviewActivity.class);
        intent.putExtra(Comutils.key_info, info);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dataSubscription != null) {
            dataSubscription.cancel();
            dataSubscription = null;
        }
        Log.d(App.TAG, "FragmentFavorite onDestroy: 订阅已取消");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        vb = null;
    }
}