import callCheckVersion from "./call-check-version";

// Recommend using in App.vue
const PACKAGE_INFO_KEY = '__package_info__';


export default function () {

    return new Promise((resolve, reject) => {
        callCheckVersion().then(async (uniUpgradeCenterResult) => {
            const code = uniUpgradeCenterResult.code;
            const message = uniUpgradeCenterResult.message;
            const url = uniUpgradeCenterResult.url;

            if (code > 0) {

                if (uniUpgradeCenterResult.is_silently) {
                    uni.downloadFile({
                        url,
                        success: res => {
                            if (res.statusCode == 200) {
                                plus.runtime.install(res.tempFilePath, {
                                    force: false
                                });
                            }
                        }
                    });
                    return;
                }

                uni.setStorageSync(PACKAGE_INFO_KEY, uniUpgradeCenterResult);
                uni.navigateTo({
                    url: `/pages/upgrade/index?local_storage_key=${PACKAGE_INFO_KEY}`,
                    fail: (err) => {
                        console.error('Update dialog navigation failed', err);
                        uni.removeStorageSync(PACKAGE_INFO_KEY);
                    }
                });



                return resolve(uniUpgradeCenterResult);
            } else if (code < 0) {
                return reject(uniUpgradeCenterResult);
            }
            return resolve(uniUpgradeCenterResult);
        }).catch((err) => {
            reject(err);
        });
    });
}

function updateUseModal(packageInfo) {
    const {
        title,
        contents,
        is_mandatory,
        url,
        type,
        platform
    } = packageInfo;

    let isWGT = type === 'wgt';
    let isiOS = !isWGT ? platform.includes('iOS') : false;

    let confirmText = isiOS ? '立即跳转更新' : '立即下载更新';

    return uni.showModal({
        title,
        content: contents,
        showCancel: !is_mandatory,
        confirmText,
        success: res => {
            if (res.cancel) return;

            if (isiOS) {
                plus.runtime.openURL(url);
                return;
            }

            uni.showToast({
                title: '后台下载中……',
                duration: 1000
            });

            uni.downloadFile({
                url,
                success: res => {
                    if (res.statusCode !== 200) {
                        console.error('Download package failed');
                        return;
                    }

                    plus.runtime.install(res.tempFilePath, {
                        force: false
                    }, () => {
                        if (is_mandatory) {
                            plus.runtime.restart();
                            return;
                        }
                        uni.showModal({
                            title: '安装成功是否重启？',
                            success: res => {
                                if (res.confirm) {
                                    plus.runtime.restart();
                                }
                            }
                        });
                    }, err => {
                        uni.showModal({
                            title: '更新失败',
                            content: err.message,
                            showCancel: false
                        });
                    });

                }
            });
        }
    });
}
