package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.greendao.download.DownLoadInfo;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GameAgencyRechargePlatformEntity;
import com.sheep.gamegroup.model.entity.OrienteeringDetail;
import com.sheep.gamegroup.model.entity.PayEntity;
import com.sheep.gamegroup.model.entity.RecyleObj;
import com.sheep.gamegroup.model.entity.RecyleType;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.model.entity.TaskEty;
import com.sheep.gamegroup.model.entity.Release_task;
import com.sheep.gamegroup.presenter.TryMakeMoneyPresenter;
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.ListUtil;
import com.sheep.gamegroup.util.SelfCountDownTimer;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.service.DownloadService;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.PackageUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import rx.functions.Action1;

import static com.sheep.gamegroup.model.entity.RecyleType.RUN_TASK;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.GAME_INSTEAD_OF_RECHARGE_GAME;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.GAME_INSTEAD_OF_RECHARGE_GET;

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

public class TryMakeMoneyAdp extends AdbCommonRecycler<RecyleObj> {
    /**
     * List view item text view tag
     */
    public static final String PUBLIC_TAG_PREFIX_TEXTVIEW_LIST   = "PUBLIC_TAG_PREFIX_TEXTVIEW_LIST";
    private final static int TAG_ID_TEMP = R.id.view_tag_1;
    private HashMap<View, SelfCountDownTimer> timerHashMap= new HashMap<>();
    @Inject
    TryMakeMoneyPresenter presenter;
    private int from;// 1 来自主页
    private int type = 0;//0:推荐 1:热门 2:预约 3:游戏推荐
    private String show_type;
    private int is_succession;//0:一般任务 1:连续任务3:所有任务

    public TryMakeMoneyAdp(Context context) {
        super(context);
    }
    public TryMakeMoneyAdp(Context context, int from) {
        super(context);
        this.from = from;
    }

    @Override
    public int getViewIdByType(int type) {
        int resId = R.layout.empty;
        RecyleType recyleType = RecyleType.values()[type];
        switch (recyleType){
            case RELEASE_TASK:
            case BEGIN_TASK:
            case RUN_TASK:
            case GAME_TASK:
            case GAME_AGENCY_PAYMENT:
            case GAME_AGENCY_PAYMENT_PLATFORM:
                resId = R.layout.try_mkmy_release_task_list;
                break;
        }
        return resId;
    }

