import {File} from "@ionic-native/file";
import {FileOpener} from "@ionic-native/file-opener";
import {Injectable} from "@angular/core";
import {LoadingController} from "ionic-angular";
import {CommonService} from "./common.service";
import {Storage} from "@ionic/storage";

/**
 * 预览文件
 */

@Injectable()
export class FileService {
    constructor(private file: File, public fileOpener: FileOpener,
                private loadingCtrl: LoadingController,
                private commonSer: CommonService,
                private storage: Storage
    ) {
    }

    /**
     * 预览文件并打开文件
     * @param url 文件URL
     */
    viewFile(fileUrl, fileName) {
        const uploadLoading = this.loadingCtrl.create({
            content: '加载中...',
            dismissOnPageChange: true,
            enableBackdropDismiss: true,
        });
        uploadLoading.present();
        const xhr = new XMLHttpRequest();
        const fileType = this.getFileMimeType(fileName);
        const url = encodeURI(fileUrl);
        xhr.open('GET', url);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.responseType = "blob";
        xhr.addEventListener("loadstart", (ev) => {
        })
        xhr.addEventListener("progress", (ev) => {
            let progress = Math.round(100.0 * ev.loaded / ev.total);
            uploadLoading.setContent('加载中...' + progress + '%');
        })
        xhr.addEventListener("load", (ev) => {
            uploadLoading.dismiss();
            const blob = xhr.response;
            if (blob) {
                let path = this.file.dataDirectory;
                this.file.writeFile(path, fileName, blob, {   //写入文件
                    replace: true
                }).then(
                    () => {
                        this.fileOpener.open(path + fileName, fileType).catch((err) => {
                            this.commonSer.alert('打开课件失败！' + err);
                        })
                    }).catch((err) => {
                    this.commonSer.alert("打开课件失败！")
                })
            }
        });

        xhr.addEventListener("loadend", (ev) => {
            // 结束下载事件
            uploadLoading.dismiss();
        });
        xhr.addEventListener("error", (ev) => {
            this.commonSer.alert("打开课件失败！")
            uploadLoading.dismiss();
        });
        xhr.addEventListener("abort", (ev) => {
        });
        xhr.send();
    }

    /**
     * 下载并打开文件
     * @param url 文件URL
     */
    downloadFile(fileUrl, fileName) {
        const uploadLoading = this.loadingCtrl.create({
            content: '下载中...',
            dismissOnPageChange: true,
            enableBackdropDismiss: true,
        });
        uploadLoading.present();
        const xhr = new XMLHttpRequest();
        const fileType = this.getFileMimeType(fileName);
        const url = encodeURI(fileUrl);
        xhr.open('GET', url);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.responseType = "blob";
        xhr.addEventListener("loadstart", (ev) => {
        })
        xhr.addEventListener("progress", (ev) => {
            let progress = Math.round(100.0 * ev.loaded / ev.total);
            uploadLoading.setContent('下载中...' + progress + '%');
        })
        xhr.addEventListener("load", (ev) => {
            uploadLoading.dismiss();
            const blob = xhr.response;
            if (blob) {
                let path = this.file.dataDirectory;
                this.file.writeFile(path, fileName, blob, {   //写入文件
                    replace: true
                }).then(
                    () => {
                        this.commonSer.toast('下载课件成功');
                        this.fileOpener.open(path + fileName, fileType).catch((err) => {
                            this.commonSer.alert('打开文件失败！' + err);
                        })
                    }).catch((err) => {
                    this.commonSer.toast("下载课件成功！")
                })
            }
        });

        xhr.addEventListener("loadend", (ev) => {
            // 结束下载事件
            uploadLoading.dismiss();
        });
        xhr.addEventListener("error", (ev) => {
            alert('下载课件失败！');
            uploadLoading.dismiss();
        });
        xhr.addEventListener("abort", (ev) => {
        });
        xhr.send();
    }

    //获取文件类型
    getFileMimeType(fileName: string): string {
        let fileType = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
        let mimeType: string = '';

        switch (fileType) {
            case 'txt':
                mimeType = 'text/plain';
                break;
            case 'docx':
                mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                break;
            case 'doc':
                mimeType = 'application/msword';
                break;
            case 'pptx':
                mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                break;
            case 'ppt':
                mimeType = 'application/vnd.ms-powerpoint';
                break;
            case 'xlsx':
                mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                break;
            case 'xls':
                mimeType = 'application/vnd.ms-excel';
                break;
            case 'zip':
                mimeType = 'application/x-zip-compressed';
                break;
            case 'rar':
                mimeType = 'application/octet-stream';
                break;
            case 'pdf':
                mimeType = 'application/pdf';
                break;
            case 'jpg':
                mimeType = 'image/jpeg';
                break;
            case 'png':
                mimeType = 'image/png';
                break;
            default:
                mimeType = 'application/' + fileType;
                break;
        }
        return mimeType;
    }
}
