import UIManager from "../UI/UIManager";
import DataLevel from "./DataLevel";
import GameManager from "./GameManager";
import MapLevel from "./MapLevel";
import Snake from "./Snake";
import Worm from "./Worm";

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])
    wall1: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    wall2: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    wall3: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    stoneSF: cc.SpriteFrame[] = [];

    @property([cc.SpriteFrame])
    appleSF: cc.SpriteFrame[] = [];

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

    @property(cc.Prefab)
    stone: cc.Prefab = null;

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

    @property(cc.Prefab)
    holeEnd: cc.Prefab = null;

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

    @property(Snake)
    snake: Snake = null;

    mapLevel: MapLevel = null;

    private listLevelInstance: number[] = [];
    private idBackground: number = 1;
    private is18: boolean = true;
    private offsetMap: number = 60;
    private sizeMap: number = 91

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

    generateMapAndPlayerByLevel(level) {
        let listSpWall: cc.SpriteFrame[] = [];
        let spApple: cc.SpriteFrame = null;
        let spStone: cc.SpriteFrame = null;

        this.idBackground = this.getID(level + 1);
        UIManager.Instance.setBackground(this.idBackground);
        if (this.idBackground == 1) {
            listSpWall = this.wall1;
            spApple = this.appleSF[0];
            spStone = this.stoneSF[0];

        } else if (this.idBackground == 2) {
            listSpWall = this.wall2;
            spApple = this.appleSF[1];
            spStone = this.stoneSF[1];
        } else {
            listSpWall = this.wall3;
            spApple = this.appleSF[2];
            spStone = this.stoneSF[2];
        }

        this.generatePlayerByLevel(level);

        if (this.listLevelInstance.includes(level)) {
            const childNodeLevel = this.containerLevel.getChildByName("NodeMapLevel_" + level);
            if (childNodeLevel) {
                this.mapLevel = childNodeLevel.getComponent(MapLevel);
                childNodeLevel.active = true;

            }
        } else {
            let dataLevel: DataLevel = GameManager.Instance.dataLevels[level];
            const width = dataLevel.width;
            const height = dataLevel.height;
            if (width == 18 && height == 10) {
                this.containerLevel.position = cc.v3(-535, -150);
                this.sizeMap = 91;
                this.offsetMap = 60;
                this.is18 = true;
            } else {
                this.containerLevel.position = cc.v3(-530, -150);
                this.sizeMap = 74;
                this.offsetMap = 50;
                this.is18 = false;
            }

            let nodeMapLevel = new cc.Node();
            nodeMapLevel.name = "NodeMapLevel_" + level;
            nodeMapLevel.position = cc.v3(0, 0);
            nodeMapLevel.parent = this.containerLevel;
            nodeMapLevel.addComponent(MapLevel).generateMapByLevel(dataLevel, listSpWall, spStone, spApple, this.trap, this.saw, this.holeEnd, this.apple, this.stone, this.sizeMap, this.offsetMap, this.is18);
            this.listLevelInstance.push(level);
            this.mapLevel = nodeMapLevel.getComponent(MapLevel);
        }
    }

    getID(n: number): number {
        if (n < 1) {
            throw new Error("Số truyền vào phải lớn hơn hoặc bằng 1");
        }
        return Math.floor((n - 1) / 3) + 1;
    }

    generatePlayerByLevel(level: number) {
        let worms: Worm[] = GameManager.Instance.dataLevels[level].worms;
        this.snake.initializeSnakeView(worms, this.offsetMap, this.is18)
    }
    
}

