import {AppVersion} from "@ionic-native/app-version/ngx";
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { File } from '@ionic-native/file/ngx';
import { Injectable } from '@angular/core';
import {AlertController} from "@ionic/angular";
import { AppService, AppGlobal } from './http.service'
import {CommonService} from "../provide/common.service";

@Injectable()
export class AppUpdateService {

    constructor(private appVersion: AppVersion,
                private fileOpener: FileOpener,
                private commonSer:CommonService,
                private fileTransfer: FileTransfer,
                private fileTransferObject: FileTransferObject,
                private file: File,
                private alertCtrl: AlertController,
                private appService: AppService,

    ) {

    }

    //检查是否需要软件更新
    async detectionUpgrade(apkUrl, allowChoose) {
        if (allowChoose) {
          const alert = await this.alertCtrl.create({
                header: '升级提示',
                subHeader: '发现新版本,是否立即升级？',
                buttons: [ {
                    text: '确定',
                    handler: () => {
                        this.downloadApp(apkUrl);
                    }
                }]
            });
          await alert.present();
        } else {
            this.downloadApp(apkUrl);
        }
    }

    //与服务器版本比较，不是最新的，返回最新的下载地址
    compariVersion() {
        return this.appService.ObserverHttpGet("/wisdomgroup/app/compariVersion", {});
    }

    //下载更新APP
    async downloadApp(apkUrl) {
        let alert = await this.alertCtrl.create({
            header: '下载进度：0%',
            backdropDismiss: false,
            buttons: ['后台下载']
        });
        const xhr = new XMLHttpRequest();
        const url = AppGlobal.domain + apkUrl;
        xhr.open("GET", url);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.responseType = "blob";
        xhr.addEventListener("loadstart", (ev) => {
            // 开始下载事件：下载进度条的显示
           alert.present()
        });
        xhr.addEventListener("progress", (ev) => {
            // 下载中事件：计算下载进度
            let progress = Math.round(100.0 * ev.loaded / ev.total);
            let title = document.getElementsByClassName('alert-title')[0];
            title && (title.innerHTML = '下载进度：' + progress + '%');
        });
        xhr.addEventListener("load", (ev) => {
            alert.dismiss();
            // 下载完成事件：处理下载文件
            const blob = xhr.response;
            const fileName = 'temp.apk';
            if (blob) {
                let path = this.file.externalDataDirectory;
                this.file.writeFile(path, fileName, blob, {
                    replace: true
                }).then(
                    () => {
                        this.fileOpener.open(
                            path + fileName,
                            'application/vnd.android.package-archive'
                        ).catch((err) => {
                            this.commonSer.alert('打开apk失败！'+err);
                        })
                    }).catch((err) => {
                    this.commonSer.alert('失败！');
                })
            }
        });
        xhr.addEventListener("loadend", (ev) => {
            // 结束下载事件
        });
        xhr.addEventListener("error", (ev) => {
            this.commonSer.alert('下载apk失败！');
        });
        xhr.addEventListener("abort", (ev) => {
        });
        xhr.send();
    }
}