// @ts-ignore
import Cookie from 'js-cookie';
import { getStorage, storageKey } from './storage'

export const appVersionCode = 17;
export const appType = 1;
// @ts-ignore
export const callBackUrl = process.env.NODE_ENV === `production` ? `https://denet.me/close` : process.env.NODE_ENV === `pre` ? `https://pre.denet.me/close` : `https://test.denet.me/close`

// 获取host
export const getEnvConfig = () => {
    let host, logHost

    // @ts-ignore
    switch(process.env.NODE_ENV) {
        case `production`:
            host = `https://api.denetme.net`
            logHost = `https://log.weiqumeta.com`
            break;
        case `pre`:
            host = `https://preapi.denetme.net`
            logHost = `https://prelog.weiqumeta.com`
            break;
        default:
            host = `https://testapi.denetme.net`
            logHost = `https://testlog.weiqumeta.com`
            break;
    }

    return {
        host,
        logHost,
    };
}

// 获取mid
export const getMid = () => {
    let _mid;
    let _cookie_mid_arr = Cookie.get('mid') || []
    if (_cookie_mid_arr.length > 0) {
        _mid = JSON.parse(_cookie_mid_arr)[0].mid
    } else {
        _mid = guid()
        Cookie.set('mid', JSON.stringify([{ mid: _mid }]), { expires: 1000 })
    }
    return _mid;
}

export const getUserInfo = () => {
    let userInfo = getStorage(storageKey.userInfo) || null;
    if (userInfo) {
        return userInfo;
    } else {
        return null
    }
}

// 推特授权url
export const getOauthUrl = (token: string) => {
    return `https://api.twitter.com/oauth/authenticate?oauth_token=${token}`
}

// 创建窗口
export const createWindow = (url: string, w: number = 400, h: number = 600) => {
    var left = Math.round((window.screen.availWidth - w) / 2);
    var top = Math.round((window.screen.availHeight - 100 - h) / 2);
    var win = window.open(url, `newWin`, `width=${w}, height=${h}, top=${top}, left=${left}, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no`);
    return win;
}


// 帮助函数
const guid = () => {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

// 获取cookie
export const getCookie = (name: string) => {
    if (name)  {
        let getVal = Cookie.get(name)
        return getVal
    }
}

// 设置cookie
export const setCookie = (name: string, val: any) => {
    Cookie.set(name, JSON.stringify(val), { expires: 1000 })
}

// 删除cookie
export const removeCookie = (name: string) => {
    Cookie.remove(name)
}

export function debounce(fn: any, delay: number) {
    let timer: number; // 定时器
    return function (...args) {
        let context = this;
        timer && clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    };
}

export function getBrowser() {
    let browser;
    let UserAgent = navigator.userAgent.toLowerCase();
    if (UserAgent.indexOf('chrome') > -1 || UserAgent.indexOf('crios') > -1) {
        browser = `Chrome`
    } else if (UserAgent.indexOf('firefox') > -1) {
        browser = `Firefox`
    } else if (UserAgent.indexOf('opera') > -1) {
        browser = `Opera`
    } else if (UserAgent.indexOf('safari') > -1 && UserAgent.indexOf('chrome') == -1) {
        browser = `Safari`
    } else if (UserAgent.indexOf('edge') > -1) {
        browser = `Edge`
    } else {
        browser = `Other`
    }
    return browser;
}