    @Override
    public void convert(ViewHolder holder, RecyleObj recyleObj) {
        switch (recyleObj.getRecyleType()){
            case RELEASE_TASK:
                bindReleaseTask(holder, (List<Release_task>) recyleObj.getDataObj(), 0);
                break;
            case RUN_TASK:
                bindRunTask(holder, (List<TaskAcceptedEty>) recyleObj.getDataObj());
                break;
            case BEGIN_TASK://即将开始
                bindReleaseTask(holder, (List<Release_task>) recyleObj.getDataObj(), 1);
                break;
            case GAME_TASK://我的游戏
                bindGameTask(holder, (List<OrienteeringDetail>) recyleObj.getDataObj());
                break;
            case GAME_AGENCY_PAYMENT_PLATFORM://游戏代充平台
                bindGamePlotForm(holder, (List<GameAgencyRechargePlatformEntity>) recyleObj.getDataObj());
                break;
            case GAME_AGENCY_PAYMENT:
                bindGameReleaseTask(holder, (List<Release_task>) recyleObj.getDataObj(), 0);
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        return listData.get(position).getRecyleType().ordinal();
    }
    /**
     * 传入presenter
     */
    public void addPresenter(TryMakeMoneyPresenter presenter){
        this.presenter = presenter;
    }
    /**
     * 传入type
     */
    public void addType(int type){
        this.type = type;
    }
    public void addShowType(String show_type){
        this.show_type = show_type;
    }
    /**
     * 传入is_succession
     */
    public void setIs_succession(int is_succession){
        this.is_succession = is_succession;
    }


    /**
     * 我的游戏
     * @param holder
     * @param etyList
     */
    private void bindGameTask(ViewHolder holder, final List<OrienteeringDetail> etyList) {
        RecyclerView recyclerView = holder.getView(R.id.release_task_listview, RecyclerView.class);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                LinearLayout view = (LinearLayout) LayoutInflater.from(context)
                        .inflate(R.layout.try_makemoney_item_recommend, viewGroup, false);
                RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(view) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
                final OrienteeringDetail item = ListUtil.getItem(etyList, i);
                if(item == null){
                    return;
                }
                viewHolder.itemView.setTag(TAG_ID_TEMP, item.getId());
                TextView detail_task_tv = viewHolder.itemView.findViewById(R.id.detail_task_tv);
                viewHolder.itemView.findViewById(R.id.task_top).setVisibility(View.GONE);

                if(i == getItemCount()-1){
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.GONE);
                }else {
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.VISIBLE);
                }
                detail_task_tv.setText("去充值");
                ((TextView)viewHolder.itemView.findViewById(R.id.item_date_tv)).setText(
                        "可定向消费:" + item.getBalance() +"元");
                ((TextView)viewHolder.itemView.findViewById(R.id.item_date_tv)).setTextColor(context.getResources().getColor(R.color.red_fd2d54));
                viewHolder.itemView.findViewById(R.id.item_date_question_img).setVisibility(View.VISIBLE);
                viewHolder.itemView.findViewById(R.id.item_date_question_img).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ViewUtil.showMsgDialog(context,
                                "游戏总金额"+ item.getBalance() +"元，按比例"+ item.getRatio() +"%进行消费，最低消费"+ item.getMin_amount() +"元。\n" +
                                        "\n" +
                                        "例如（消费100元可用"+ item.getRatio() +"元小绵羊定向消费金额）",
                                "定向消费详情",
                                false);
                    }
                });

                //CommonUtil.getInstance().getCharSequence(context, item.getName(), "")
                ((TextView)viewHolder.itemView.findViewById(R.id.item_name_tv)).setText(item.getName()+"");
                final TextView task_type_tv = viewHolder.itemView.findViewById(R.id.task_type_tv);
                task_type_tv.setVisibility(View.VISIBLE);
                viewHolder.itemView.findViewById(R.id.item_money).setVisibility(View.GONE);

                GlideImageLoader.setGameImage((ImageView) viewHolder.itemView.findViewById(R.id.item_icon_iv), item.getIcon());
                if(item.isGameReservationCantDownload()){
                    task_type_tv.setEnabled(false);
                    detail_task_tv.setEnabled(false);
                    viewHolder.itemView.setEnabled(true);
                    task_type_tv.setText("已预约");
                    task_type_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));
                    detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));

                    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Jump2View.getInstance().goGameTaskDetailView(context,  item.getRelease_task_id(), item.getGame_id(),1);

                        }
                    });
                    return;
                }
                if(TextUtils.isEmpty(item.getPackage_name())){
                    task_type_tv.setEnabled(false);
                    detail_task_tv.setEnabled(false);
                    viewHolder.itemView.setEnabled(false);
                    task_type_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));
                    detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));
                    task_type_tv.setText(R.string.error_package_link);
                    return;
                }
                task_type_tv.setEnabled(true);
                detail_task_tv.setEnabled(true);
                viewHolder.itemView.setEnabled(true);
                task_type_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_bg_gradient_rectangle_blue));
                detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_bg_gradient_rectangle_blue));
                //领取任务
                final boolean isInstall = PackageUtil.isAppInstalled(SheepApp.getInstance(), item.getPackage_name());
                boolean isExistApk = !TextUtils.isEmpty(PackageUtil.isExistsFile(item.getPackage_name(), item.getDownload_url()));
                final DownloadUtil mDownloadTaskService = new DownloadUtil();

                String taskTypeTvValue;
                if(isInstall){//已经安装
                    taskTypeTvValue = "启动游戏";
                }else{
                    DownLoadInfo downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(item.getDownload_url());
                    if(downLoadInfo == null) {
                        taskTypeTvValue = "下载游戏";
                    }else {
                        Integer mStatus = downLoadInfo.getMStatus();
                        int taskState = mStatus == null ? 0 : mStatus;
                        switch (taskState) {
                            case DownloadUtil.STATUS_ING:
                                taskTypeTvValue = Math.abs(downLoadInfo.getMPercent()) + "%";
                                break;
                            case DownloadUtil.STATUS_PAUSE:
                                taskTypeTvValue = CommonUtil.CONTINUE_DOWNLOAD;
                                break;
                            case DownloadUtil.STATUS_FINISH:
                                if(isExistApk){
                                    taskTypeTvValue = "安装游戏";
                                } else {
                                    taskTypeTvValue = CommonUtil.START_DOWNLOAD;
                                }
                                break;
                            case DownloadUtil.STATUS_FAIL:
                                taskTypeTvValue = CommonUtil.FAIL_DOWNLOAD;
                                break;
                            default:
                                taskTypeTvValue = CommonUtil.START_DOWNLOAD;
                                break;
                        }
                    }
                }
                task_type_tv.setText(taskTypeTvValue+"");
                task_type_tv.setTag(PUBLIC_TAG_PREFIX_TEXTVIEW_LIST + item.getDownload_url());
