package com.wisewoods.eduandroid.view;

import com.azhon.appupdate.config.UpdateConfiguration;
import com.azhon.appupdate.manager.DownloadManager;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.pgyer.pgyersdk.PgyerSDKManager;
import com.wisewoods.base.bean.VersionInfo;
import com.wisewoods.base.utils.$;
import com.wisewoods.eduandroid.R;

public class AppUpdateUtils {
    public static void compareVersion(VersionInfo.DataBean data) {
        compareVersion(data,true);
    }

    public static void compareVersion(VersionInfo.DataBean data, boolean isShowTip) {
        if (data != null) {
            int appVersionCode = AppUtils.getAppVersionCode();
            if (Integer.parseInt(data.appCode) > appVersionCode) {
                getVersion(data);
            } else {
                if (isShowTip){
                    $.toast("已经是最新版本了！");
                }else{
                    PgyerSDKManager.checkSoftwareUpdate(ActivityUtils.getTopActivity());
                }
            }
        } else {
            if (isShowTip) {
                $.toast("正在获取版本信息,请稍后重试");
            }
        }
    }

    private static void getVersion(VersionInfo.DataBean data) {
        DownloadManager manager = DownloadManager.getInstance(ActivityUtils.getTopActivity());
        UpdateConfiguration updateConfiguration = new UpdateConfiguration();
//        updateConfiguration.setDialogButtonTextColor(Color.parseColor("#ff0000"));
        manager.setApkName("new.apk")
                .setApkUrl(data.appAddress)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setConfiguration(updateConfiguration)
                .setApkVersionCode(Integer.parseInt(data.appCode))
                .setApkDescription(data.functionDesc)
                .download();
    }
}
