package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sheep.gamegroup.greendao.download.DownLoadInfo;
import com.sheep.gamegroup.model.api.IDownload;
import com.sheep.gamegroup.model.entity.GameEntity;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DownloadUtil;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;

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

public class PlayGameItemAdapter extends AdbCommonRecycler<GameEntity>{
    private Context activity;
    public PlayGameItemAdapter(Context context, List<GameEntity> list) {
        super(context, list);
        activity = context;
    }

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

    @Override
    public void convert(ViewHolder holder, final GameEntity gameEntity) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Jump2View.getInstance().goPlayGameDetail((Activity) activity, gameEntity.getApp().getId());
            }
        });
        if(TestUtil.isTest())
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    IDownload download = gameEntity.getDownload();
                    DownloadUtil downloadUtil = new DownloadUtil();
                    final DownLoadInfo downLoadInfo = downloadUtil.getDownloadTaskByUrl(download);
                    downloadUtil.delete(downLoadInfo);
                    downloadUtil.start(downloadUtil.getDownloadTaskByUrl(download));
                    downloadUtil.setDownloadTaskStatus(download.getDownload_link(), DownloadUtil.STATUS_INIT);
                    G.showToast("游戏下载开始");
                    return true;
                }
            });
        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(activity, gameEntity, ll_game_tag, 4,R.color.txt_bule, R.drawable.shape_blue_stroke_rectangle_no_lb);


    }
}
