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.LinearLayout;
import android.widget.TextView;

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.CommonUtil;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.service.DownloadService;

import java.util.List;

/**
 * Created by ljy on 2018/9/3.
 */

public class PlayGameListAdapter extends AdbCommonRecycler<ListTypeList<GameEntity, GameListType>>{
    public PlayGameListAdapter(Context context, List<ListTypeList<GameEntity, GameListType>> list) {
        super(context, list);
    }

    @Override
    public int getViewIdByType(int type) {
        return R.layout.try_mkmy_release_task_list;
    }

    @Override
    public void convert(ViewHolder holderParent, final ListTypeList<GameEntity, GameListType> item) {
        RecyclerView recyclerView = holderParent.getView(R.id.release_task_listview, RecyclerView.class);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(new AdbCommonRecycler<GameEntity>(context, item.getList()){
            @Override
            public int getViewIdByType(int type) {
                return R.layout.play_game_vertical_list_item;
            }

            @Override
            public void convert(ViewHolder holder, final GameEntity gameEntity) {
                setOnClickGameEntity((Activity) context, holder.itemView, gameEntity);

                View item_download_welfare_line = holder.itemView.findViewById(R.id.line_tv);
                TextView detail_task_tv_center = holder.itemView.findViewById(R.id.detail_task_tv_center);
                TextView item_name_tv = holder.itemView.findViewById(R.id.item_name_tv);
                TextView item_date_tv = holder.itemView.findViewById(R.id.item_date_tv);
                LinearLayout ll_game_tag = holder.itemView.findViewById(R.id.ll_game_tag);

                ViewUtil.setText(item_name_tv, gameEntity.getApp().getName());
                ViewUtil.setText(item_date_tv, gameEntity.getApp().getPackage_size()+"M");
                GlideImageLoader.setGameImage((ImageView) holder.itemView.findViewById(R.id.item_icon_iv), gameEntity.getApp().getIcon());
                CommonUtil.getInstance().palyGameDetailBtnValue(context,gameEntity,detail_task_tv_center, 1);

                item_download_welfare_line.setVisibility(holder.getAdapterPosition() + 1 == getItemCount() ? View.GONE : View.VISIBLE);

                //tag 标签绘制
                CommonUtil.getInstance().paintLineaLayout(context, gameEntity, ll_game_tag, 4,R.color.txt_bule, R.drawable.shape_blue_stroke_rectangle_no_lb);
            }
        });



    }

    /**
     * 设置游戏中心中游戏的点击与长按
     * @param activity
     * @param view
     * @param item
     */
    public static void setOnClickGameEntity(final Activity activity, View view, final GameEntity item){
        if(item.getApp() != null){
            DownloadService.setDownLoadLongClick(view, item.getApp());
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Jump2View.getInstance().goPlayGameDetail(activity, item.getApp().getId());
                }
            });
        } else if(item.getRelease_task() != null) {
            DownloadService.setDownLoadLongClick(view, item.getRelease_task().getTask());
        }
    }
}
