import router from '@/router';
import { message } from 'ant-design-vue';
import { geti18n } from './i18n';

/**
 * 图片转Base64
 */
function getBase64(file: File): Promise<string | ArrayBuffer | null> {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
    });
}
/**
 * 预览图片
 */
export async function previewCover(file: any): Promise<string> {
    if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
    }
    return file.url || file.preview || '';
}

/**
 * 存储对应key
 * @param key 键值
 * @param value 内容 object自动序列化
 */

export function saveValue(key: string, value: any): boolean{
    if(key == "token"){
        saveValue("time",new Date().getTime())
    }
    if(typeof value == 'object'){
        value = JSON.stringify(value)
    }
    try {
        localStorage.setItem(key, value);
        return true;
    } catch (error) {
        return false;
    }

}

/**
 * 获取对应key
 * @param key 键值
 */
export function getValue(key: string): any{
    let value = localStorage.getItem(key);
    if(value != null){
        try {
            value = JSON.parse(value);
        } catch (error) {
            return value;
        }
    }
    return value;
}

/**
 * 验证图片是否为对应类型
 * @param name 图片名字
 */

export function provenimg(file: any): boolean | void{
    const lan = geti18n()
    const type = ['png', 'jpg'];
    const ntypearr = file.name.split('.');
    const ntype = ntypearr[ntypearr.length - 1];
    console.log(ntype)
    const size = 2 * 1024 * 1024;
    if(file.size > size){
        message.error(lan.$t('zuida') + "2MB")
        return false;
    }
    for(const i in type){
        if(type[i] == ntype){
            return true
        }
    }
    message.error(lan.$t("leixingcuowu"))
    return false;
}

/**
 * 验证视频是否为对应类型
 * @param name 图片名字
 */

export function provenvideo(file: any, isvideo?: boolean): boolean{
    const lan = geti18n()
    const type = ['flv', 'mp4', 'wmv', 'mov', 'avi'];
    const ntypearr = file.name.split('.');
    const ntype = ntypearr[ntypearr.length - 1];
    if(isvideo){
        const size = 500 * 1024 * 1024;
        if(file.size > size){
            message.error(lan.$t('zuida') + "500MB")
            return false;
        }
    }else{
        const size = 100 * 1024 * 1024;
        if(file.size > size){
            message.error(lan.$t('zuida') + "100MB")
            return false;
        }
    }
    for(const i in type){
        if(type[i] == ntype){
            return true;
        }
    }
    message.error(lan.$t("leixingcuowu"))
    return false;
}
export function toindex(){
    function GetUrlRelativePath() {
        const url = document.location.toString();
        const arrUrl = url.split("//");

        const start = arrUrl[1].indexOf("/");
        let relUrl = arrUrl[1].substring(start); //stop省略，截取从start开始到结尾的所有字符

        if (relUrl.indexOf("?") != -1) {
            relUrl = relUrl.split("?")[0];
        }
        return relUrl;
    }
    const url = GetUrlRelativePath()
    console.log(url)
    const list = ['/teacher/wblogin','/teacher/wbloginerr','/teacher/fblogin','/teacher/fbloginerr', '/teacher/wxlogin']
    for(const i in list){
        if(list[i] == url){
            console.log("return")
            return ;
        }
    }
    console.log("跳转")

    router.push("/")
}