import {BASEURL} from "./request.js";
import {useUserStore} from "../pinia/UserStore/index.js";

export const showToast = (options) => {
    if (typeof options === 'string') {
        uni.showToast({
            title: options, icon: 'none',
        }).then();
    } else {
        uni.showToast(options).then();
    }
}

export const isWXWeb = () => {
    const userAgent = navigator.userAgent;
    return userAgent.includes('MicroMessenger');
}

export const toPage = async (url) => {
    try {
        await uni.navigateTo({
            url: url,
        })
    } catch (e) {
        await uni.reLaunch({
            url: url,
        })
    }
}

export const clearObject = (obj) => {
    Object.keys(obj).forEach(key => {
        delete obj[key];
    });
};

export const backPage = () => {
    uni.navigateBack().then();
}

export const copy = (context) => {
    // #ifndef APP-PLUS || MP-WEIXIN
    try {
        navigator.clipboard.writeText(context)
            .then(() => {
                showToast('已复制');
            })
            .catch(() => {
                showToast('复制失败');
            });
    } catch (e) {
        showToast('复制失败');
    }
    // #endif
    // #ifdef APP-PLUS || MP-WEIXIN
    uni.setClipboardData({
        data: context, success: () => {
            showToast('已复制');
        }, fail: () => {
            showToast('复制失败');
        }
    })
    // #endif
}

export const download = (urls) => {
    // #ifndef APP-PLUS || MP-WEIXIN
    const promises1 = urls.map(url => new Promise((resolve, reject) => {
        const iframe = document.createElement('iframe');
        iframe.src = url;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
        resolve(true);
    }));
    Promise.all(promises1).then(() => {
        showToast('保存成功');
    }).catch(() => {
        showToast('保存失败');
    });
    // #endif

    // #ifdef APP-PLUS
    const promises2 = urls.map(v => new Promise((resolve, reject) => {
        uni.downloadFile({
            url: v,
            success: () => {
                resolve(true);
            },
            fail: () => {
                reject(false);
            }
        })
    }));
    Promise.all(promises2).then(() => {
        showToast('保存成功');
    }).catch(() => {
        showToast('保存失败');
    });
    // #endif

    // #ifdef MP-WEIXIN
    const promises3 = urls.map(v => new Promise((resolve, reject) => {
        uni.downloadFile({
            url: v,
            success: ({tempFilePath}) => {
                uni.saveImageToPhotosAlbum({
                    filePath: tempFilePath,
                    success: () => {
                        resolve(true);
                    },
                    fail: (err) => {
                        console.log(err);
                        reject(false);
                    }
                })
            },
            fail: () => {
                reject(false);
            }
        })
    }));
    Promise.all(promises3).then(() => {
        showToast('保存成功');
    }).catch(() => {
        showToast('保存失败');
    });
    // #endif
}

export const numberToCharacter = (number) => {
    return ['一', '二', '三', '四', '五'][number];
}

export const uploadFile = ({count}) => {
    const UserStore = useUserStore();
    return new Promise((resolve, reject) => {
        uni.chooseImage({
            count: count,
            success: ({tempFilePaths}) => {
                const file = tempFilePaths[0];
                uni.uploadFile({
                    url: BASEURL + '/upload/upload',
                    filePath: file,
                    name: "file",
                    header: {
                        token: UserStore.token
                    },
                    success: ({data}) => {
                        resolve(JSON.parse(data));
                    },
                    fail: (err) => {
                        showToast(err.errMsg);
                        reject(err);
                    }
                });
            },
            fail: (err) => {
                showToast(err.errMsg);
                reject(err);
            }
        });
    });
}
