package com.gh.gamecenter.collection;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.view.ViewGroup;

import com.gh.gamecenter.common.callback.OnListClickListener;
import com.gh.gamecenter.common.constant.ItemViewType;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.common.viewholder.FooterViewHolder;
import com.gh.gamecenter.adapter.viewholder.ToolBoxViewHolder;
import com.gh.gamecenter.common.baselist.ListAdapter;
import com.gh.gamecenter.databinding.ToolboxItemBinding;
import com.gh.gamecenter.common.entity.ToolBoxEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by khy on 18/07/17.
 */

public class ToolsAdapter extends ListAdapter<ToolBoxEntity> {

    private OnListClickListener mListListener;

    public ToolsAdapter(Context context, OnListClickListener listListener) {
        super(context);
        mListListener = listListener;
        mEntityList = new ArrayList<>();
    }

    @Override
    protected void setListData(List<ToolBoxEntity> updateData) {
        super.setListData(updateData);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return ItemViewType.ITEM_FOOTER;
        } else {
            return ItemViewType.ITEM_BODY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ItemViewType.ITEM_FOOTER) {
            return new FooterViewHolder(mLayoutInflater.inflate(com.gh.gamecenter.common.R.layout.refresh_footerview, parent, false));
        } else {
            View view = mLayoutInflater.inflate(R.layout.toolbox_item, parent, false);
            return new ToolBoxViewHolder(ToolboxItemBinding.bind(view), mListListener);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ToolBoxViewHolder) {
            ToolBoxViewHolder viewHolder = (ToolBoxViewHolder) holder;
            ToolBoxEntity toolBoxEntity = mEntityList.get(position);
            initToolsViewHolder(viewHolder, toolBoxEntity);
        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            footerViewHolder.initItemPadding();
            footerViewHolder.initFooterViewHolder(mIsLoading, mIsNetworkError, mIsOver);
        }
    }

    @Override
    public int getItemCount() {
        return mEntityList == null || mEntityList.isEmpty() ? 0 : mEntityList.size() + FOOTER_ITEM_COUNT;
    }

    private void initToolsViewHolder(ToolBoxViewHolder viewHolder, final ToolBoxEntity toolBoxEntity) {
        viewHolder.setClickData(toolBoxEntity);
        viewHolder.binding.toolboxItemDes.setText(toolBoxEntity.getDes());
        viewHolder.binding.toolboxItemTitle.setText(toolBoxEntity.getName());
        ImageUtils.display(viewHolder.binding.toolboxItemGameThumb, toolBoxEntity.getIcon());
    }
}
