package com.sheep.jiuyan.samllsheep.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadTask;
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.kfzs.duanduan.utils.dlg.HelperUtils;
import com.kfzs.duanduan.view.DialogStorageLow;
import com.sheep.gamegroup.util.LogUtil;

import org.greenrobot.eventbus.EventBus;

/**
 *  @auther realicing
 * 自动更新的Service
 */

public class DownloadService extends Service{

    private DownloadTaskService mDownloadTaskService;
    @Override
    public void onCreate() {
        super.onCreate();
        Aria.download(this).register();
        mDownloadTaskService = new DownloadTaskService(this);
    }

    @Download.onPre void onPre(DownloadTask task) {
        LogUtil.println("DownloadService", "onPre", task.getKey(), task.getTaskName());
    }
    @Download.onTaskStart void taskStart(DownloadTask task) {
        LogUtil.println("DownloadService", "taskStart", task.getKey(), task.getTaskName());
    }
    @Download.onTaskResume void taskResume(DownloadTask task) {
        LogUtil.println("DownloadService", "taskResume", task.getKey(), task.getTaskName());
    }
    //在这里处理任务执行中的状态，如进度进度条的刷新
    @Download.onTaskRunning protected void running(DownloadTask task) {
        LogUtil.println("DownloadService", "running", task.getKey(), task.getTaskName(), task.getPercent());
        mDownloadTaskService.setDownloadTaskPercent(task.getPercent(), task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.GENERATION_ACCOUNT_REFRESH_ADAPTER).setData(task));
    }

    @Download.onTaskStop void taskStop(DownloadTask task) {
        LogUtil.println("DownloadService", "taskStop", task.getKey(), task.getTaskName());
        mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_PAUSE);
        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_STOP).setData(task));
    }

    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
    }

    @Download.onTaskCancel void taskCancel(DownloadTask task) {
        LogUtil.println("DownloadService", "taskCancel", task.getKey(), task.getTaskName());
        mDownloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_CANCEL).setData(task));
    }

    @Download.onTaskFail void taskFail(DownloadTask task) {
        LogUtil.println("DownloadService", "taskFail", task.getKey(), task.getTaskName());
        mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_FAIL);
        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(task));
        //空间不足，弹框提示
        try{
            if (0 <= task.getFileSize() && task.getFileSize() > HelperUtils.getFreeSpaceB()) {
                DialogStorageLow.showDialog(DownloadService.this);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Download.onTaskComplete void taskComplete(DownloadTask task) {
        LogUtil.println("DownloadService", "taskComplete", task.getKey(), task.getTaskName());
        mDownloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
        ApkUtils.installApk(getApplicationContext(), task.getDownloadPath());
        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_COMPLETE).setData(task));
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
