package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadTarget;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.IEntity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.kfzs.duanduan.datashare.DDProviderHelper;
import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
import com.kfzs.duanduan.event.BigEvent;
import com.kfzs.duanduan.event.EventTypes;
import com.kfzs.duanduan.services.DownloadTaskService;
import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.gamegroup.di.components.DaggerTaskDetailComponent;
import com.sheep.gamegroup.di.modules.TaskDetailModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.DialogEntity;
import com.sheep.gamegroup.model.entity.Ext;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.model.entity.TaskChild;
import com.sheep.gamegroup.model.entity.TaskEty;
import com.sheep.gamegroup.model.entity.TaskListen;
import com.sheep.gamegroup.model.entity.TaskReleaseEty;
import com.sheep.gamegroup.model.util.EntityUtils;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.presenter.TaskDetailContract;
import com.sheep.gamegroup.presenter.TaskDetailPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DeviceUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.gamegroup.util.MyListview;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.view.adapter.AdpTaskDetailChildListview;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.AbsChooseImageActivity;
import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;

/**
 * 任务详情页面
 * Created by ljy on 2018/4/2.
 */

public class TaskDetailAct extends AbsChooseImageActivity implements TaskDetailContract.View {

    @BindView(R.id.icon_iv)
    ImageView iconIv;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.date_tv)
    TextView dateTv;
    @BindView(R.id.price_tv)
    TextView priceTv;
    @BindView(R.id.detail_task_tv)
    TextView detailTaskTv;
    @BindView(R.id.web_text)
    WebView webText;
    @BindView(R.id.ll_infos)
    LinearLayout llInfos;
    @BindView(R.id.btn_task_item)
    TextView btnTaskItem;
    @BindView(R.id.btn_up_imag)
    TextView btnUpImag;
    @BindView(R.id.bottom_btn_layout)
    LinearLayout bottom_btn_layout;
    @BindView(R.id.detail_task_listview)
    MyListview detail_task_listview;
    @BindView(R.id.empty_view)
    View empty_view;
    private Activity activity;
    @Inject
    TaskDetailPresenter presenter;

    private TaskEty taskEty;

    //通过task_id找
    private TaskReleaseEty taskReleaseEty;
    private TaskAcceptedEty taskAcceptedEty;

    private int taskId = -1;

    private int type;//-1:开始任务 1：下载 2：安装 3：打开  10：打开H5
    private boolean isDownLoad = false;//是否下载

    private AdpTaskDetailChildListview adpTaskDetailChildListview;
    private List<TaskChild> taskChildList = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.task_detail_layout;
    }

    @Override
    public void initView() {
        activity = this;
        taskAcceptedEty = (TaskAcceptedEty) getIntent().getSerializableExtra("task_entity");
        taskId = getIntent().getIntExtra("task_id", -1);
        DaggerTaskDetailComponent.builder()
                .netComponent(SheepApp.get(activity).getNetComponent())
                .taskDetailModule(new TaskDetailModule(this))
                .build()
                .inject(this);
        TitleBarUtils.getInstance()
                .setTitle(activity, "任务详情")
                .setTitleFinish(activity);
        Aria.download(this).register();
        mDownloadTaskService = new DownloadTaskService(this);
        adpTaskDetailChildListview = new AdpTaskDetailChildListview(taskChildList, activity);
        detail_task_listview.setAdapter(adpTaskDetailChildListview);
        adpTaskDetailChildListview.notifyDataSetChanged();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        if (taskId != -1 && !isGetTaskDesc) {
            isGetTaskDesc = true;
            presenter.taskDesc(taskId);
            return;
        }
        if (taskAcceptedEty == null) {
            G.showToast("网络异常，请稍候再试");
//            finish();
            return;
        }
        taskReleaseEty = taskAcceptedEty.getRelease_task();
        initWigget();
    }

    private void initWigget() {
        taskEty = taskReleaseEty.getTask();
        if (taskEty.getChild() != null && taskEty.getChild().size() > 0) {
            taskChildList.clear();
            taskChildList.addAll(taskEty.getChild());
        }

        downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(taskEty);
        webText.setWebChromeClient(new WebChromeClient() {

        });
        webText.getSettings().setJavaScriptEnabled(true);
        webText.setWebViewClient(new WebViewClient() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadDataWithBaseURL(null, request.getUrl().toString(), "text/html", "utf-8", null);
                return true;
            }
        });
        //封装头文件
        String sHead = "<html><head><meta name=\"viewport\" content=\"width=device-width, " +
                "initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes\" />" +
                "<style>img{max-width:100% !important;height:auto !important;}</style>"
                + "<style>body{max-width:100% !important;}</style>" + "</head><body>";
        webText.loadDataWithBaseURL(null, sHead + taskEty.getDesc() + "</body></html>", "text/html", "utf-8", null);
        nameTv.setText(taskReleaseEty.getName() + "");
        priceTv.setText("+" + taskReleaseEty.getBonus() + "元");
        dateTv.setText(TimeUtil.getDate(TimeUtil.FORMAT, Long.valueOf(taskEty.getUpdate_time())) + "");


        Glide.with(SheepApp.mContext)
                .load(taskEty.getIcon())
                .apply(new RequestOptions().placeholder(R.drawable.icon)
                        .dontAnimate())
                .into(iconIv);


        bottom_btn_layout.setVisibility(View.VISIBLE);

        if (taskChildList != null && taskChildList.size() > 0) {
            detail_task_listview.setVisibility(View.VISIBLE);
            adpTaskDetailChildListview.notifyDataSetChanged();
        } else {

            detail_task_listview.setVisibility(View.GONE);
        }

        if (taskAcceptedEty != null) {
            checkAcceptedTask();
            return;
        }
        detailTaskTv.setVisibility(View.INVISIBLE);

        if (!taskReleaseEty.isIs_running() || taskEty.getInspect_type() == 1) {
            btnUpImag.setVisibility(View.GONE);
        } else {
            taskEty.setRunTask(1);
            btnUpImag.setVisibility(View.VISIBLE);
        }
        switch (taskEty.getTask_type()) {
            case 1:
            case 3:
            default:
                if (taskReleaseEty.isIs_running()) {

                    setBtnStr(taskEty.getTask_type());
                } else {
                    type = -1;
                    btnTaskItem.setText("领取任务");
                }
                break;
            case 2:
                btnTaskItem.setText("信用卡注册");
                break;
            case 4:
                if (taskReleaseEty.isIs_running()) {
                    type = 10;
                    btnTaskItem.setText("开始任务");
                } else {
                    type = -1;
                    btnTaskItem.setText("领取任务");
                }
                break;

        }
        if (type >= 3) {

            btnUpImag.setVisibility(View.VISIBLE);
        } else {

            btnUpImag.setVisibility(View.GONE);
        }

        taskSchedule();
    }

    private void checkAcceptedTask() {
        if (taskAcceptedEty != null) {
            switch (taskAcceptedEty.getStatus()) {
                case 1://"已接受任务";
                case 2:// "正在进行中";
                    taskId = taskAcceptedEty.getRelease_task_id();
                    taskAcceptedEty = null;
                    taskReleaseEty = null;
                    taskEty = null;
                    initData();
                    return;
                case 6:
                    //审核失败
                    btnUpImag.setText("重新提交");
                    btnUpImag.setVisibility(View.VISIBLE);
                    btnTaskItem.setVisibility(View.GONE);
                    llInfos.removeAllViews();
                    llInfos.addView(getStep("领取任务", "已完成", 0));
                    llInfos.addView(getStep("APP下载", "已完成", 0));
                    llInfos.addView(getStep("完成任务", "审核失败", 1));
                    return;
                //不可操作
                case 7:// "提交审核";
                    llInfos.removeAllViews();
                    llInfos.addView(getStep("领取任务", "已完成", 0));
                    llInfos.addView(getStep("APP下载", "已完成", 0));
                    llInfos.addView(getStep("完成任务", "审核中", 1));
                    bottom_btn_layout.setVisibility(View.GONE);
                    return;
                case 3://完成任务
                    llInfos.removeAllViews();
                    llInfos.addView(getStep("领取任务", "已完成", 0));
                    llInfos.addView(getStep("APP下载", "已完成", 0));
                    llInfos.addView(getStep("完成任务", "已完成", 0));
                    bottom_btn_layout.setVisibility(View.GONE);
                    return;
            }
            bottom_btn_layout.setVisibility(View.GONE);
            ((View)llInfos.getParent()).setVisibility(View.GONE);
        }
    }

    private void setBtnStr(int taskType) {
        isDownLoad = PackageUtil.isAppInstalled(SheepApp.mContext, taskEty.getPackage_names());
        if (taskEty.getDownload_link() == null || !taskEty.getDownload_link().startsWith("http")) {
            G.showToast(R.string.error_download_link);
            return;
        }
        DownloadTarget task = Aria.download(this).load(taskEty.getDownload_link());
        int taskState = task.getTaskState();
        String buttonStr = "";
        if (isDownLoad) {
            type = 3;
            if (taskType != 3) {
                buttonStr = CommonUtil.START_PLAY;

            } else {//应用
                buttonStr = CommonUtil.OPEN_APPLY;

            }
        } else {
            switch (taskState) {
                case IEntity.STATE_RUNNING:
                    type = 1;
                    buttonStr = task.getPercent() + "%";
                    break;
                case IEntity.STATE_STOP:
                    type = 1;
                    buttonStr = CommonUtil.CONTINUE_DOWNLOAD;
                    break;
                case IEntity.STATE_COMPLETE:
                    if (PackageUtil.isAppInstalled(SheepApp.mContext, taskEty.getPackage_names())) {
                        type = 3;
                        if (taskType != 3) {
                            buttonStr = CommonUtil.START_PLAY;

                        } else {//应用
                            buttonStr = CommonUtil.OPEN_APPLY;

                        }
                    } else if (!TextUtils.isEmpty(PackageUtil.isExistsFile(activity, taskEty.getPackage_names(), ""))) {
                        type = 2;
                        btnTaskItem.setText("开始安装");
                        buttonStr = CommonUtil.START_INSTALL;
                    } else {
                        type = 1;
                        task.resetState();
                        buttonStr = CommonUtil.START_DOWNLOAD;

                    }
                    break;
                case IEntity.STATE_FAIL:
                    type = 1;
                    buttonStr = CommonUtil.FAIL_DOWNLOAD;
                    break;
                default:
                    type = 1;
                    buttonStr = CommonUtil.START_DOWNLOAD;
                    break;
            }

        }
        btnTaskItem.setText(buttonStr + "");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 下载模块修改
     */
    private DownloadTaskService mDownloadTaskService;
    DownLoadInfo downLoadInfo;

    @OnClick({R.id.detail_task_tv, R.id.web_text, R.id.ll_infos, R.id.btn_task_item, R.id.btn_up_imag})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.detail_task_tv://查看任务截图
                if (TextUtils.isEmpty(taskEty.getScreenshots())) {
                    G.showToast("任务截图为空");
                    return;
                }
                Jump2View.getInstance().showImgs(activity, 0, taskEty.getScreenshots());
                break;
            case R.id.web_text:
                break;
            case R.id.ll_infos:
                break;
            case R.id.btn_task_item:
                switch (type) {
                    case -1:
                        if (taskReleaseEty.getAccepted_task_id() > 0) {
                            Jump2View.getInstance()
                                    .goNoticeAct(activity, new DialogEntity(null,
                                            "是否放弃正在运行中的任务?",
                                            "是",
                                            "否",
                                            null));
                        } else {
                            acceptedTask();
                        }
                        break;
                    case 1://开始下载

                        DownloadTarget task = null;
                        if (TextUtils.isEmpty(taskReleaseEty.getTask().getDownload_link()) || !taskReleaseEty.getTask().getDownload_link().contains("http")) {
                            G.showToast(R.string.error_download_link);
                            return;
                        }
                        task = Aria.download(this).load(taskReleaseEty.getTask().getDownload_link());

                        String filePath = DIR + File.separator + taskReleaseEty.getTask().getPackage_names() + ClassFileHelper.FILE_SUFFIX;
                        if (task.getTaskState() == IEntity.STATE_RUNNING) {
                            task.stop();
                        } else if (task.getTaskState() == IEntity.STATE_FAIL) {
                            task.cancel(true);
                            task.resetState();
                            task.removeRecord();
                        } else {
//                            task.setFilePath(filePath);
//                            task.start();
                            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();
                        UMConfigUtils.onEvent(UMConfigUtils.Event.TASK_DOWNLOAD);
                        break;
                    case 2:
                        ApkUtils.installApk(SheepApp.mContext, PackageUtil.isExistsFile(activity, taskEty.getPackage_names(), ""));
                        break;
                    case 3:
                        PackageUtil.startApp(SheepApp.mContext, taskEty.getPackage_names());
                        break;
                    case 10:
                        Ext ext;
                        if (taskEty == null || (ext = EntityUtils.getExtInfo(taskEty.getExt())) == null || TextUtils.isEmpty(ext.getUrl())) {
                            G.showToast("服务器数据错误，请联系客服或者稍候重试");
                        } else {
                            Jump2View.getInstance().goWeb(activity, ext.getUrl());
                        }
                        break;
                }
                break;
            case R.id.btn_up_imag:
                showChooseDialog(false, true, DEFAULT_MAX_COUNT);
                UMConfigUtils.onEvent(UMConfigUtils.Event.TASK_UPLOAD_PICTURE);
                break;
        }
    }

    /**
     * 任务进度
     */
    private void taskSchedule() {
        llInfos.removeAllViews();
        if (taskEty != null) {
            int thisRun = taskEty.getRunTask();
            if (thisRun == 1) {//接受了任务
                llInfos.addView(getStep("领取任务", "已完成", 0));
                if (taskEty.getTask_type() == 2) {//信用卡
                    llInfos.addView(getStep("信用卡注册", "待完成", 0));
                } else if (PackageUtil.isAppInstalled(SheepApp.mContext, taskEty.getPackage_names())) {
                    llInfos.addView(getStep("APP下载", "已完成", 0));
                    llInfos.addView(getStep("完成任务", "进行中", 1));
                } else {
                    llInfos.addView(getStep("APP下载", "待完成", 2));
                }
            } else {
                llInfos.addView(getStep("领取任务", "待完成", 2));
            }
        } else {
            llInfos.addView(getStep("领取任务", "待完成", 2));
        }
    }


    private View getStep(String step, String state, int color) {
        View view = View.inflate(this, R.layout.task_step_info, null);
        ImageView img = view.findViewById(R.id.img_tab);
        TextView tvStep = view.findViewById(R.id.tv_step);
        TextView tvStatus = view.findViewById(R.id.tv_status);
        if (color == 0) {
            img.setImageResource(R.drawable.garden_green);
            tvStep.setTextColor(getResources().getColor(R.color.green));
            tvStatus.setTextColor(getResources().getColor(R.color.green));
        } else if (color == 1) {
            img.setImageResource(R.drawable.garden_yellow);
            tvStep.setTextColor(getResources().getColor(R.color.yellow_text_light));
            tvStatus.setTextColor(getResources().getColor(R.color.yellow_text_light));
        } else {
            img.setImageResource(R.drawable.garden_gray);
            tvStep.setTextColor(getResources().getColor(R.color.black_text_gray));
            tvStatus.setTextColor(getResources().getColor(R.color.black_text_gray));
        }

        tvStep.setText(step);
        tvStatus.setText(state);
        return view;
    }


    @Override
    public void taskStatusSuccess(BaseMessage baseMessage) {
        setLoaddingText("完成");
//                            onGetNetImageUrl(data);
        dismissLoaddingDialog();
        G.showToast("提交成功，等待审核!");
//        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.ONFRESH_TRYMAKEMANY_PAGE));
        UMConfigUtils.finishTask();
        finish();
    }

    @Override
    public void taskStatusFaile(BaseMessage baseMessage) {
        updateError("失败");
        G.showToast(baseMessage.getMsg() + "");

    }

    @Override
    public void taskDescSuccess(BaseMessage baseMessage) {
        empty_view.setVisibility(View.GONE);
        taskReleaseEty = JSONObject.parseObject(JSONObject.toJSONString(baseMessage.getData()), TaskReleaseEty.class);
        initWigget();
        isGetTaskDesc = false;
    }

    @Override
    public void taskDescFaile(BaseMessage baseMessage) {
        G.showToast(baseMessage.getMsg() + "");
        isGetTaskDesc = false;
        empty_view.setVisibility(View.VISIBLE);
    }


    @Override
    protected void onNotGetImage(String msg) {
        G.showToast(msg);

    }

    @Override
    protected void onGetImage(String path) {

    }

    @Override
    protected void onSetNetImageUrl(String data) {
        setLoaddingText("提交中");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", taskReleaseEty.getAccepted_task_id());
        jsonObject.put("screenshots", data + "");
        jsonObject.put("remark", taskEty.getRemarks() + "");
        jsonObject.put("package_name", taskEty.getPackage_names() + "");
        jsonObject.put("status", 7 + "");
        presenter.taskStatus(jsonObject);
        taskEty.setScreenshots(data);
    }

    @Override
    protected void onGetNetImageUrl(String url) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            if (taskReleaseEty.getAccepted_task_id() > 0) {
                showProgress(true);
                SheepApp.getInstance()
                        .getNetComponent()
                        .getApiService()
                        .giveUpTask(taskReleaseEty.getAccepted_task_id())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.mContext) {
                            @Override
                            public void onError(BaseMessage baseMessage) {
                                hideProgress();
                                G.showToast(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                            }

                            @Override
                            public void onNext(BaseMessage baseMessage) {

                                acceptedTask();
                            }
                        });
            }
        }
    }

    /**
     * 领取任务
     */
    private void acceptedTask() {
        hideProgress();
        showProgress(true);
        JSONObject object = new JSONObject();
        object.put("device_id", DeviceUtil.getDeviceId(activity));
        object.put("release_task_id", Integer.valueOf(taskReleaseEty.getId() + ""));
        SheepApp.getInstance()
                .getNetComponent()
                .getApiService()
                .acceptedTask(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.mContext) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        hideProgress();
                        G.showToast(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        hideProgress();
                        initData();

                        taskReleaseEty.getTask().setRelease_task_id(taskReleaseEty.getId());
                        CommonUtil.getInstance().setTAskEnty(taskReleaseEty.getTask());
//                        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.ONFRESH_TRYMAKEMANY_PAGE));
                    }
                });
    }

    @Download.onPre
    void onPre(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        if (task.getKey().equals(taskEty.getDownload_link())) {
            if (taskEty == null) {
                return;
            }
            btnTaskItem.setText("准备下载中");
        }
    }

    @Download.onTaskStart
    void taskStart(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        if (task.getKey().equals(taskEty.getDownload_link())) {
            btnTaskItem.setText("开始下载中");
        }
    }

    @Download.onTaskResume
    void taskResume(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        if (task.getKey().equals(taskEty.getDownload_link())) {
            btnTaskItem.setText("恢复下载中");
        }
    }

    //在这里处理任务执行中的状态，如进度进度条的刷新
    @Download.onTaskRunning
    protected void running(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        try {
            if (task.getKey().equals(taskEty.getDownload_link())) {
                int p = task.getPercent();    //任务进度百分比
                String speed = task.getConvertSpeed();    //转换单位后的下载速度，单位转换需要在配置文件中打开
                long speed1 = task.getSpeed(); //原始byte长度速度
                btnTaskItem.setText(p + "%");
                System.out.println("Aria p " + p + ", speed = " + speed);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Download.onTaskStop
    void taskStop(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        if (task.getKey().equals(taskEty.getDownload_link())) {
            btnTaskItem.setText(CommonUtil.CONTINUE_DOWNLOAD);
        }
    }

    @Download.onTaskCancel
    void taskCancel(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        if (task.getKey().equals(taskEty.getDownload_link())) {
            btnTaskItem.setText("已经取消");
        }
    }

    @Download.onTaskFail
    void taskFail(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        if (task.getKey().equals(taskEty.getDownload_link())) {
            btnTaskItem.setText(CommonUtil.FAIL_DOWNLOAD);
        }
    }

    @Download.onTaskComplete
    void taskComplete(DownloadTask task) {
        if (taskEty == null) {
            return;
        }
        if (task.getKey().equals(taskEty.getDownload_link())) {
            //在这里处理任务完成的状态
            btnTaskItem.setText(CommonUtil.START_INSTALL);
        }
    }

    private boolean isGetTaskDesc = false;

    @Override
    protected void onResume() {
        super.onResume();
        if (taskId != -1 && !isGetTaskDesc) {
            isGetTaskDesc = true;
            presenter.taskDesc(taskId);
        }
    }
}
