/**
 * 完工单解析
 */
import extend from '../utils/extend.js';
import analyze from '../utils/analyzeScannedContent.js';

/**
 * 完工送货单
 */
export default class {
    /**
     * 订单
     */
    order = null;

    /**
     * 行集合
     */
    lines = new Map();

    /**
     * 条码集合
     */
    barcodes = new Map();

    /**
     * 订单编码
     */
    orderNumber = '';

    constructor(order) {
        this.order = order;
        this.init();
    }

    /**
     * 初始化
     */
    init(order) {
        this.order = order;
        this.lines.clear();
        this.barcodes.clear();

        if (extend.isNull(order)) {
            return;
        }

        if (extend.isArray(this.order.Lines)) {
            for (let i in this.order.Lines) {
                const key = this.order.Lines[i].InventoryNumber.trim() + this.order.Lines[i].BatchNumber.trim();
                this.lines.set(key, this.order.Lines[i]);

                if (extend.isArray(this.order.Lines[i].Codes)) {
                    for (let j in this.order.Lines[i].Codes) {
                        this.barcodes.set(this.order.Lines[i].Codes[j].Barcode, this.order.Lines[i].Codes[j]);
                    }
                }
            }
        }
    }

    /**
     * 送货单数量
     */
    get quantity() {
        let res = 0;
        if (extend.isObject(this.order) && extend.isArray(this.order.Lines)) {
            for (let i in this.order.Lines) {
                res += this.order.Lines[i].Quantity;
            }
        }
        return res;
    }

    /**
     * 送货扫描确认数量
     */
    get completionConfirmedQty() {
        let res = 0;
        for (let [key, value] of this.barcodes) {
            res += value.CompletionConfirmed > 0 ? 1 : 0;
        }
        return res;
    }

    /**
     * 入库扫描确认数量
     */
    get warehouseConfirmedQty() {
        let res = 0;
        for (let [key, value] of this.barcodes) {
            res += value.WarehouseConfirmed > 0 ? 1 : 0;
        }
        return res;
    }

    /**
     * 获取行
     * @param {string} inventoryNumber 料号
     * @param {string} batchNumber 批次
     */
    getLine(inventoryNumber, batchNumber) {
        let res = null;
        if (extend.isNonEmptyString(inventoryNumber) && extend.isNonEmptyString(batchNumber)) {
            const key = inventoryNumber.trim() + batchNumber.trim();
            res = this.lines.get(key) || null;
        }
        return res;
    }

    /**
     * 设置送货单行条码值集合
     */
    setLineBarcodes() {
        if (extend.isObject(this.order) && extend.isArray(this.order.Lines)) {
            for (let i in this.order.Lines) {
                let rows = [];
                for (let [key, value] of this.barcodes) {
                    if (value.CompletionOrderLineId === this.order.Lines[i].CompletionOrderLineId) {
                        if (!extend.isArray(this.order.Lines[i].Codes)) {
                            this.order.Lines[i].Codes = [];
                        }
                        rows.push(value);
                    }
                }
                this.order.Lines[i].Codes = rows;
            }
        }
    }

    /**
     * 按流水码排序
     * @param {array} list 列表
     * @param {array} list[].SerialNumber 流水码
     * 
     */
    sortBySerialNumber(list) {
        for (let i in list) {
            for (let j in list) {
                if (parseInt(list[i].SerialNumber) < parseInt(list[j].SerialNumber)) {
                    let temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                    temp = null;
                }
            }
        }
        return list;
    }

