import moment from "moment";
import { revertSeconds } from '@/utils/dateUtils'

function timeFormatter(date) {
    if (date === null || date === undefined) {
        return ''
    }
    return moment(date).format('yyyy-MM-DD HH:mm:ss');
}

function dateFormatter(date) {
    if (date === null || date === undefined) {
        return ''
    }
    return moment(date).format('yyyy-MM-DD');
}

function dueFormatter(date) {
    if (date === null || date === undefined) {
        return ''
    }
    return revertSeconds(moment(new Date().getTime()).diff(moment(date), 'seconds'));
}

function taskStatusFormatter(value) {
    switch (value) {
        case -2:
            return '重复入库'
        case -1:
            return '暂存任务'
        case 0:
            return '任务新建，待下发'
        case 1:
            return '任务已下发'
        case 2:
            return '任务开始执行'
        case 8:
            return '拣选中'
        case 9:
            return '盘点中'
        case 100:
            return '任务完成'
        case 998:
            return '任务取消'
        case 999:
            return '任务异常'
        default:
            return '其他状态'
    }
}

// 库位格式化
function locationFormatter(locationId) {
    if (locationId === null || locationId == undefined) {
        return ''
    }
    const locationArray = locationId.split('-')
    if (locationArray.length == 3) {
        return locationArray[0] + '排' + locationArray[1] + '列' + locationArray[2] + '层'
    } else if (locationArray.length == 4) {
        return locationArray[0] + '排' + locationArray[1] + '列' + locationArray[2] + '层' + locationArray[3] + '深度'
    } else {
        return ''
    }
}

function kateTaskStatusFormatter(value) {
    switch (value) {
        case 0:
            return '待下发'
        case 1:
            return '已下发'
        case 2:
            return '执行中'
        case 3:
            return '正在拣货'
        case 5:
            return '任务完成'
        default:
            return '异常状态'
    }
}

function pickingStatusFormatter(value) {
    switch (value) {
        case 0:
            return '待拣货'
        case 1:
            return '正在拣货'
        case 2:
            return '拣货完成'
        default:
            return '未拣货'
    }
}

function vehicleStatusFormatter(value) {
    switch (value) {
        case 1:
            return '入库中'
        case 2:
            return '在库中'
        case 3:
            return '出库中'
        case 4:
            return '移库中'
        default:
            return '异常状态'
    }
}

function locationStatusFormatter(value) {
    switch (value) {
        case 0:
            return '空闲'
        case 1:
            return '占用'
        default:
            return '异常状态'
    }
}

function configTypeFormatter(value) {
    switch (value) {
        case '1':
            return '输入框'
        case '2':
            return '下拉多选'
        case '3':
            return '开关'
        default:
            return '任务类型异常'
    }
}

function sizeFormatter(value) {
    if (typeof value != 'number') {
        return 'error'
    }
    const oneByte = 1
    const oneKB = oneByte * 1024
    const oneMB = oneKB * 1024
    const oneGB = oneMB * 1024
    const oneTB = oneGB * 1024
    const onePB = oneTB * 1024
    const oneEB = onePB * 1024
    const oneZB = oneEB * 1024
    const oneYB = oneZB * 1024
    if (value < oneKB) {
        return value + 'B'
    } else if (value < oneMB) {
        return (value / oneKB).toFixed(2) + 'KB'
    } else if (value < oneGB) {
        return (value / oneMB).toFixed(2) + 'MB'
    } else if (value < oneTB) {
        return (value / oneGB).toFixed(2) + 'GB'
    } else if (value < onePB) {
        return (value / oneTB).toFixed(2) + 'TB'
    } else if (value < oneEB) {
        return (value / onePB).toFixed(2) + 'PB'
    } else if (value < oneZB) {
        return (value / oneEB).toFixed(2) + 'EB'
    } else if (value < oneYB) {
        return (value / oneZB).toFixed(2) + 'ZB'
    } else {
        return (value / oneYB).toFixed(2) + 'YB'
    }
}

function jsonFormatter(value) {
    if (value == null) {
        return ""
    }
    if (typeof value == 'string') {
        try {
            return JSON.stringify(JSON.parse(value), null, 4)
        } catch (e) {
            return "error"
        }
    } else {
        return JSON.stringify(value, null, 4)
    }
}

function replaceEnglishAndNumberIGAI(value) {
    return value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '')
}

function yesOrNoFormatter(value) {
    return value == 1 ? '是' : '否'
}

function converseYesOrNoFormatter(value) {
    return value == 0 ? '是' : '否'
}

export {
    timeFormatter,
    dateFormatter,
    taskStatusFormatter,
    locationFormatter,
    kateTaskStatusFormatter,
    pickingStatusFormatter,
    vehicleStatusFormatter,
    configTypeFormatter,
    dueFormatter,
    locationStatusFormatter,
    sizeFormatter,
    jsonFormatter,
    replaceEnglishAndNumberIGAI,
    yesOrNoFormatter,
    converseYesOrNoFormatter
}