
import Util from "../../../script/before/util/Util";
import JsbSystem from "../../../script/mk/system/JsbSystem";


/** 最新版本下载 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class DownLoadUI extends cc.Component {
    @property(cc.Toggle)
    skipToggle: cc.Toggle = null

    @property(cc.Label)
    labUpdate: cc.Label = null

    @property(cc.Label)
    labFix: cc.Label = null

    @property(cc.Node)
    btnSure: cc.Node = null

    @property(cc.Node)
    btnCancel: cc.Node = null

    @property(cc.Sprite)
    fileProgress: cc.Sprite = null

    @property(cc.Label)
    labPro: cc.Label = null

    /** 是否显示“不再提醒” */
    showToggle = false

    start() {
        this.setText()
    }

    init(showToggle) {
        this.skipToggle.node.active = showToggle
        this.showToggle = showToggle
        if (!showToggle) {
            this.btnSure.y = -366
        }
    }



    clickDownLoad() {
        mk.audio.playEffect("ef_button_click")
        this.btnSure.active = false
        this.btnCancel.active = false
        this.skipToggle.node.active = false

        this.fileProgress.node.parent.active = true

        let apkUrl = gData.appData.installUrl
        let arr = apkUrl.split('/')
        let apkName = arr[arr.length - 1]

        let installUrl = Util.checkHasApk(apkName)
        //已下载过
        if (installUrl != '') {
            JsbSystem.installApk(installUrl)
        }
        else {
            //未下载过
            JsbSystem.downFile2Local(apkUrl, apkName,
                (locaPath) => {
                    JsbSystem.installApk(locaPath)
                },
                (locaPath) => {
                    let str = '下载失败，请检查网络后重试'
                    // if (cc.director.getScene().name == 'Loader') {
                    //     cc.find('Canvas').getComponent(Loader).PlayTip(str)
                    // }
                    // else {
                    //     EffectNode.instance.PlayTip(str)
                    // }

                    this.fileProgress.node.parent.active = false
                    this.fileProgress.fillRange = 0
                    this.labPro.string = '0%'

                    this.btnSure.active = true
                    this.btnCancel.active = true
                    this.skipToggle.node.active = this.showToggle

                },
                (bytesReceived, totalBytesReceived, totalBytesExpected) => {
                    let pro = totalBytesReceived / totalBytesExpected
                    this.setPro(pro)
                })

        }

    }

    //更新进度
    setPro(pro) {
        this.labPro.string = `${Math.floor(pro * 100)}%`
        this.fileProgress.fillRange = pro
    }

    clickSkip() {
        // mk.audio.playEffect(AUDIO_TYPE.button)
        // if (this.skipToggle.isChecked) {
        //     let installVersion = gData.appData.installVersion
        //     GameM.globalStorage.setStorage('installVersion' + installVersion, 'skip', 1)
        // }

        // UiM.Instance.offPanel(PANEL_NAME.DownLoadUI)

        // GameM.commonData.skipUpdate = true
    }

    /** 设置文本 */
    setText() {
        this.labUpdate.string = gData.appData.updateDes;
        this.labFix.string = gData.appData.fixDes;
    }
}
