package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kfzs.appstore.utils.adapter.recyclerview.RecyclerViewAdapter;
import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.sheep.gamegroup.model.entity.GameEntity;
import com.sheep.gamegroup.model.entity.GameListType;
import com.sheep.gamegroup.model.entity.ListTypeList;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.List;

import static com.sheep.gamegroup.view.adapter.PlayGameListAdapter.setOnClickGameEntity;


/**
 * Created by realicing on 2018/10/30.
 * realicing@sina.com
 * 游戏主页上每一屏显示有3.3个元素
 */
public class AdpGameCenter3_3List extends RecyclerViewAdapter<ListTypeList<GameEntity, GameListType>> {
    public AdpGameCenter3_3List(Context context, List<ListTypeList<GameEntity, GameListType>> itemList) {
        super(context, R.layout.item_play_game_list, itemList);
    }

    public AdpGameCenter3_3List setShowFirstTop(boolean showFirstTop) {
        isShowFirstTop = showFirstTop;
        return this;
    }

    private boolean isShowFirstTop = true;

    @Override
    public void convert(ViewHolder viewHolder, final ListTypeList<GameEntity, GameListType> itemList, int position) {
        View item_play_game_list_top = viewHolder.itemView.findViewById(R.id.item_play_game_list_top);
        TextView item_play_game_list_tv = viewHolder.itemView.findViewById(R.id.item_play_game_list_tv);
        TextView item_play_game_list_more_tv = viewHolder.itemView.findViewById(R.id.item_play_game_list_more_tv);
        if (!isShowFirstTop)//不显示第一项的线
            ViewUtil.setVisibility(item_play_game_list_top, position != 0);
        ViewUtil.setText(item_play_game_list_tv, itemList.getType().getTitle());
        item_play_game_list_more_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mContext instanceof Activity)
                    Jump2View.getInstance().goActPlayGameList((Activity) mContext, itemList.getType());
            }
        });
        RecyclerView recyclerView = viewHolder.itemView.findViewById(R.id.item_play_game_list_rv);
        LinearLayoutManager playHotManager = new LinearLayoutManager(SheepApp.getInstance(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(playHotManager);
        recyclerView.setAdapter(new AdbCommonRecycler<GameEntity>(SheepApp.getInstance(), itemList.getList()) {
            @Override
            public int getViewIdByType(int type) {
                return R.layout.item_hot_welfare;
            }

            @Override
            public void convert(com.sheep.gamegroup.util.ViewHolder holder, GameEntity item) {
                ImageView item_hot_welfare_iv = holder.itemView.findViewById(R.id.item_hot_welfare_iv);
                TextView item_hot_welfare_tv = holder.itemView.findViewById(R.id.item_hot_welfare_tv);
                if (item == null) {
                    ViewUtil.setDefaultText(item_hot_welfare_tv);
                } else {
                    // 加载数据
                    ViewUtil.setText(item_hot_welfare_tv, item.getApp().getName());
                    GlideImageLoader.setGameImage(item_hot_welfare_iv, item.getApp().getIcon());
                    if (mContext instanceof Activity)
                        setOnClickGameEntity((Activity) mContext, holder.itemView, item);
                }
            }
        });
    }
}
