package com.gh.gamecenter.collection;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import com.gh.common.util.CollectionUtils;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
import com.gh.gamecenter.common.baselist.ListAdapter;
import com.gh.gamecenter.common.baselist.ListFragment;
import com.gh.gamecenter.common.baselist.LoadType;
import com.gh.gamecenter.eventbus.EBCollectionChanged;
import com.gh.gamecenter.history.IBatchDelete;
import com.gh.gamecenter.history.ManageOption;
import com.gh.gamecenter.feature.entity.AnswerEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

/**
 * Created by khy on 22/12/17.
 */

public class AnswerFragment extends ListFragment<AnswerEntity, AnswerViewModel> implements IBatchDelete {

    private AnswerAdapter mAdapter;
    private String mType;

    public static String COLLECTION = "collection";
    public static String COLLECTION_ANSWER = "collection_answer";
    public static String HISTORY = "history";

    @Override
    protected ListAdapter provideListAdapter() {
        return mAdapter == null ? mAdapter = new AnswerAdapter(getContext(), mListViewModel, this, mEntrance) : mAdapter;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        mType = getArguments().getString(EntranceConsts.KEY_TYPE, COLLECTION);
        super.onCreate(savedInstanceState);
        resetBackgroundColor();
    }

    @Override
    protected AnswerViewModel provideListViewModel() {
        AnswerViewModel viewModel = ViewModelProviders.of(this).get(AnswerViewModel.class);
        viewModel.setType(mType);
        return viewModel;
    }

    @Override
    public void onListClick(View view, int position, Object data) {
        if (view.getId() == com.gh.gamecenter.common.R.id.footerview_item) {
            if (mAdapter.isNetworkError()) {
                mListViewModel.load(LoadType.RETRY);
            }
        }
    }

    // 收藏事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBCollectionChanged changed) {
        if (changed.getCollectionType().equals(CollectionUtils.CollectionType.ANSWER)) {
            mListViewModel.load(LoadType.REFRESH);
        }
    }

    @Override
    protected boolean addSyncPageObserver() {
        return true;
    }

    @Nullable
    @Override
    protected RecyclerView.Adapter provideSyncAdapter() {
        return mAdapter;
    }

    @Override
    public void changeOption(@NotNull ManageOption option) {
        mAdapter.changeOption(option);
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        Drawable insetDivider = ContextCompat.getDrawable(requireContext(), com.gh.gamecenter.common.R.drawable.divider_item_line_space_16);
        mItemDecoration = new CustomDividerItemDecoration(requireContext(), false, false, true, false);
        ((CustomDividerItemDecoration) mItemDecoration).setDrawable(insetDivider);
        return mItemDecoration;
    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        mCachedView.post(this::resetBackgroundColor);
    }

    private void resetBackgroundColor() {
        mCachedView.setBackgroundColor(ContextCompat.getColor(requireContext(), com.gh.gamecenter.common.R.color.ui_surface));
    }
}
