package com.example.iot_controlhost.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;
import com.example.iot_controlhost.model.net.CommonResponse;
import com.example.iot_controlhost.model.net.FrpConfig;
import com.example.iot_controlhost.model.net.FrpNewVersion;
import com.example.iot_controlhost.ui.dialog.ConfirmDialog;
import com.example.iot_controlhost.utils.global.HardwareSetting;
import com.example.iot_controlhost.utils.global.RoomSetting;
import com.example.iot_controlhost.utils.network.PublicNetRequest;
import com.example.iot_controlhost.utils.network.Url;
import com.example.iot_controlhost.utils.network.XHttpManager;
import com.example.iot_controlhost.utils.network.XHttpShorthand;
import com.xuexiang.rxutil2.rxjava.RxJavaUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

import es.dmoral.toasty.Toasty;
import io.reactivex.functions.Consumer;

/**
 * App更新工具类
 *
 * @author DuanKaiji
 */
public class FrpUpdateUtil {

    /**
     * 检查是否下载
     *
     * @param mContext   上下文
     * @param needNotice 是否需要提示
     */
    public static void check(Context mContext, boolean needNotice) {
        if (needNotice) {
            DialogUtil.showLoadingDialog(mContext, "正在请求中");
        }
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.sos);
        params.addParameter("HostCode", HardwareSetting.getHostId());
        params.addParameter("VersionNumber", "0");
        params.addParameter("VersionName", "0");
        x.http().get(params, new XHttpShorthand<FrpNewVersion>() {
            @Override
            public void success(FrpNewVersion model) {
                Intent intent = mContext.getPackageManager().getLaunchIntentForPackage("com.bbitcn.bbit_frp2");
                boolean needUpdate = false;
                if (intent == null) {
                    //启动失败，下载新版本
                    needUpdate = true;
                } else {
                    if (StringUtils.isEmpty(model.getData().getUrl())) {
                        if (needNotice) {
                            Toasty.error(mContext, "未获取到下载地址，请联系管理员分配版本").show();
                        }
                    } else {
                        if (Integer.parseInt(model.getData().getVersionNumber()) > HardwareSetting.getFrpVersion()) {
                            needUpdate = true;
                        } else {
                            Toasty.success(mContext, "即将启动远程协助软件").show();
                            MyUtil.relaunchFrp();
                        }
                    }
                }
                if (needUpdate) {
                    if (StringUtils.isEmpty(model.getData().getUrl())) {
                        if (needNotice) {
                            Toasty.error(mContext, "未获取到下载地址，请联系管理员分配版本").show();
                        }
                    } else {
                        if (HardwareSetting.isZCAndroid()) {
                            new ConfirmDialog(mContext, "提示", "未安装最新版远程协助软件\n是否立即下载安装？",
                                    () -> down(mContext, model.getData().getUrl())).show();
                        } else {
                            //直接下载
                            down(mContext, model.getData().getUrl());
                        }
                    }
                }
            }

            @Override
            public void onSuccessError(CommonResponse data) {
                if (needNotice) {
                    Toasty.error(mContext, data.getMessage()).show();
                }
            }

            @Override
            public void error(Throwable e) {
                if (needNotice) {
                    Toasty.error(mContext, "检测失败：" + e.getMessage()).show();
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUtil.hideLoadingDialog();
            }
        });
    }

    public static void down(Context mContext, String url) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            PublicNetRequest.downFile(mContext, url);
        } else {
            Toasty.error(mContext, "SD卡不可用，请插入SD卡").show();
        }
    }

}