//                TaskHelper.setNumText((TextView) viewHolder.itemView.findViewById(R.id.item_num_tv), taskReleaseEty);
//                ((TextView)viewHolder.itemView.findViewById(R.id.item_money)).setText("+"+ item.getBonusText() +"元");
                //任务领取
                task_type_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try{
                            switch (task_type_tv.getText().toString().trim() +""){
                                case "安装游戏":
                                    ApkUtils.installApk(SheepApp.getInstance(), PackageUtil.isExistsFile(item.getPackage_name(), item.getDownload_url()));
                                    PackageUtil.startApp(SheepApp.getInstance(), item.getPackage_name());
                                    break;
                                case "启动游戏":
                                    ApkUtils.installApk(SheepApp.getInstance(), PackageUtil.isExistsFile(item.getPackage_name(), item.getDownload_url()));
                                    PackageUtil.startApp(SheepApp.getInstance(), item.getPackage_name());
                                    break;
                                default:
                                    final DownLoadInfo downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(item);
                                    mDownloadTaskService.start(downLoadInfo);
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if(mDownloadTaskService.addDownloadTask(downLoadInfo)){
                                                //发送红点信息
                                                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.TIPS_DOWN_ICON));
                                            }
                                        }
                                    }).start();
                                    break;
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                            G.showToast("服务器繁忙，请稍候再试!");
                        }
                    }
                });
                //去充值
                detail_task_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ViewUtil.showGamePayAccount((Activity) context, item.getGame_id(), item.getName());
                    }
                });
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Jump2View.getInstance().goGameTaskDetailView(context,  item.getRelease_task_id(), item.getGame_id(),1);

                    }
                });
//                TestUtil.setDebugDownLoad(context, viewHolder.itemView, item.getTask());
            }

            @Override
            public int getItemCount() {
                return etyList.size();
            }
        });
    }
 /**
     * 游戏代充平台
     * @param holder
     * @param etyList
     */
    private void bindGamePlotForm(ViewHolder holder, final List<GameAgencyRechargePlatformEntity> etyList) {
        RecyclerView recyclerView = holder.getView(R.id.release_task_listview, RecyclerView.class);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View view = LayoutInflater.from(context)
                        .inflate(R.layout.game_agency_recharge_plotform_item, viewGroup, false);
                RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(view) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
                final GameAgencyRechargePlatformEntity item = ListUtil.getItem(etyList, i);
                if(item == null){
                    return;
                }
                View padding = viewHolder.itemView.findViewById(R.id.padding);
                padding.setVisibility(viewHolder.getAdapterPosition() == 0 ? View.VISIBLE : View.GONE);
                TextView detail_task_tv_center = viewHolder.itemView.findViewById(R.id.detail_task_tv);
                detail_task_tv_center.setVisibility(View.VISIBLE);

                if(i == getItemCount()-1){
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.GONE);
                }else {
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.GONE);
                }
                detail_task_tv_center.setText("充值");

                //name
                ((TextView)viewHolder.itemView.findViewById(R.id.item_name_tv)).setText(item.getName()+"");

                final TextView item_num_tv = viewHolder.itemView.findViewById(R.id.item_num_tv);
                item_num_tv.setText(item.getDesc()+"");

                Glide.with(context)
                        .load(item.getIcon())
                        .apply(new RequestOptions().placeholder(R.mipmap.icon))
                        .into((ImageView) viewHolder.itemView.findViewById(R.id.item_icon_iv));


                //去充值
                detail_task_tv_center.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PayEntity payEntity = new PayEntity();
                        payEntity.setTask_type(item.getTask_type()+"");
                        ViewUtil.newInstance().showGamePayAccount((Activity) context, payEntity);

