package com.gh.gamecenter.info;

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

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

import android.view.View;
import android.widget.FrameLayout;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.ViewSkeletonScreen;
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.databinding.FragmentListBaseSkeletonBinding;
import com.gh.gamecenter.entity.NewsEntity;

/**
 * Created by LGT on 2016/6/29.
 * 资讯-资讯界面
 */
public class InfoFragment extends ListFragment<NewsEntity, InfoViewModel> {

    private FragmentListBaseSkeletonBinding mBinding;

    private InfoAdapter mAdapter;
    private ViewSkeletonScreen mViewSkeletonScreen;
    private FrameLayout mSkeletonView;

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

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

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

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected View getInflatedLayout() {
        mBinding = FragmentListBaseSkeletonBinding.inflate(getLayoutInflater());
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSkeletonView = mBinding.listSkeleton;
        mViewSkeletonScreen = Skeleton.bind(mSkeletonView).shimmer(false).load(R.layout.fragment_zixun_skeleton).show();
    }

    @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 onLoadDone() {
        super.onLoadDone();
        mViewSkeletonScreen.hide();
    }

    @Override
    public void onLoadError() {
        super.onLoadError();
        mViewSkeletonScreen.hide();
    }

    @Override
    public void onLoadEmpty() {
        super.onLoadEmpty();
        mViewSkeletonScreen.hide();
    }

    @Override
    public void loadNotFound() {
        super.loadNotFound();
        mViewSkeletonScreen.hide();
    }
}
