/**
 * Created by jiachenpan on 16/11/18.
 */

export function parseTime(time, cFormat) {
    if (arguments.length === 0) {
        return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
        date = time
    } else {
        if (('' + time).length === 10) time = parseInt(time) * 1000
        date = new Date(time)
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
        if (result.length > 0 && value < 10) {
            value = '0' + value
        }
        return value || 0
    })
    return time_str
}

export function formatTime(format) {
    const now = new Date()
    var o = {
        'M+': now.getMonth() + 1, // month
        'd+': now.getDate(), // day
        'h+': now.getHours(), // hour
        'm+': now.getMinutes(), // minute
        's+': now.getSeconds(), // second
        'q+': Math.floor((now.getMonth() + 3) / 3), // quarter
        'S': now.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1,
            (now.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (var k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
        }
    }
    return format
}

// 将list转成tree，使用前注意把array进行深拷贝
export function listToTreeSelect(array, parent, tree) {
    tree = typeof tree !== 'undefined' ? tree : []
    parent = typeof parent !== 'undefined' ? parent : {
        id: null
    }

    var children = array.filter((val) => {
        return val.parentId === parent.id
    })

    if (children.length > 0) {
        if (parent.id === null) {
            tree = children
        } else {
            parent['children'] = children
        }

        children.forEach((val) => {
            listToTreeSelect(array, val)
        })
    }

    return tree
}

// 深拷贝
export const deepClone = (obj) => {
    var objArray = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === "object") {
        for (let key in obj) {
            if (Object.prototype.hasOwnProperty.call(obj, key)) {
                if (obj[key] && typeof obj[key] === "object") {
                    objArray[key] = deepClone(obj[key]);
                } else {
                    objArray[key] = obj[key];
                }
            }
        }
    }
    return objArray;
}

export const generateRandomString = (e = null) => {
    e = e || 32;
    let t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz012345678",
        a = t.length,
        n = "";
    for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
    return n
}
export const diffArrFunc = (arr1, arr2) => {
    let oneArr = arr1;
    let twoArr = arr2;
    let diffArr = []
    for (let i = 0; i < oneArr.length; i++) {
        let flag = true
        for (let j = 0; j < twoArr.length; j++) {
            if (twoArr[j] === oneArr[i]) {
                flag = false
                break
            }
        }
        if (flag) {
            diffArr.push(oneArr[i])
        }
    }
    return diffArr
}

// 判断的是否是JSON字符串
export const isJson=(str)=>{
	if (typeof str == 'string') {
	      try {
	        var obj = JSON.parse(str);
			// 等于这个条件说明就是JSON字符串 会返回true
	        if (typeof obj == 'object' && obj) {
	          return true;
	        } else {
				//不是就返回false
	          return false;
	        }
	      } catch (e) {
	        return false;
	      }
	    }
	    return false;
}
