
export class designManager {

    private static _instance: designManager;
    private constructor() { };
    public static get instance(): designManager {
        if (!this._instance) {
            this._instance = new designManager();
        }
        return this._instance;
    }

    tb2Arr: object = {};
    tb2Obj: object = {};

    initTable(tableName: string, arr: string[]) {
        // 获取首行提示符
        let tips = arr[0].split(',');
        // 获取第二行字段类型
        let types = arr[1].split(',');
        // 获取第三行字段名字
        let names = arr[2].split(',');

        // 处理剩余数据
        let table: object = {};
        let rowArr = [];
        for (let i = 3, len = arr.length; i < len; i++) {
            let valArr = arr[i].split(/,/g);
            let id = valArr[0];
            if (!id) {
                continue;
            }
            let obj = this.initRow(names, types, valArr);
            table[id] = obj;
            rowArr.push(obj);
        }

        // 加入表格
        this.tb2Obj[tableName] = table;
        this.tb2Arr[tableName] = rowArr;
    }

    initRow(names: string[], types: string[], valArr: string[]) {
        let obj = {};
        for (let i = 0, len = valArr.length; i < len; i++) {
            // 如果字段不存在则跳过
            if (!names[i]) {
                continue;
            }
            // 去除空格
            let key = names[i].replace(/\b\s*/g, "");
            let typeName = types[i].replace(/\b\s*/g, "");
            let value = valArr[i].replace(/\b\s*/g, "");

            // 区分字段类型
            if (typeName == 'string') {
                if (key.length >= 3 && key.substring(key.length - 3, key.length).toLowerCase() == "arr") {
                    let tmpArr: any = value.split("|");
                    tmpArr.forEach((tmpVal, index) => {
                        tmpArr[index] = parseInt(tmpVal);
                    });
                    obj[key] = tmpArr;
                } else {
                    obj[key] = value;
                }

            } else if (typeName == "number") {
                obj[key] = Number(value);
            }
        }

        return obj;
    }

    getTable(tabelName: string) {
        return this.tb2Arr[tabelName];
    }

    getRowById(tableName: string, id: number) {
        return this.tb2Obj[tableName][id];
    }

}

