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.recyclerview.widget.RecyclerView;

import com.gh.common.util.CollectionUtils;
import com.gh.common.util.DataCollectionUtils;
import com.gh.gamecenter.common.utils.DialogHelper;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
import com.gh.gamecenter.NewsDetailActivity;
import com.gh.gamecenter.R;
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.entity.NewsEntity;
import com.gh.gamecenter.eventbus.EBCollectionChanged;
import com.gh.gamecenter.history.IBatchDelete;
import com.gh.gamecenter.history.ManageOption;

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

/**
 * Created by khy on 18/07/17.
 */

public class ArticleFragment extends ListFragment<NewsEntity, ArticleViewModel> implements IBatchDelete {

    private String mType;
    private ArticleAdapter mAdapter;
    private ArticleViewModel mViewModel;

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

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

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

    @Override
    protected ArticleViewModel provideListViewModel() {
        if (mViewModel == null) {
            mViewModel = super.provideListViewModel();
        }
        mViewModel.type = mType;
        return mViewModel;
    }

    // 收藏事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBCollectionChanged changed) {
        if (changed.getCollectionType().equals(CollectionUtils.CollectionType.article)) { // 取消关注/新增关注
            mListViewModel.load(LoadType.REFRESH);
        }
    }

    @Override
    public void onListClick(View view, int position, Object data) {
        NewsEntity newsEntity = (NewsEntity) data;

        if (COLLECTION.equals(mType)) {
            if (!newsEntity.getActive()) {
                showDeleteDialog(newsEntity.getId());
                return;
            }

            DataCollectionUtils.uploadClick(getContext(), "列表", "收藏-文章", newsEntity.getTitle());
            NewsDetailActivity.startNewsDetailByCollection(getContext(), newsEntity, "(收藏:文章)");
        } else {
            DataCollectionUtils.uploadClick(getContext(), "列表", "浏览记录-文章", newsEntity.getTitle());
            NewsDetailActivity.startNewsDetailByCollection(getContext(), newsEntity, "(浏览记录:文章)");
        }

        mAdapter.statNewsViews(newsEntity, position);
    }

    private void showDeleteDialog(String articleId) {
        DialogHelper.showCenterDialog(requireContext(), "提示"
                , "内容已被删除，是否取消收藏？"
                , "取消收藏", "暂不"
                , () -> CollectionUtils.INSTANCE.deleteCollection(requireContext(), articleId
                        , CollectionUtils.CollectionType.article, new CollectionUtils.OnCollectionListener() {
                            @Override
                            public void onSuccess() {
                                toast(R.string.collection_cancel);
                                mListViewModel.load(LoadType.REFRESH);
                            }

                            @Override
                            public void onError() {
                                toast(R.string.collection_cancel_failure);
                            }
                        }), () -> {
                });
    }

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

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        Drawable insetDivider = ContextCompat.getDrawable(requireContext(), 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(), R.color.background_white));
    }
}
