'use strict';

/**
 * 辅助方法扩展
 *
 * @author CaiAoLin
 * @date 2017/9/28
 * @version
 */
const zeroRange = 0.0000000001;
const fs = require('fs');
const streamToArray = require('stream-to-array');
module.exports = {

    /**
     * 生成随机字符串
     *
     * @param {Number} length - 需要生成字符串的长度
     * @param {Number} type - 1为数字和字符 2为纯数字 3为纯字母
     * @return {String} - 返回生成结果
     */
    generateRandomString(length, type = 1) {
        length = parseInt(length);
        length = isNaN(length) ? 1 : length;
        let randSeed = [];
        let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
            'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

        switch (type) {
            case 1:
                randSeed = stringSeed.concat(numberSeed);
                stringSeed = numberSeed = null;
                break;
            case 2:
                randSeed = numberSeed;
                break;
            case 3:
                randSeed = stringSeed;
                break;
            default:
                break;
        }

        const seedLength = randSeed.length - 1;
        let result = '';
        for (let i = 0; i < length; i++) {
            const index = Math.ceil(Math.random() * seedLength);
            result += randSeed[index];
        }

        return result;
    },

    /**
     * 显示排序符号
     *
     * @param {String} field - 字段名称
     * @return {String} - 返回字段排序的符号
     */
    showSortFlag(field) {
        const sort = this.ctx.sort;
        if (!(sort instanceof Array) || sort.length !== 2) {
            return '';
        }
        sort[1] = sort[1].toUpperCase();
        return (sort[0] === field && sort[1] === 'DESC') ? '' : '-';
    },

    /**
     * 判断是否为ajax请求
     *
     * @param {Object} request - 请求数据
     * @return {boolean} 判断结果
     */
    isAjax(request) {
        let headerInfo = request.headers['x-requested-with'] === undefined ? '' : request.headers['x-requested-with'];
        headerInfo = headerInfo.toLowerCase();
        return headerInfo === 'xmlhttprequest';
    },

    /**
     * 模拟发送请求
     *
     * @param {String} url - 请求地址
     * @param {Object} data - 请求数据
     * @param {String} type - 请求类型(POST) POST | GET
     * @param {String} dataType - 数据类型 json|text
     * @return {Object} - 请求结果
     */
    async sendRequest(url, data, type = 'POST', dataType = 'json') {
        // 发起请求
        const response = await this.ctx.curl(url, {
            method: type,
            data,
            dataType,
        });
        if (response.status !== 200) {
            throw '请求失败';
        }

        return response.data;
    },

    /**
     * 深度验证数据
     *
     * @param {Object} rule - 数据规则
     * @return {void}
     */
    validate(rule) {
        // 先用内置的验证器验证数据
        this.ctx.validate(rule);

        // 然后再验证是否有多余的数据
        const postData = this.ctx.request.body;
        delete postData._csrf;

        const postDataKey = Object.keys(postData);
        const ruleKey = Object.keys(rule);
        // 自动增加字段则填充上，以防判断出错
        if (postData.create_time !== undefined) {
            ruleKey.push('create_time');
        }
        for (const tmp of postDataKey) {
            // 规则里面没有定义则抛出异常
            if (ruleKey.indexOf(tmp) < 0) {
                throw '参数不正确';
            }
        }
    },

    /**
     * 拆分path
     *
     * @param {String|Array} paths - 拆分字符
     * @param {String} symbol - 拆分符号
     * @return {Array} - 拆分结果
     */
    explodePath(paths, symbol = '.') {
        const result = [];
        paths = paths instanceof Array ? paths : [paths];
        for (const path of paths) {
            // 拆分数据
            const pathArray = path.split(symbol);
            // 用户缓存循环的数据
            const tmpArray = [];
            for (const tmp of pathArray) {
                // 每次循环都追加一个数据进去
                tmpArray.push(tmp);
                const tmpPathString = tmpArray.join(symbol);
                // 判断是否已经存在有对应数据
                if (result.indexOf(tmpPathString) >= 0) {
                    continue;
                }
                result.push(tmpPathString);
            }
        }

        return result;
    },

    /**
     * 基于obj, 拷贝sObj中的内容
     * obj = {a: 1, b: 2}, sObj = {a: 0, c: 3}, 返回{a: 0, b: 2, c: 3}
     * @param obj
     * @param sObj
     * @returns {any}
     */
    updateObj(obj, sObj) {
        if (!obj) {
            return JSON.parse(JSON.stringify(sObj));
        }
        const result = JSON.parse(JSON.stringify(obj));
        if (sObj) {
            for (const prop in sObj) {
                result[prop] = sObj[prop];
            }
        }
        return result;
    },

    /**
     * 在数组中查找
     * @param {Array} arr
     * @param name -
     * @param value
     * @returns {*}
     */
    findData(arr, name, value) {
        if (!arr instanceof Array) {
            throw '该方法仅用于数组查找';
        }

        if (arr.length === 0) { return undefined; }
        for (const data of arr) {
            if (data[name] == value) {
                return data;
            }
        }
        return undefined;
    },

    /**
     * 检查数字是否为0
     * @param {Number} value
     * @return {boolean}
     */
    checkZero(value) {
        return value && Math.abs(value) > zeroRange;
    },
    /**
     * 检查数字是否相等
     * @param {Number} value1
     * @param {Number} value2
     * @returns {boolean}
     */
    checkNumberEqual(value1, value2) {
        if (value1 && value2) {
            return Math.abs(value2 - value1) > zeroRange;
        } else {
            return (!value1 && !value2)
        }
    },

    /**
     * 比较编码
     * @param str1
     * @param str2
     * @param symbol
     * @returns {number}
     */
    compareCode(str1, str2, symbol = '-') {
        if (!str1) {
            return -1;
        } else if (!str2) {
            return 1;
        }

        const path1 = str1.split(symbol);
        const path2 = str2.split(symbol);
        for (let i = 0, iLen = Math.min(path1.length, path2.length); i < iLen; i++) {
            if (path1 < path2) {
                return -1;
            } else if (path1 > path2) {
                return 1;
            }
        }
        return path1.length - path2.length;
    },

    /**
     * 树结构节点排序，要求最顶层节点须在同一父节点下
     * @param treeNodes
     * @param idField
     * @param pidField
     */
    sortTreeNodes (treeNodes, idField, pidField) {
        const result = [];
        const getFirstLevel = function (nodes) {
            let result;
            for (const node of nodes) {
                if (!result || result > node.level) {
                    result = node.level;
                }
            }
            return result;
        }
        const getLevelNodes = function (nodes, level) {
            const children = nodes.filter(function (a) {
                return a.level = level;
            });
            children.sort(function (a, b) {
                return a.order - b.order;
            })
            return children;
        }
        const getChildren = function (nodes, node) {
            const children = nodes.filter(function (a) {
                return a[pidField] = node[idField];
            });
            children.sort(function (a, b) {
                return a.order - b.order;
            })
            return children;
        }
        const addSortNodes = function (nodes) {
            for (let i = 0; i< nodes.length; i++) {
                result.push(nodes[i]);
                addSortNodes(getChildren(nodes[i]));
            }
        }

        const firstLevel = getFirstLevel(treeNodes);
        addSortNodes(getLevelNodes(treeNodes, firstLevel));
    },

    /**
     * 判断当前用户是否有指定权限
     *
     * @param {Number|Array} permission - 权限id
     * @return {Boolean} - 返回判断结果
     */
    hasPermission(permission) {
        let result = false;
        try {
            const sessionUser = this.ctx.session.sessionUser;
            if (sessionUser.permission === undefined) {
                throw '不存在权限数据';
            }
            let currentPermission = sessionUser.permission;
            if (currentPermission === '') {
                throw '权限数据为空';
            }
            // 管理员则直接返回结果
            if (currentPermission === 'all') {
                return true;
            }
            currentPermission = currentPermission.split(',');
            permission = permission instanceof Array ? permission : [permission];
            let counter = 0;
            for (const tmp of permission) {
                if (currentPermission[tmp] !== undefined) {
                    counter++;
                }
            }
            result = counter === permission.length;
        } catch (error) {
            result = false;
        }

        return result;
    },

    /**
     * 将文件流的数据保存至本地文件
     * @param stream
     * @param fileName
     * @returns {Promise<void>}
     */
    async saveStreamFile(stream, fileName) {
        // 读取字节流
        const parts = await streamToArray(stream);
        // 转化为buffer
        const buffer = Buffer.concat(parts);
        // 写入文件
        await fs.writeFileSync(fileName, buffer);
    },

    /**
     * 检查code是否是指标模板数据
     * @param {String} code
     * @returns {boolean}
     */
    validBillsCode(code) {
        const reg1 = /(^[0-9]+)([a-z0-9\-]*)/i;
        const reg2 = /([a-z0-9]+$)/i;
        return reg1.test(code) && reg2.test(code);
    },
};
