import {
    OpenLoading,
    CloseLoading
} from "./Loading";
import {
    BASE_URL,
    AIRPORTS
} from "./Constants";
import website from "../config/website.js"
import {
    Base64
} from 'js-base64'
import store from '@/store/';
import {
    baseUrl
} from '@/config/env.js'
import {
    replaceStr
} from "../config";
import overAllConfig from "../config";

let showModal = true
const tabbarPageName = ['/pages/project/project', '/pages/my/my', '/pages/news/news']
/**
 *
 * @export
 * @param {string} url
 * @param {*} [data={}]
 * @param {*} [config={}]
 * @returns {Promise<any>}
 */
export function DoGet(url, data = {}, config = {}) {
    return DoAjax({
            url: url,
            data,
            method: "GET",
            dataType: "json"
        },
        config
    );
}

/**
 *
 * @export
 * @param {string} url
 * @param {*} [data={}]
 * @param {*} [config={}]
 * @returns {Promise<any>}
 */
export function DoPost(url, data = {}, config = {}) {

    return DoAjax({
            url: url,
            data,
            method: "POST",
            dataType: "json"
        },
        config
    );
}

export function request(options) {
    const params = options.params
    if (params) {
        let query = ''
        for (const key in params) {
            if (params[key] !== undefined && params[key] !== null) {
                query += `&${key}=${params[key]}`
            }
        }
        query = '?' + query.substring(1)
        options.url += query
    }
    return DoAjax(options)
}

async function DoAjax(options, config = {}) {
    const func = Promisify(uni.request);

    const header = options.headers || {};
    header["Authorization"] = `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`;

    const token = store.state.user.token;
    if (token) {
        header[website.tokenName] = "bearer " + token
    }
    if (baseUrl) {
        options.url = baseUrl + options.url
    }
    options = Object.assign({}, options, {
        header
    });
    let res = null
    try {
        res = (await func(options));
    } catch (err) {
        console.log({
            err
        })
        res = err;
        res.data = {}
    } finally {}

    // 获取状态码
    const status = res.data.code || res.statusCode;
    const statusWhiteList = website.statusWhiteList || [];
    let message = res.data.msg || res.data.error_description || '未知错误';

    //如果在白名单里则自行catch逻辑处理
    if (statusWhiteList.includes(status)) throw res;
    //如果是401则跳转到登录页面
    if (status === 401) {
        // store.dispatch('FedLogOut').then(() => {
        //     GoLogin()
        // });
        throw new Error(message);
    }
    // 如果请求为非200否者默认统一处理
    if (status !== 200) {
        if (message === 'Bad credentials') {
            message = '账号或密码错误'
        }
        uni.showToast({
            title: message,
            icon: 'none'
        });
        throw new Error(message);
    }
    return transformRepalceString(res);
}

// 递归替换 url
export function transformRepalceString(props) {
    if (typeof props !== 'object' || props === null) {
        for (const key in replaceStr) {
            if (typeof props === 'string') {
                props = props.replace(key, replaceStr[key]);
            }
        }
        return props;
    }
    let result;
    if (props instanceof Array) {
        result = [];
    } else {
        result = {};
    }
    for (let key in props) {
        if (props.hasOwnProperty(key)) {
            result[key] = transformRepalceString(props[key]);
        }
    }
    return result;
}


/**
 * 时间格式化
 * @param {*} str
 * @param {string} fmt
 */
export function DateFormat(str, fmt = "YYYY-MM-DD HH:mm:ss") {
    return str ? moment(str).format(fmt) : "--";
}

/**
 * 富文本添加域名
 * @export
 * @param {string} html
 * @returns
 */
export function HtmlImgAddHost(html) {
    return html.replace(
        / src="\//g,
        ` style="width:100% !important" src="${BASE_URL}/`
    );
}

export function Promisify(func) {
    return function(data) {
        return new Promise((resolve, reject) => {
            func({
                ...data,
                success: resolve,
                fail: reject
            });
        });
    };
}

export function GoLogin() {
    if (showModal) {
        uni.showModal({
            title: "提示",
            content: "您还未登录，点击确认去登录",
            success({
                confirm,
                cancel
            }) {
                if (confirm) {
                    const returnPage = `${overAllConfig.loginURl}?returnPage=${location.href}`
                    location.href = returnPage
                }
                if (cancel) {
                    let pages = getCurrentPages();
                    let prePage = pages[pages.length - 1];
                    const resetPage = tabbarPageName.find((item) => item === prePage.$route.fullPath)
                    if (resetPage) {
                        uni.switchTab({
                            url: '/pages/index/index'
                        })
                    }
                }
            },
            complete() {
                showModal = true
            }

        })
    }
}

/**
 * 获取服务机场
 * @export
 * @param {string[]} list
 */
export function GetServiceIata(...list) {
    const dic = new Set(AIRPORTS);

    for (let i = 0; i < list.length; i++) {
        if (dic.has(list[i])) return list[i];
    }

    return AIRPORTS[0];
}

export async function confirm(options) {
    const func = Promisify(wx.showModal);
    const res = await func(options);
    if (res.confirm !== true) throw new Error(res.cancel);
}