import DataLevel from "./DataLevel";
import GameManager from "./GameManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MapManager extends cc.Component {

    public static Instance: MapManager = null;

    @property(cc.Node)
    containerLevel: cc.Node = null;

    @property(cc.SpriteFrame)
    wall: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    store: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    trap: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    saw: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    apple: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    headWorm: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    bodyWorm: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    tailWorm: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    end: cc.SpriteFrame = null;


    map: cc.Node[][] = [];

    private listLevelInstance: number[] = [];

    private nodeParentPlatform: cc.Node = null;

    private offsetMap: number = 43;

    protected onLoad(): void {
        MapManager.Instance = this;
    }

    generateMapByLevel(level) {
        let dataLevel: DataLevel = GameManager.Instance.dataLevels[level];
        // const width = dataLevel.width;
        // const height = dataLevel.height;
        const width = 21;
        const height = 12;

        for (let y = 0; y < height; y++) {
            this.map.push([]);
            for (let x = 0; x < width; x++) {
                this.map[y].push(null);
            }
        }

        this.nodeParentPlatform = new cc.Node();
        this.nodeParentPlatform.name = "NodePlatformLevel_" + level;
        this.nodeParentPlatform.position = cc.v3(0, 0);
        this.nodeParentPlatform.parent = this.containerLevel;
        let count = dataLevel.bricks.length;
        for (const brick of dataLevel.bricks) {
            let brickNode: cc.Node = this.instanceNodeById(brick.id);
            brickNode.position = cc.v3(this.offsetMap * brick.x, this.offsetMap * brick.y)
            brickNode.zIndex = count--;
            this.map[brick.y][brick.x] = brickNode;
        }
    }

    instanceNodeById(id: number): cc.Node {
        let brick = new cc.Node();
        brick.name = "brick_" + id;
        brick.parent = this.nodeParentPlatform;
        switch (id) {
            case 1:
                brick.addComponent(cc.Sprite).spriteFrame = this.wall;
                brick.width = 64;
                brick.height = 64;
                break;
            case 2:
                brick.addComponent(cc.Sprite).spriteFrame = this.store;
                brick.width = 64;
                brick.height = 64;
                break;
            case 3:
                brick.addComponent(cc.Sprite).spriteFrame = this.trap;
                brick.width = 64;
                brick.height = 64;
                break;
            case 4:
                brick.addComponent(cc.Sprite).spriteFrame = this.apple;
                brick.width = 64;
                brick.height = 64;
                break;
            case 7:
                brick.addComponent(cc.Sprite).spriteFrame = this.saw;
                brick.width = 64;
                brick.height = 64;
                break;
            case 8:
                brick.addComponent(cc.Sprite).spriteFrame = this.end;
                brick.width = 64;
                brick.height = 64;
                break;
        }
        return brick;
    }
}