package com.gh.gamecenter.qa.myqa;

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.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.constant.EntranceConsts;
import com.gh.gamecenter.common.view.CustomDividerItemDecoration;
import com.gh.gamecenter.login.user.UserManager;
import com.gh.gamecenter.qa.entity.AnswerEntity;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.halo.assistant.HaloApp;
import com.lightgame.utils.Utils;

import java.util.List;

import io.reactivex.Observable;

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

public class MyAnswerFragment extends ListFragment<AnswerEntity, NormalListViewModel> {

    private MyAnswerAdapter mAdapter;

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

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

    @Override
    public Observable<List<AnswerEntity>> provideDataObservable(int page) {
        return RetrofitManager.getInstance().getApi().getMyAnswers(UserManager.getInstance().getUserId(), page,
                HaloApp.getInstance().getChannel(), Utils.getTime(getContext()));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mCachedView.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_white));
    }

    @Override
    public void onListClick(View view, int position, Object data) {
        switch (view.getId()) {
            case R.id.footerview_item:
                if (mAdapter.isNetworkError()) {
                    mListViewModel.load(LoadType.RETRY);
                }
                break;
        }
    }

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

    @Nullable
    @Override
    protected RecyclerView.Adapter provideSyncAdapter() {
        return 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);
        itemDecoration.setDrawable(insetDivider);
        return itemDecoration;
    }

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        if (mListRv != null && mListRv.getItemDecorationCount() > 0) {
            mListRv.removeItemDecorationAt(0);
            mListRv.addItemDecoration(getItemDecoration());
        }
    }
}
