/**
 * 解析扫描内容
 */
import extend from '../utils/extend.js';

export default {
    /**
     * 二维码有效性判断
     * @param {string} str 传入字符串
     */
    isBarcode(str) {
        let res = false;
        let code = this.parse(str);
        if (extend.isObject(code)) {
            res = true;
        }
        return res;
    },
    
    /**
     * 获取料号
     * @param {string} str 传入字符串
     */
    getInventoryNumber(str) {
        let res = '';
        if (extend.isNonEmptyString(str)) {
            if (str.length > 21) {
                res = str.substring(1, 20);
            }
        }
        return res.trim();
    },

    /**
     * 获取批次
     * @param {string} str 传入字符串
     */
    getBatchNumber(str) {
        let res = '';
        if (extend.isNonEmptyString(str)) {
            if (str.length > 31) {
                let batch = str.substring(21, 31);
                if (batch.substring(7, 8) === '-') {
                    res = batch.substring(0, 2) + '-' + batch.substring(2, 4) + '-' + batch.substring(4);
                } else {
                    res = batch;
                }
            }
        }
        return res.trim();
    },

    /**
     * 获取流水号
     * @param {string} str 传入字符串
     */
    getSerialNumber(str) {
        let res = '';
        if (extend.isNonEmptyString(str)) {
            if (str.length > 35) {
                res = str.substring(31, 35);
            }
        }
        return res.trim();
    },

    /**
     * 解析字符串成对象
     * @param {string} str 传入字符串
     * @returns {Barcode} 条码对象
     */
    parse(str) {
        let res = null;
        if (extend.isNonEmptyString(str) && str.length > 21 && str.length < 37) {
            res = new Barcode({
                InventoryNumber: this.getInventoryNumber(str),
                BatchNumber: this.getBatchNumber(str),
                SerialNumber: this.getSerialNumber(str)
            });
        }
        return res;
    },

    /**
     * 创建二维码内容
     * @param {string} inventoryNumber 料号
     * @param {string} batchNumber 批次
     * @param {string} serialNumber 流水号
     */
    createBarcodeString(inventoryNumber, batchNumber, serialNumber) {
        let res = '';
        if (extend.isEmptyString(inventoryNumber) || extend.isEmptyString(batchNumber) || extend.isEmptyString(
                serialNumber)) {
            return res;
        }
        if (inventoryNumber.length >= 20) {
            return res;
        }
        res = 'A' + inventoryNumber;
        for (let i = 0; i < 20 - inventoryNumber.length; i++) {
            res += ' ';
        }
        res += (batchNumber + serialNumber + '1');
        return res;
    },

    /**
     * 排序·正序
     * @param {Array<Barcode>} list 列表
     * @returns {Array<Barcode>}
     */
    sortASC(list) {
        if (!extend.isArray(list)) {
            return [];
        }
        for (let i in list) {
            for (let j in list) {
                if (list[i].SerialNumber < list[j].SerialNumber) {
                    let temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }
        }
        return list;
    },

    /**
     * 排序·倒序
     * @param {Array<Barcode>} list 列表
     * @returns {Array<Barcode>}
     */
    sortDESC(list) {
        if (!extend.isArray(list)) {
            return [];
        }
        for (let i in list) {
            for (let j in list) {
                if (list[i].SerialNumber > list[j].SerialNumber) {
                    let temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }
        }
        return list;
    }
}

/**
 * 条码对象
 */
class Barcode {
    /**
     * 料号
     */
    InventoryNumber = '';

    /**
     * 批次
     */
    BatchNumber = '';

    /**
     * 流水码
     */
    SerialNumber = '';

    constructor(data) {
        if (extend.isObject(data)) {
            this.InventoryNumber = data.InventoryNumber || '';
            this.BatchNumber = data.BatchNumber || '';
            this.SerialNumber = data.SerialNumber || '';
        }
    }
}