import { CHECKDIR, PROPTYPE, SURPRISETASKTYPE } from "../data/Enum";
import GamePlay from "../GamePlay";
import GameConst from "../data/GameConst";
import PlayerConst from "../data/PlayerConst";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameLogic extends cc.Component {

    /**加载头像
     * @param headImgUrl 头像url
     * @param headSpr 头像
     * @param size  尺寸
     */
    public static loadHeadImg(headImgUrl: string, headSpr: cc.Sprite, size: number) {
        mk.console.log("[LoaderUtil] loadHeadImg ----------------------------------- ", headImgUrl);
        mk.loader.loadRemote(`${headImgUrl}+"?aaa=aa.jpg"`, null).then((res) => {
            let tex: cc.Texture2D = res as cc.Texture2D;
            headSpr.spriteFrame = new cc.SpriteFrame(tex);
            headSpr.node.setContentSize(size, size);
        })
    }


    //获取世界坐标------------------------------------------------------------------------------------------------------
    public static getWorldPos(node: cc.Node): cc.Vec2 {
        let originX = node.x;
        let originY = node.y;
        let curNode = node;

        while (curNode.parent) {
            let parent = curNode.parent;
            mk.console.log("GameUtil", curNode.parent.name);
            if (parent.name == 'Canvas') {
                break;
            }
            originX += parent.x;
            originY += parent.y;
            curNode = parent;
        }
        mk.console.log("GameUtil", originX, originY);
        return new cc.Vec2(originX, originY);
    }

    //消除相关算法-------------------------------------------------------------------------------------------------------
    /**检测周围的cellItem(该算法有问题 待修正)
    * 
    */
    public checkAroundCellItem(checkDir: CHECKDIR, index_x: number, index_y: number, type: number, ifTagRemoved: boolean) {
        let x: number = 0;
        let y: number = 0;
        switch (checkDir) {
            case CHECKDIR.Above:
                if (index_y <= 0) {
                    return null;
                }
                x = index_x;
                y = index_y - 1;
                break;
            case CHECKDIR.Below:
                if (index_y >= (GameConst.row_num - 1)) {
                    return null;
                }
                x = index_x;
                y = index_y + 1;
                break;
            case CHECKDIR.Left:
                if (index_x <= 0) {
                    return null;
                }
                x = index_x - 1;
                y = index_y;
                break;
            case CHECKDIR.Right:
                if (index_x >= (GameConst.col_num - 1)) {
                    return null;
                }
                x = index_x + 1;
                y = index_y;
                break;
        }
        let cellItem = GamePlay.Inst.cellItemDic[x][y];
        if (cellItem && cellItem.ifRemoved == false && cellItem.type === type) {
            if (ifTagRemoved) {
                cellItem.ifRemoved = true;
            }
            return new cc.Vec2(x, y);
        }
        else {
            return null;
        }
    }

    /**检测上边的CellItem
     * @param index_x 
     * @param index_y
     * @param type     cellItem类型
     * @param ifTagRemoved 查找到时是否标记移除
     */
    public static checkAboveCellItem(index_x: number, index_y: number, type: number, ifTagRemoved: boolean): cc.Vec2 {

        if (!GamePlay.Inst || !GamePlay.Inst.cellItemDic) {
            return;
        }

        if (index_y <= 0) {
            return null;
        }
        //LogUtil.log("index_x", index_x, index_y);
        //LogUtil.log("index_y - 1",index_y - 1);
        let cellItem = GamePlay.Inst.cellItemDic[index_x][index_y - 1];
        if (cellItem && cellItem.ifRemoved == false && cellItem.type === type) {
            if (ifTagRemoved) {
                cellItem.ifRemoved = true;
            }
            return new cc.Vec2(index_x, index_y - 1);
        }
        else {
            return null;
        }
    }

    /**检测下边的CellItem
     * @param index_x 
     * @param index_y
     * @param type     cellItem类型
     * @param ifTagRemoved 查找到时是否标记移除
     */
    public static checkBelowCellItem(index_x: number, index_y: number, type: number, ifTagRemoved: boolean): cc.Vec2 {

        if (!GamePlay.Inst || !GamePlay.Inst.cellItemDic) {
            return;
        }

        if (index_y >= (GameConst.row_num - 1)) {
            return null;
        }
        let cellItem = GamePlay.Inst.cellItemDic[index_x][index_y + 1];
        if (cellItem && cellItem.ifRemoved == false && cellItem.type === type) {
            if (ifTagRemoved) {
                cellItem.ifRemoved = true;
            }
            return new cc.Vec2(index_x, index_y + 1);
        }
        else {
            return null;
        }
    }

    /**检测左边的CellItem
     * @param index_x 
     * @param index_y
     * @param type     cellItem类型
     * @param ifTagRemoved 查找到时是否标记移除
     */
    public static checkLeftCellItem(index_x: number, index_y: number, type: number, ifTagRemoved: boolean): cc.Vec2 {

        if (!GamePlay.Inst || !GamePlay.Inst.cellItemDic) {
            return;
        }

        if (index_x <= 0) {
            return null;
        }

        let cellItem = GamePlay.Inst.cellItemDic[index_x - 1][index_y];
        if (cellItem && cellItem.ifRemoved == false && cellItem.type === type) {
            if (ifTagRemoved) {
                cellItem.ifRemoved = true;
            }
            return new cc.Vec2(index_x - 1, index_y);
        }
        else {
            return null;
        }
    }

    /**检测右边的CellItem
     * @param index_x 
     * @param index_y
     * @param type
     * @param ifTagRemoved 查找到时是否标记移除
     */
    public static checkRightCellItem(index_x: number, index_y: number, type: number, ifTagRemoved: boolean): cc.Vec2 {

        if (index_x >= (GameConst.col_num - 1)) {
            return null;
        }

        if (!GamePlay.Inst || !GamePlay.Inst.cellItemDic || !GamePlay.Inst.cellItemDic[index_x + 1]) {
            return;
        }

        let cellItem = GamePlay.Inst.cellItemDic[index_x + 1][index_y];

        if (cellItem && cellItem.ifRemoved == false && cellItem.type === type) {
            if (ifTagRemoved) {
                cellItem.ifRemoved = true;
            }
            return new cc.Vec2(index_x + 1, index_y);
        }
        else {
            return null;
        }
    }

    /**获取同类型cellType
     * @param: index_x x索引值
     * @param: index_y y索引值
     * @param: type    类型
     * @param: 查找到之后是否标记移除 默认为 false
     */
    public static getAroundSameType(index_x: number, index_y: number, type: number, ifTagRemoved: boolean = false): cc.Vec2[] {
        return [
            GameLogic.checkAboveCellItem(index_x, index_y, type, ifTagRemoved),
            GameLogic.checkBelowCellItem(index_x, index_y, type, ifTagRemoved),
            GameLogic.checkLeftCellItem(index_x, index_y, type, ifTagRemoved),
            GameLogic.checkRightCellItem(index_x, index_y, type, ifTagRemoved)
        ].filter((vec) => vec != null);
    }

    /**获取清理列表
     * @param index_x x
     * @param index_y y
     * @param type    类型
     */
    public static getCleanedVecList(index_x: number, index_y: number, type: number, ifTagRemoved: boolean = true) {
        let aroundSameTypeItem = this.getAroundSameType(index_x, index_y, type, ifTagRemoved);
        aroundSameTypeItem.forEach((vec) => {
            if (GamePlay.Inst.cleanedVecArr.indexOf(vec) == -1) {
                GamePlay.Inst.cleanedVecArr.push(vec);
                this.getCleanedVecList(vec.x, vec.y, type, ifTagRemoved);
            }
        });
    }

    /**获取能够清理列表
     * @param index_x       x
     * @param index_y       y
     * @param type          类型
     * @param ifTagRemoved: boolean = false 找到之后是否标记移除
     */
    public static getCouldCleanVecList(index_x: number, index_y: number, type: number, ifTagRemoved: boolean = true) {
        let aroundSameTypeItem = this.getAroundSameType(index_x, index_y, type, ifTagRemoved);
        aroundSameTypeItem.forEach((vec) => {
            if (GamePlay.Inst.couldCleanVecArr.indexOf(vec) == -1) {
                GamePlay.Inst.couldCleanVecArr.push(vec);
                this.getCouldCleanVecList(vec.x, vec.y, type, ifTagRemoved);
            }
        });
    }

    /**获取能够移除的CellItem的位置信息(找到符合条件就return)*/
    public static getCouldCleanVec() {
        let keys = Object.keys(GamePlay.Inst.cellItemDic);
        for (var i = 0; i < keys.length; i++) {
            let key = Number(keys[i]);
            let colCellItemArr = GamePlay.Inst.cellItemDic[key];
            if (colCellItemArr) {
                for (var j = 0; j < colCellItemArr.length; j++) {
                    let cellItem = colCellItemArr[j];
                    if (cellItem) {
                        this.getCouldCleanVecList(cellItem.x, cellItem.y, cellItem.type, true)
                        if (GamePlay.Inst.couldCleanVecArr.length > 0) {
                            //重置ifRemoved状态
                            for (var i = 0; i < GamePlay.Inst.couldCleanVecArr.length; i++) {
                                let vec = GamePlay.Inst.couldCleanVecArr[i];

                                if (GamePlay.Inst.cellItemDic[vec.x]) {
                                    let cellItem = GamePlay.Inst.cellItemDic[vec.x][vec.y];
                                    if (cellItem) {
                                        cellItem.ifRemoved = false;
                                    }
                                }
                            }
                            return;
                        }
                    }
                }
            }
        }
    }

    /**获取全部移除的XIndex(只针对已经移除得index)
     * @param cleanXIndexArr[] 移除操作的index数组
     */
    public static getAllCleanXIndex(cleanXIndexArr: number[]): number[] {
        let allCleadXIndexArr: number[] = [];
        //LogUtil.log("Game.Inst.cellItemDic",Game.Inst.cellItemDic);
        for (var i = 0; i < cleanXIndexArr.length; i++) {
            let xIndex = cleanXIndexArr[i];
            let colCellItemArr = GamePlay.Inst.cellItemDic[xIndex];
            let ifAllCleaned: boolean = true;
            for (var j = GameConst.row_num - 1; j >= 0; j--) {
                if (colCellItemArr[j]) {
                    ifAllCleaned = false;

                    break;
                }
                // LogUtil.log("colCellItemArr[j]", j, colCellItemArr[j])
                // LogUtil.log("colCellItemArr[j]", j, colCellItemArr)
            }
            if (ifAllCleaned) {
                allCleadXIndexArr.push(xIndex);
            }
        }
        allCleadXIndexArr.sort();
        return allCleadXIndexArr;
    }

    /**获取剩余的xIndexArr */
    public static getLeftXIndexArr(): number[] {
        let leftXindexArr: number[] = [];
        for (var i = 0; i < GameConst.col_num; i++) {
            let cellItemArr = GamePlay.Inst.cellItemDic[i];
            let leftCellItemArr = cellItemArr.filter((cellItem) => cellItem != null);
            if (leftCellItemArr.length > 0) {
                leftXindexArr.push(i);
            }
        }
        return leftXindexArr;
    }

}