//                        ViewUtil.showGamePayAccount((Activity) context, item.getTask_type());
                    }
                });
            }

            @Override
            public int getItemCount() {
                return etyList.size();
            }
        });
    }

    /**
     * 游戏代充
     */
    private void bindGameReleaseTask(ViewHolder holder, final List<Release_task> etyList, final int isBegin){
        RecyclerView recyclerView = holder.getView(R.id.release_task_listview, RecyclerView.class);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(new RecyclerView.Adapter(){

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                LinearLayout view = (LinearLayout) LayoutInflater.from(context)
                        .inflate(R.layout.try_makemoney_item_recommend, viewGroup,false);
                RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(view) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
                final Release_task taskReleaseEty = ListUtil.getItem(etyList, i);
                final TaskEty taskEty;
                if (taskReleaseEty == null || (taskEty = taskReleaseEty.getTask()) == null) {
                    return;
                }
                viewHolder.itemView.setTag(TAG_ID_TEMP, etyList.get(i).getId());
                ((TextView) viewHolder.itemView.findViewById(R.id.item_name_tv)).setText(taskReleaseEty.getName() + "");
                ((TextView) viewHolder.itemView.findViewById(R.id.item_num_tv)).setText(taskReleaseEty.getTask().getDetailShow()+"");
                ((TextView) viewHolder.itemView.findViewById(R.id.item_money)).setVisibility(View.GONE);
                GlideImageLoader.setGameImage((ImageView) viewHolder.itemView.findViewById(R.id.item_icon_iv), taskEty.getIcon());
                viewHolder.itemView.setEnabled(false);
                ((TextView) viewHolder.itemView.findViewById(R.id.item_date_tv)).setVisibility(View.GONE);
                viewHolder.itemView.findViewById(R.id.task_top).setVisibility(View.GONE);

                if (i == getItemCount() - 1) {
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.GONE);
                } else {
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.VISIBLE);
                }

                TextView detail_task_tv = viewHolder.itemView.findViewById(R.id.detail_task_tv);
                TextView detail_task_tv_down = viewHolder.itemView.findViewById(R.id.detail_task_tv_down);


                if(!taskReleaseEty.getTask().isCan_receive_account()){
//                    detail_task_tv.setText("已有账号");
//                    detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));
//                    detail_task_tv.setEnabled(false);
//                    viewHolder.itemView.setEnabled(false);
                    detail_task_tv.setVisibility(View.GONE);
                    detail_task_tv_down.setVisibility(View.VISIBLE);
                    taskReleaseEty.getDownloadHelper().updateDownloadTaskView((Activity) context, taskEty, detail_task_tv_down);
                    detail_task_tv_down.setTag(PUBLIC_TAG_PREFIX_TEXTVIEW_LIST + taskEty.getDownload_link());
                }else {
                    detail_task_tv.setVisibility(View.VISIBLE);
                    detail_task_tv_down.setVisibility(View.GONE);
                    detail_task_tv.setText("领取帐号");
                    detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_bg_gradient_rectangle_blue));
                    detail_task_tv.setEnabled(true);
                    viewHolder.itemView.setEnabled(false);
                    detail_task_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            GAME_INSTEAD_OF_RECHARGE_GET.onEvent();
                            CommonUtil.getInstance()
                                    .gameAccountManager(context, Integer.parseInt(taskReleaseEty.getTask_id()), new Action1<BaseMessage>() {
                                        @Override
                                        public void call(BaseMessage baseMessage) {
                                            G.showToast("领取成功");
                                            EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.GENERATION_ACCOUNT_REFRESH));


                                        }
                                    });
                        }
                    });
                }

                /**
                 * 立即充值
                 */
                TextView task_type_tv = viewHolder.itemView.findViewById(R.id.task_type_tv);
                task_type_tv.setText("立即充值");
                task_type_tv.setVisibility(View.VISIBLE);
                task_type_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GAME_INSTEAD_OF_RECHARGE_GAME.onEvent();
                        PayEntity payEntity = new PayEntity();
                        payEntity.setTask_type(taskEty.getTask_type()+"");
                        payEntity.setGame_id(taskReleaseEty.getTask().getThird_task_id());
                        payEntity.setGame_name(taskReleaseEty.getName());
                        ViewUtil.newInstance().showGamePayAccount((Activity) context, payEntity);
                    }
                });

            }

            @Override
            public int getItemCount() {
                return etyList.size();
            }
        });
    }
    /**
     * 获取已发布的
     */
    private void bindReleaseTask(ViewHolder holder, final List<Release_task> etyList, final int isBegin){
        RecyclerView recyclerView = holder.getView(R.id.release_task_listview, RecyclerView.class);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(new RecyclerView.Adapter(){

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                LinearLayout view = (LinearLayout) LayoutInflater.from(context)
                        .inflate(R.layout.try_makemoney_item_recommend, viewGroup,false);
                RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(view) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
                final Release_task taskReleaseEty = ListUtil.getItem(etyList, i);
                final TaskEty taskEty;
                if (taskReleaseEty == null || (taskEty = taskReleaseEty.getTask()) == null) {
                    return;
                }
                viewHolder.itemView.setTag(TAG_ID_TEMP, etyList.get(i).getId());
                ((TextView) viewHolder.itemView.findViewById(R.id.item_name_tv)).setText(taskReleaseEty.getName() + "");
                CommonUtil.getInstance().setTaskTag(context, viewHolder.itemView, taskReleaseEty,show_type);
                ((TextView) viewHolder.itemView.findViewById(R.id.item_money)).setText("+" + taskReleaseEty.getBonusText() + "元");

                GlideImageLoader.setGameImage((ImageView) viewHolder.itemView.findViewById(R.id.item_icon_iv), taskEty.getIcon());
                if (isBegin == 1 || type == 2) {//即将开始
                    viewHolder.itemView.setEnabled(false);
                    ((TextView) viewHolder.itemView.findViewById(R.id.item_date_tv)).setText(
                            "开始时间:" + TimeUtil.getDate(TimeUtil.FORMAT_DD, taskReleaseEty.getOnline_time()) + "");
                } else {
                    if (!taskReleaseEty.getDeadline().equals("永久")) {
                        ((TextView) viewHolder.itemView.findViewById(R.id.item_date_tv)).setText(
                                "截止时间:" + TimeUtil.getDate(TimeUtil.FORMAT_DD, Long.valueOf(taskReleaseEty.getDeadline())) + "");
                    } else {
                        ((TextView) viewHolder.itemView.findViewById(R.id.item_date_tv)).setText(
                                "截止时间:" + taskReleaseEty.getDeadline());
                    }
                }
                if (i == 0 && TextUtils.isEmpty(show_type)) {
                    viewHolder.itemView.findViewById(R.id.task_top).setVisibility(View.VISIBLE);
                    if (isBegin == 1) {
                        ((ImageView) viewHolder.itemView.findViewById(R.id.item_title_iv)).setImageResource(R.mipmap.jijiangks);
                        ((TextView) viewHolder.itemView.findViewById(R.id.item_title_tv)).setText("即将开始");
                    } else {
                        switch (type) {
                            case 1:
                                ((ImageView) viewHolder.itemView.findViewById(R.id.item_title_iv)).setImageResource(R.mipmap.remenyx);
                                ((TextView) viewHolder.itemView.findViewById(R.id.item_title_tv)).setText("每日必做");
                                break;
                            case 2:
                                ((ImageView) viewHolder.itemView.findViewById(R.id.item_title_iv)).setImageResource(R.mipmap.jijiangks);
                                ((TextView) viewHolder.itemView.findViewById(R.id.item_title_tv)).setText("即将开始");
                                break;
                            case 0:
                            default:
                                ((ImageView) viewHolder.itemView.findViewById(R.id.item_title_iv)).setImageResource(R.mipmap.kejiequ);
                                ((TextView) viewHolder.itemView.findViewById(R.id.item_title_tv)).setText("可接取");
                                break;
                        }
                    }
                } else {
                    viewHolder.itemView.findViewById(R.id.task_top).setVisibility(View.GONE);
                }
                if (i == getItemCount() - 1) {
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.GONE);
                } else {
                    viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.VISIBLE);
                }
                DownloadService.setDownLoadLongClick(viewHolder.itemView, taskEty);
                TextView detail_task_tv = viewHolder.itemView.findViewById(R.id.detail_task_tv);


                if("0".equals(taskReleaseEty.getLast_num())){
                    detail_task_tv.setText("任务已抢光");
                    detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));
                    detail_task_tv.setEnabled(false);
                    viewHolder.itemView.setEnabled(false);
                }else {
                    if(isBegin == 1 || type == 2){//即将开始
                        detail_task_tv.setText("即将开始");
                        detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_rectangle_bg_gray_radius));
                        detail_task_tv.setEnabled(false);
                        viewHolder.itemView.setEnabled(false);
                    } else if(taskEty.isGameReservationCantDownload()){//游戏任务中的预约任务，且不能下载情况下
                        detail_task_tv.setText("预约下载");
                        detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_bg_gradient_rectangle_blue));
                        detail_task_tv.setEnabled(true);
                        viewHolder.itemView.setEnabled(true);
                    } else {
                        detail_task_tv.setText("开始赚钱");
                        detail_task_tv.setBackground(context.getResources().getDrawable(R.drawable.sp_bg_gradient_rectangle_blue));
                        detail_task_tv.setEnabled(true);
                        viewHolder.itemView.setEnabled(true);
                    }
                }


                detail_task_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(taskEty.isGameReservationCantDownload()) {//游戏任务中的预约任务，且不能下载情况下
                            Action1<Integer> action1 = new Action1<Integer>() {
                                @Override
                                public void call(Integer type) {
                                    switch (type) {
                                        case 1:
                                            Jump2View.getInstance().goGameMakeMoney(context, 1);
                                            break;
                                    }
                                }
                            };
                            if(taskReleaseEty.getAccepted_task_id() < 1){//没有正在进行的任务
                                CommonUtil.getInstance().reservationGameTask(context, taskReleaseEty, action1);
                            } else {//先取消正在进行的任务
                                CommonUtil.getInstance().tryGiveUpTask(context, taskReleaseEty, action1);
                            }
                            return;
                        }
                        viewHolder.itemView.performClick();
