package com.unis.sxzcl.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.unis.model.Constant;
import com.unis.sxzcl.util.VersionState;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.SPTool;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.text.DecimalFormat;

/**
 * App 版本更新服务
 * 创建人: mQ
 * 创建时间: 2021/1/8 16:27
 * 1.启动服务后下载文件
 * 2.下载过程中发送EventBus状态通知
 */
public class AppUpdateService extends Service {

    public AppUpdateService() {
    }

    // 下载中
    private boolean isLoading = false;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 未下载时 启动下载
        if (!isLoading)
            startDownload();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 启动下载
     */
    private void startDownload() {

        String path = SPTool.getString(Constant.KEY_APK_PATH, null);
        if (path == null)
            return;
        OkGo.<File>get(path)
                .execute(new FileCallback() {
                    @Override
                    public void onStart(Request<File, ? extends Request> request) {
                        super.onStart(request);
                        isLoading = true;
                    }

                    @Override
                    public void onSuccess(Response<File> response) {
                        LogTool.d("下载完成");
                        VersionState bean = new VersionState();
                        bean.setState(1);
                        bean.setMsg("下载完成");
                        EventBus.getDefault().post(bean);
                    }

                    @Override
                    public void downloadProgress(Progress progress) {
                        super.downloadProgress(progress);
                        VersionState bean = new VersionState();
                        bean.setState(0);
                        bean.setMsg("下载中");
                        String format = new DecimalFormat("###").format(progress.fraction * 100);
                        bean.setProgress(Integer.valueOf(format));
                        EventBus.getDefault().post(bean);
                        LogTool.d("下载中:" + progress.toString());
                    }

                    @Override
                    public void onError(Response<File> response) {
                        super.onError(response);
                        LogTool.d("下载失败");
                        VersionState bean = new VersionState();
                        bean.setState(2);
                        bean.setMsg("下载失败");
                        EventBus.getDefault().post(bean);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        isLoading = false;
                    }
                });

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
