package com.gh.gamecenter.qa.answer.draft;

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

import androidx.annotation.Nullable;

import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.core.utils.UrlFilterUtils;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
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.common.baselist.NormalListViewModel;
import com.gh.gamecenter.common.eventbus.EBReuse;
import com.gh.gamecenter.login.user.UserManager;
import com.gh.gamecenter.qa.answer.edit.AnswerEditActivity;
import com.gh.gamecenter.feature.entity.AnswerEntity;
import com.gh.gamecenter.retrofit.RetrofitManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import io.reactivex.Observable;

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

public class AnswerDraftFragment extends ListFragment<AnswerEntity, NormalListViewModel> {

    private AnswerDraftAdapter mAdapter;

    @Nullable
    private String mAnswerId;

    public static AnswerDraftFragment getInstance(String entrance) {
        AnswerDraftFragment fragment = new AnswerDraftFragment();
        Bundle args = new Bundle();
        args.putString(EntranceConsts.KEY_ENTRANCE, entrance);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mAnswerId = arguments.getString(EntranceConsts.KEY_ANSWER_ID);
        }

        super.onCreate(savedInstanceState);
        if (getActivity() instanceof AnswerDraftActivity) {
            setNavigationTitle("回答草稿");
        }
    }

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


    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        Drawable insetDivider = ContextCompat.getDrawable(requireContext(), R.drawable.divider_item_line_space_16);
        CustomDividerItemDecoration itemDecoration = new CustomDividerItemDecoration(requireContext(), false, false, true, false);
        if (insetDivider != null) {
            itemDecoration.setDrawable(insetDivider);
        }
        return itemDecoration;
    }

    @Override
    public Observable<List<AnswerEntity>> provideDataObservable(int page) {
        return RetrofitManager.getInstance().getApi().getAnswerDrafts(
                UserManager.getInstance().getUserId(),
                UrlFilterUtils.getFilterQuery("answer_id", mAnswerId),
                page);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EBReuse reuse) {
        if (AnswerEditActivity.ANSWER_DRAFT_CHANGE_TAG.equals(reuse.getType())) {
            mListRv.scrollToPosition(0);
            mBaseHandler.postDelayed(() -> mListViewModel.load(LoadType.REFRESH), 100);
        }
    }
}
