/*
 * @Author: zhangzc
 * @Email: zhangzc2@guahao.com
 * @Date: 2019-04-25 14:39:21
 * @Last Modified by: hejk
 * @Last Modified time: 2021-04-23 16:44:51
 * @Description: 组件集成挂载输出
 */
import {
    getUrlSuffix
} from 'utils/util';
import componentImg from '../packages/image-viewer';
import componentPdf from '../packages/pdf-viewer';

// let TargetImg = '';

export default {
    install: (v, options = {}) => {
        /**
         * 图片预览器
         */
        const ImageViewer = v.extend(componentImg);
        const TargetImg = new ImageViewer({
            propsData: Object.assign(options, {
                mode: 'none'
            })
        });
        // 实例化
        TargetImg.vm = TargetImg.$mount(); // 挂载但是并未插入dom，是一个完整的Vue实例
        const dom = TargetImg.vm.$el;
        document.body.appendChild(dom); // 将dom插入body
        /**
         * PDF预览器
         */
        const PdfViewer = v.extend(componentPdf);
        const TargetPdf = new PdfViewer({
            propsData: options
        });
        TargetPdf.vm = TargetPdf.$mount();
        const domPdf = TargetPdf.vm.$el;
        document.body.appendChild(domPdf); // 将dom插入body
        /**
         * 根据类型预览
         * @param {*} type  类型
         * @param {*} url 地址
         */
        function openUrl(url) {
            let type = '';
            let item = url;
            const { fileSuffix, fileUrl } = TargetImg.vm.props;

            if (typeof url === 'string') {
                type = getUrlSuffix(url);
                item = {
                    [fileUrl]: url,
                    [fileSuffix]: type
                };
            } else if (typeof url === 'object' && !Array.isArray(url)) {
                type = url[fileSuffix];
            }
            // 大小写转换
            type = String(type).toLowerCase();
            if (['png', 'jpeg', 'jpg', 'gif'].indexOf(type) > -1) {
                TargetImg.vm.handleImgPreview(item[fileUrl]);
            } else if (type === 'pdf') {
                TargetPdf.vm.handleVisible(item);
            } else if (type) {
                window.open(item[fileUrl]);
            }
            return type;
        }
        /**
         * 并挂载在Vue的prototype上
         * @param {*} cfg 
         */
        v.prototype.$viewer = cfg => {
            let url = cfg;
            if (typeof cfg === 'object') {
                Object.keys(cfg).forEach(key => {
                    if (key === 'value') {
                        url = cfg.value;
                    } else {
                        TargetImg.vm[key] = cfg[key];
                        TargetPdf.vm[key] = cfg[key];
                    }
                });
            }
            // 获得当前类型
            const type = openUrl(url);
            if (!type) {
                // id
                const ids = url.split(','); // 是否多个
                if (ids.length > 1) {
                    TargetImg.vm.handleImgPreview(url);
                } else {
                    // 请求远程数据
                    TargetImg.vm.fetchFile(url).then(res => {
                        if (res) {
                            const { data } = TargetImg.vm.props;
                            openUrl(res[data]);
                        }
                    });
                }
            }
        };
        v.component(componentImg.name, componentImg);
        v.component(componentPdf.name, componentPdf);
    }
};
