package com.gh.gamecenter.qa.subject;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
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.constant.EntranceConsts;
import com.gh.gamecenter.common.view.VerticalItemDecoration;
import com.gh.gamecenter.core.utils.DisplayUtils;
import com.gh.gamecenter.core.utils.TimeElapsedHelper;
import com.gh.gamecenter.common.mvvm.Status;
import com.gh.gamecenter.feature.entity.AnswerEntity;

/**
 * Created by khy on 1/04/18.
 */

public class AskSubjectFragment extends ListFragment<AnswerEntity, AskSubjectViewModel> {

    TextView mNoDataTv;

    private AskSubjectAdapter mAdapter;

    private TimeElapsedHelper mElapsedHelper;

    private int height;

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new VerticalItemDecoration(getContext(), 8, false);
    }

    @Override
    protected boolean isAutomaticLoad() {
        return false;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setNavigationTitle("问答专题");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mListViewModel.initData(arguments.getParcelable(EntranceConsts.KEY_COMMUNITY_DATA)
                    , arguments.getString(EntranceConsts.KEY_COLUMN_ID));
        }

        mNoDataTv = mCachedView.findViewById(R.id.reuseNoneDataTv);
        if (mNoDataTv != null) mNoDataTv.setText(com.gh.gamecenter.common.R.string.content_delete_hint);
        height = (int) (getResources().getDisplayMetrics().widthPixels * 0.44) + DisplayUtils.dip2px(getContext(), 20);
        mListRv.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = mLayoutManager.findViewByPosition(0);
                if (view == null) return;
                int top = view.getTop();
                if (-top > height && mListViewModel.getCacheSubjectData() != null) {
                    setNavigationTitle(mListViewModel.getCacheSubjectData().getTitle());
                } else {
                    setNavigationTitle("问答专题");
                }
            }
        });

        mListViewModel.getHeadLiveData().observe(this, it -> {
            if (it != null && it.status == Status.SUCCESS) {
                mAdapter.setSubjectEntity(it.data);
            }
        });

        mElapsedHelper = new TimeElapsedHelper(this);
    }

    @Override
    protected ListAdapter provideListAdapter() {
        return mAdapter == null ? mAdapter = new AskSubjectAdapter(getContext(), this, mEntrance + "+(问答专题)") : mAdapter;
    }

    @Override
    protected void autoLoadMore() {
        mListViewModel.load(LoadType.NORMAL);
    }

    @Override
    public void onListClick(View view, int position, Object data) {
        switch (view.getId()) {
            case com.gh.gamecenter.common.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;
    }
}