    /**
     * 获取二维码对象
     */
    getCode(code) {
        if (extend.isObject(this.order) && extend.isArray(this.order.Lines)) {
            for (let i in this.order.Lines) {
                if (extend.isArray(this.order.Lines[i].Codes)) {
                    for (let j in this.order.Lines[i].Codes) {
                        if (this.order.Lines[i].Codes[j].Barcode === code) {
                            return this.order.Lines[i].Codes[j];
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取二维码对象
     */
    getCodes() {
        let list = [];
        if (extend.isObject(this.order) && extend.isArray(this.order.Lines)) {
            for (let i in this.order.Lines) {
                if (extend.isArray(this.order.Lines[i].Codes)) {
                    list = list.concat(this.order.Lines[i].Codes);
                }
            }
        }
        return list;
    }

    /**
     * 获取二维码列表
     */
    getBarcodes() {
        let list = [];
        if (extend.isObject(this.order) && extend.isArray(this.order.Lines)) {
            for (let i in this.order.Lines) {
                if (extend.isArray(this.order.Lines[i].Codes)) {
                    for (let j in this.order.Lines[i].Codes) {
                        if (extend.isNonEmptyString(this.order.Lines[i].Codes[j].Barcode)) {
                            list.push(this.order.Lines[i].Codes[j].Barcode);
                        }
                    }
                }
            }
        }
        return list;
    }

    /**
     * 获取已解析二维码列表
     */
    getBarcodeRows() {
        let rows = [];
        let list = this.getBarcodes();
        for (let i in list) {
            rows.push(analyze.parse(list[i]));
        }
        return this.sortBySerialNumber(rows);
    }

    /**
     * 获取二维码Map
     */
    getBarcodesMap() {
        let map = new Map();
        let list = this.getBarcodes();
        for (let i in list) {
            map.set(list[i], i);
        }
        return map;
    }

    /**
     * 获取行二维码解析对象列表
     * @param {string} inventoryNumber 料号
     * @param {string} batchNumber 批次
     */
    getLineBarcodeRows(inventoryNumber, batchNumber) {
        let rows = [];
        let line = this.getLine(inventoryNumber, batchNumber);
        if (extend.isObject(line) && extend.isArray(line.Codes)) {
            for (let i in line.Codes) {
                let row = analyze.parse(line.Codes[i].Barcode);
                if (extend.isObject(row)) {
                    rows.push(row);
                }
            }
        }
        return this.sortBySerialNumber(rows);
    }

    /**
     * 获取已入库确认二维码列表
     */
    getWarehouseConfirmedBarcodes() {
        let rows = [];
        let codes = this.getCodes();
        for (let i in codes) {
            if (codes[i].WarehouseConfirmed > 0) {
                rows.push(codes[i]);
            }
        }
        return rows;
    }

    /**
     * 获取已入库二维码解析对象列表
     */
    getWarehouseConfirmedBarcodeRows() {
        let rows = [];
        let barcodes = this.getWarehouseConfirmedBarcodes();
        for (let i in barcodes) {
            rows.push(analyze.parse(barcodes[i].Barcode));
        }
        for (let i = 0; i < rows.length; i++) {
            for (let j = 0; j < rows.length; j++) {
                if (rows[i].SerialNumber < rows[j].SerialNumber) {
                    let temp = rows[i];
                    rows[i] = rows[j];
                    rows[j] = temp;
                    temp = null;
                }
            }
        }
        return rows;
    }

    /**
     * 获取已未入库确认二维码列表
     */
    getUnWarehouseConfirmedBarcodes() {
        let rows = [];
        let codes = this.getCodes();
        for (let i in codes) {
            if (codes[i].WarehouseConfirmed === 0) {
                rows.push(codes[i]);
            }
        }
        return rows;
    }

    /**
     * 获取已未入库二维码解析对象列表
     */
    getUnWarehouseConfirmedBarcodeRows() {
        let rows = [];
        let barcodes = this.getUnWarehouseConfirmedBarcodes();
        for (let i in barcodes) {
            rows.push(analyze.parse(barcodes[i].Barcode));
        }
        for (let i = 0; i < rows.length; i++) {
            for (let j = 0; j < rows.length; j++) {
                if (rows[i].SerialNumber < rows[j].SerialNumber) {
                    let temp = rows[i];
                    rows[i] = rows[j];
                    rows[j] = temp;
                    temp = null;
                }
            }
        }
        return rows;
    }

    /**
     * 确认所有二维码
     */
    confirmAllBarcodes() {
        let codes = this.getCodes();
        for(let i in codes) {
            if (!codes[i].WarehouseConfirmed) {
                codes[i].WarehouseConfirmed = 1;
                codes[i].WarehouseConfirmedOn = new Date();
            }
        }
    }
}

class CompletionOrderLineBarcode {
    Barcode = '';
    CompletionConfirmed = 0;
    CompletionConfirmedOn = null;
    CompletionOrderLineId = 0;
    WarehouseConfirmed = 0;
    WarehouseConfirmedOn = null;
}