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.GameListType;
import com.sheep.gamegroup.model.entity.ListTypeList;
import com.sheep.gamegroup.model.entity.GameRecommend;
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;


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

    @Override
    public void convert(ViewHolder viewHolder, final ListTypeList<GameRecommend, GameListType> gameListTypeList, int position) {
        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);
        ViewUtil.setText(item_play_game_list_tv, gameListTypeList.getType().getTitle());
        ViewUtil.setVisibility(item_play_game_list_more_tv, false);
        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, gameListTypeList.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<GameRecommend>(SheepApp.getInstance(), gameListTypeList.getList()) {
            @Override
            public int getViewIdByType(int type) {
                return R.layout.item_play_game;
            }

            @Override
            public void convert(com.sheep.gamegroup.util.ViewHolder holder, final GameRecommend item) {
                int index = holder.getAdapterPosition();
                TextView item_play_game_name =  holder.itemView.findViewById(R.id.item_play_game_name);
                View item_play_game_start =  holder.itemView.findViewById(R.id.item_play_game_start);
                View item_play_game_end =  holder.itemView.findViewById(R.id.item_play_game_end);
                ImageView item_play_game_iv =  holder.itemView.findViewById(R.id.item_play_game_iv);
                ViewUtil.setVisibility(item_play_game_start, index == 0 );
                ViewUtil.setVisibility(item_play_game_end, index + 1 == listData.size());
                if(item == null){
                    ViewUtil.setDefaultText(item_play_game_name);
                } else {
                    ViewUtil.setText(item_play_game_name, item.getTitle());
                    ViewUtil.setImage(item_play_game_iv, item.getUrl(), mContext.getResources().getDimensionPixelSize(R.dimen.content_padding_10));
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(gameListTypeList.getType().getTitle() != null) {
                                switch (gameListTypeList.getType().getTitle()) {
                                    case "天天更新":
                                        Jump2View.getInstance().goPlayGameDetail((Activity) mContext, item.getAppId());
                                        break;
                                    case "重点推荐":
                                        Jump2View.getInstance().goActPlayGameList((Activity) mContext, item.getType());
                                        break;
                                }
                            }
                        }
                    });
                }
            }
        });
    }
}
