
//数据表的读取
//按列读取
//按id读取行
class TableReder
{
    constructor(){
        this._tableData = {
            "knifeItems": require("knifeItems"),  //商城物品表
            "ActArray": require("ActArray"), //行为集合表
            "Action": require("Action"), //行为表
            "FruitArray": require("FruitArray"), //水果集合表
            "LevelData": require("LevelData"), //关卡数据表
            "Levels": require("Levels"), //关卡表
            "PosArray": require("PosArray"), //位置集合表
            "Position": require("Position"), //位置表
        }
    }

    getTableRow(tableName,id){
        if(!tableName||!id||!this._tableData[tableName]){
            console.warn("[TableReder]not find row:",tableName,id);
            return null;
        }
        return this._tableData[tableName].rows[id-1];
    }
    /**
     * 根据表名获取表里面数据的长度
     * 
     * 
     * @apiParam  {String} tableName 表名
     * 
     * @apiSuccess (200) {int} 表的长度
     * 
     * 
     * 外部调用
     * Global.TableReader.getTableLength("TestData");
     * 
     */
    getTableLength(tableName){
        if(!tableName||!this._tableData[tableName]){
            console.warn("[TableReder]not find row:",tableName);
            return null;
        } 
        return this._tableData[tableName].rows.length;
    }
    /**
     * 根据表名和id，获取一条数据
     */
    getTableRowById(tableName,id){
        if(!tableName||!id||!this._tableData[tableName]){
            console.warn("[TableReder]not find row:",tableName,id);
            return null;
        }
        return this._tableData[tableName].rows[id-1];
    }
    /**
     * 根据表名获取表里面所有数据
     * 
     * 
     * @apiParam  {String} tableName 表名
     * 
     * @apiSuccess (200) {Array[Object]} 表的所有数据
     * 
     * 
     * 外部调用
     * Global.TableReader.getTable("TestData");
     * 
     */
    getTable(tableName){
        if(!tableName||!this._tableData[tableName]){
            console.warn("[TableReder]not find row:",tableName);
            return null;
        } 
        return this._tableData[tableName].rows
    }

    getTableRowByNum(tableName,num){
        if(!tableName||!this._tableData[tableName]){
            console.warn("[TableReder]not find row:",tableName);
            return null;
        } 
        for (let index = 0; index < this.getTableLength(tableName); index++) {
            let tempRow = this.getTableRow(tableName, index+1);
            if(tempRow.goodsId == num){
                return tempRow;
            }
        }
        return null;
    }
   
    //-----------可以自己根据表的数据结构写一些解析方法，比如下面这些----------------- */
     /**
     * 根据大关数和小关数返回一条关卡信息
     * 
     * @apiParam  {int} levelId 大关数
     * @apiParam  {int} chirldLevel 小关数
     * 
     */
    getLevelDataInfo(levelId, chirldLevel){
        let levelsInfo = this.getTableRowById("Levels", levelId);
        let levelDataInfo = this.getTableRowById("LevelData", levelsInfo.chirldLevel[chirldLevel - 1]);
        return levelDataInfo;
    }


    /**
     * 根据大关数和小关数返回关卡内所有水果位置
     * 
     * @apiParam  {int} levelId 大关数
     * @apiParam  {int} chirldLevel 小关数
     * 
     */
    getPositionArray(levelId, chirldLevel){
        let positionArray = new Array();
        let levelDataInfo = this.getLevelDataInfo(levelId, chirldLevel);
        let posIdArray = this.getTableRowById("PosArray", levelDataInfo.posArrayId).posArray;
        for (let i = 0; i < posIdArray.length; i++) {
            let p = cc.v2();
            let posInfo = this.getTableRowById("Position", posIdArray[i]);
            p.x = posInfo.x;
            p.y = posInfo.y;
            positionArray.push(p);
        }
        return positionArray;
    }


    /**
     * 根据大关数和小关数返回关卡内所有水果
     * 
     * @apiParam  {int} levelId 大关数
     * @apiParam  {int} chirldLevel 小关数
     * 
     */
    getFruitArray(levelId, chirldLevel){
        let levelDataInfo = this.getLevelDataInfo(levelId, chirldLevel);
        let fruitArray = this.getTableRowById("FruitArray", levelDataInfo.fruitArrayId);
        return fruitArray;
    }


    /**
     * 根据大关数和小关数返回关卡内所有动作属性
     * 
     * @apiParam  {int} levelId 大关数
     * @apiParam  {int} chirldLevel 小关数
     * 
     */
    getActArray(levelId, chirldLevel){
        let actArray = new Array();
        let levelDataInfo = this.getLevelDataInfo(levelId, chirldLevel);
        let actIdArray = this.getTableRowById("ActArray", levelDataInfo.actArrayId).actArray;
        for (let i = 0; i < actIdArray.length; i++) {
            let act = new Object();
            let actInfo = this.getTableRowById("Action", actIdArray[i]);
            act.time = actInfo.time;
            act.rotation = actInfo.rotation;
            act.eTime = actInfo.eTime;
            actArray.push(act);
        }
        return actArray;
    }

    getLevelIsScale(levelId, chirldLevel){
        let levelDataInfo = this.getLevelDataInfo(levelId, chirldLevel);
        return this.getTableRowById("PosArray", levelDataInfo.posArrayId).isScale;
    }

}
module.exports = new TableReder();