//                        taskEty.setRunTask(0);
//                        taskEty.setRelease_task_id(taskReleaseEty.getId());
//                        taskEty.setName(taskReleaseEty.getName());
//                        taskEty.setAcceptedTaskId(taskReleaseEty.getAccepted_task_id());
//                        Jump2View.getInstance().goDialogActivityView(context, taskEty);
                    }
                });
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UMConfigUtils.IdEvent.TASK.commit(taskReleaseEty.getId());
                        taskEty.setRunTask(0);
                        taskEty.setBonus(taskReleaseEty.getBonusText());
                        taskEty.setRelease_task_id(taskReleaseEty.getId());
                        taskEty.setName(taskReleaseEty.getName());
                        if("0".equals(taskReleaseEty.getLast_num())){
                            Jump2View.getInstance().goTaskDetailView(context,  taskReleaseEty.getId(), true);
                            return;
                        }
                        Jump2View.getInstance().goTaskDetailView(context,  taskReleaseEty.getId());
                    }
                });

            }

            @Override
            public int getItemCount() {
                return etyList.size();
            }
        });
    }

    /**
     * 暂时这么处理，也许以后也是列表了
     * @param holder
     * @param etyList
     */
    private void bindRunTask(ViewHolder holder, final List<TaskAcceptedEty> etyList){
        RecyclerView recyclerView = holder.getView(R.id.release_task_listview, RecyclerView.class);
        recyclerView.setHasFixedSize(true);
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,
//                StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(new RecyclerView.Adapter(){

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                LinearLayout view = (LinearLayout) LayoutInflater.from(context)
                        .inflate(R.layout.try_makemoney_item_recommend, viewGroup,false);
                RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(view) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
                final TaskAcceptedEty taskAcceptedEty;
                final Release_task taskReleaseEty;
                final TaskEty taskEty;
                if((taskAcceptedEty = ListUtil.getItem(etyList, i)) == null
                        || (taskReleaseEty = taskAcceptedEty.getRelease_task()) == null
                        || (taskEty = taskReleaseEty.getTask()) == null){
                    return;
                }
                    viewHolder.itemView.setTag(TAG_ID_TEMP, etyList.get(i).getId());
                if(i == 0){
                    viewHolder.itemView.findViewById(R.id.task_top).setVisibility(View.VISIBLE);
                    viewHolder.itemView.findViewById(R.id.item_title_tv).setVisibility(View.VISIBLE);
                    ((TextView)viewHolder.itemView.findViewById(R.id.item_title_tv)).setText("进行中");
                }else{
                    viewHolder.itemView.findViewById(R.id.task_top).setVisibility(View.GONE);
                }
                viewHolder.itemView.findViewById(R.id.line_tv).setVisibility(View.GONE);


                ((TextView)viewHolder.itemView.findViewById(R.id.item_name_tv)).setText(taskReleaseEty.getName()+"");
                CommonUtil.getInstance().setTaskTag(context, viewHolder.itemView, taskReleaseEty,show_type);
                ((TextView)viewHolder.itemView.findViewById(R.id.item_money)).setText("+"+ taskReleaseEty.getBonusText() +"元");
                if(!taskReleaseEty.getDeadline().equals("永久")){
                    ((TextView)viewHolder.itemView.findViewById(R.id.item_date_tv)).setText(
                            "截止时间:" + TimeUtil.getDate(TimeUtil.FORMAT_DD, Long.valueOf(taskReleaseEty.getDeadline()))+"");
                }else {
                    ((TextView)viewHolder.itemView.findViewById(R.id.item_date_tv)).setText(
                            "截止时间:" + taskReleaseEty.getDeadline());
                }

                ((TextView)viewHolder.itemView.findViewById(R.id.detail_task_tv)).setText("取消任务");

                if((is_succession == 1 || is_succession == 3)
                        &&  (taskAcceptedEty.getChild_task_id() > 1
                                || (taskAcceptedEty.getChild_task_id() == 1
                        && (taskAcceptedEty.getStatus() == 6 || taskAcceptedEty.getStatus() == 7 || taskAcceptedEty.getStatus() == 9)))
                        ){
                    viewHolder.itemView.findViewById(R.id.detail_task_tv).setVisibility(View.GONE);
                }else {

                    viewHolder.itemView.findViewById(R.id.detail_task_tv).setVisibility(View.VISIBLE);
                }


                TextView task_type_tv = viewHolder.itemView.findViewById(R.id.task_type_tv);

                //设置标签颜色task_type_tv
                //Todo 2.6又不需要了
//                if(!TextUtils.isEmpty(taskReleaseEty.getChild_task_name())){
//                    task_type_tv.setVisibility(View.VISIBLE);
//                    task_type_tv.setText(taskReleaseEty.getChild_task_name() + "");
//                    CommonUtil.getInstance()
//                            .setViewBackground(
//                                    task_type_tv,
//                                    taskReleaseEty.getLabel_color()+"");
//                }else {
//                    task_type_tv.setVisibility(View.INVISIBLE);
//                }
                viewHolder.itemView.findViewById(R.id.detail_task_tv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        presenter.giveUpTask(taskAcceptedEty.getId());
                        //暂停下载
                        CommonUtil.getInstance().stopDownloadTask(context, taskEty.getDownload_link());

                    }
                });
                GlideImageLoader.setGameImage((ImageView) viewHolder.itemView.findViewById(R.id.item_icon_iv), taskEty.getIcon());
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TaskAcceptedEty item = ListUtil.getItem(etyList, i);
                        if(item == null){
                            return;
                        }
                        UMConfigUtils.IdEvent.TASK.commit(item.getRelease_task_id());

                        taskEty.setRunTask(1);
                        taskEty.setBonus(taskReleaseEty.getBonusText());
                        taskEty.setRelease_task_id(taskReleaseEty.getId());
                        taskEty.setAcceptedTaskId(taskAcceptedEty.getId());
                        taskEty.setName(taskReleaseEty.getName());
                        Jump2View.getInstance().goTaskDetailView(context,  taskReleaseEty.getId());
                        UMConfigUtils.onEvent(UMConfigUtils.Event.TASK_DETAIL);
                    }
                });
                DownloadService.setDownLoadLongClick(viewHolder.itemView, taskEty);

                /*
                  倒计时
                 */
                final TextView countdown_tv = viewHolder.itemView.findViewById(R.id.end_of_time_tv);
                if(taskAcceptedEty.getEnd_time() > 0){
                    SelfCountDownTimer scTimer = timerHashMap.get(countdown_tv);
                    if(scTimer != null){
                        scTimer.cancel();
                    }

                    countdown_tv.setVisibility(View.VISIBLE);
                    SelfCountDownTimer selfCountDownTimer = new SelfCountDownTimer( taskAcceptedEty.getEnd_time() * 1000, 1000, taskReleaseEty.getCancel_duration()) {
                        @Override
                        public void onTimerRest() {
                            countdown_tv.setText("");
                        }

                        @Override
                        public void onTimerTick(long millisUntilFinished, int counTime) {
                            countdown_tv.setText(TimeUtil.getHours(millisUntilFinished));
                        }

                        @Override
                        public void onTimerFinish() {
                            countdown_tv.setVisibility(View.GONE);
//                            presenter.giveUpTask(taskAcceptedEty.getId());
//                            //暂停下载
//                            CommonUtil.getInstance().stopDownloadTask(mContext, taskEty.getDownload_link());
                        }
                    };
                    selfCountDownTimer.reset(taskReleaseEty.getCancel_duration());
                    selfCountDownTimer.start();
                    timerHashMap.put(countdown_tv, selfCountDownTimer);
                }else {
                    countdown_tv.setVisibility(View.GONE);
                }

            }

            @Override
            public int getItemCount() {
                return etyList.size();
            }
        });
    }

    /**
     * 重置map参数
     */
    public void cancelAllTimers() {
        Set<Map.Entry<View, SelfCountDownTimer>> s = timerHashMap.entrySet();
        Iterator it = s.iterator();
        while (it.hasNext()) {
            try {
                Map.Entry pairs = (Map.Entry) it.next();
                SelfCountDownTimer cdt = (SelfCountDownTimer) pairs.getValue();
                cdt.cancel();
            } catch (Exception ignored) {
            }
        }
        timerHashMap.clear();
    }

    public void refreshAdapter(){
        notifyDataSetChanged();
    }

    public boolean isEmpty() {
        switch (type){
            case 0://推荐任务
                RecyleObj item;
                if((item = ListUtil.getItem(listData, 0)) != null
                        && item.getRecyleType() == RUN_TASK
                        && !ListUtil.isEmpty((List<TaskAcceptedEty>)item.getDataObj())){
                    return false;
                }else {
                    return true;
                }
            case 2://即将开始
                return true;
        }
        return false;
    }
}
