export default class Utils {
    static str2bytes1(str) {
        const array = new Uint8Array(str.length)
        for (let i = 0; i < str.length; i++) {
            array[i] = str.charCodeAt(i)
        }
        return array
    }

    static str2bytes2(str, len) {
        const array = new Uint8Array(len)
        if (str) {
            for (let i = 0; i < str.length; i++) {
                array[i] = str.charCodeAt(i)
            }
        }
        return array
    }

    static str2bytes(str, len) {
        if (arguments.length === 1) {
            return Utils.str2bytes1(str)
        } else {
            return Utils.str2bytes2(str, len)
        }
    }

    static bytes2str(bytes) {
        if (bytes) {
            let str = ''
            for (let i = 0; i < bytes.length; i++) {
                if (bytes[i] !== 0) {
                    str += String.fromCharCode(bytes[i])
                } else {
                    break
                }
            }
            return str
        } else {
            return null
        }
    }

    static getMacFromLockId(lockId) {
        let mac = ''
        const start = lockId.indexOf('_')
        const str = lockId.substring(start + 1)
        for (let i = str.length - 1; i >= 0; i -= 2) {
            mac += str.charAt(i - 1)
            mac += str.charAt(i)
            if (i > 1) {
                mac += ':'
            }
        }
        return mac.toLocaleUpperCase()
    }

    static byte2HexStr(byteArray) {
        if (byteArray) {
            var str = ''
            for (var i = 0; i < byteArray.length; i++) {
                var tmp = byteArray[i].toString(16)
                if (tmp.length === 1) {
                    tmp = '0' + tmp
                }
                str += tmp
            }
            return str
        } else {
            return null
        }
    }

    static hexStr2Byte(hexStr) {
        var pos = 0
        var len = hexStr.length
        if (len % 2 !== 0) {
            return null
        }
        len /= 2
        var hexA = []
        for (var i = 0; i < len; i++) {
            var s = hexStr.substr(pos, 2)
            var v = parseInt(s, 16)
            hexA.push(v)
            pos += 2
        }
        return hexA
    }

    static int2Byte(n) {
        const array = new Uint8Array(4)
        array[3] = n & 0xFF
        array[2] = (n >> 8) & 0xFF
        array[1] = (n >> 16) & 0xFF
        array[0] = (n >> 24) & 0xFF
        return array
    }

    static mergeTypedArrays(a, b) {
        if (!b || b.length === 0) return a
        if (!a || a.length === 0) return b
        var c = new a.constructor(a.length + b.length)
        c.set(a)
        c.set(b, a.length)
        return c
    }

    static dateFormat(fmt, time) {
        const date = new Date(time)
        const o = {
            'M+': date.getMonth() + 1, // 月份
            'd+': date.getDate(), // 日
            'h+': date.getHours(), // 小时
            'm+': date.getMinutes(), // 分
            's+': date.getSeconds(), // 秒
            'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
            'S': date.getMilliseconds() // 毫秒
        }
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        for (var k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
            }
        }
        return fmt
    }
}
