package com.gh.gamecenter.info;

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

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

import com.gh.common.util.DataCollectionUtils;
import com.gh.gamecenter.core.utils.StringUtils;
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.common.databinding.FragmentListBaseBinding;
import com.gh.gamecenter.entity.NewsEntity;

/**
 * Created by khy on 2016/6/29.
 * 资讯-原创界面
 */
public class OriginalFragment extends ListFragment<NewsEntity, OriginalViewModel> {
    private FragmentListBaseBinding mBinding;

    private OriginalAdapter mAdapter;

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


    @Override
    public void onListClick(View view, int position, Object data) {
        super.onListClick(view, position, data);
        if (view.getId() == R.id.footerview_item) {
            if (mAdapter.isNetworkError()) {
                mListViewModel.load(LoadType.RETRY);
            }
        } else {
            NewsEntity newsEntity = (NewsEntity) data;
            DataCollectionUtils.uploadClick(getContext(), "列表", "资讯-原创", newsEntity.getTitle());

            //统计阅读量
            mAdapter.statNewsViews(newsEntity, position);
            NewsDetailActivity.startNewsDetailActivity(getContext(), newsEntity,
                    StringUtils.buildString("(资讯:原创[" + position + "])"));
        }
    }

    @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);
        mItemDecoration = itemDecoration;
        return itemDecoration;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding = FragmentListBaseBinding.bind(mCachedView);
    }

    @Override
    public void onLoadRefresh() {
        super.onLoadRefresh();
        mBinding.reuseLlLoading.getRoot().setVisibility(mListRefresh.isRefreshing() ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onLoadDone() {
        super.onLoadDone();
        mBinding.reuseLlLoading.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoConnection.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.GONE);
        mBinding.reuseDataException.getRoot().setVisibility(View.GONE);
    }

    @Override
    public void onLoadEmpty() {
        super.onLoadEmpty();
        mBinding.reuseLlLoading.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoConnection.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.VISIBLE);
        mBinding.reuseDataException.getRoot().setVisibility(View.GONE);
    }

    @Override
    public void onLoadError() {
        super.onLoadError();
        mBinding.reuseLlLoading.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoConnection.getRoot().setVisibility(View.VISIBLE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.GONE);
        mBinding.reuseDataException.getRoot().setVisibility(View.GONE);
    }

    @Override
    public void loadNotFound() {
        super.loadNotFound();
        mBinding.reuseLlLoading.getRoot().setVisibility(View.GONE);
        mBinding.reuseDataException.getRoot().setVisibility(View.VISIBLE);
        mBinding.reuseNoneData.getRoot().setVisibility(View.GONE);
        mBinding.reuseNoConnection.getRoot().setVisibility(View.GONE);
    }
}
