import { GlobalParam } from "../Net/GlobalParam";
import {GameTool} from "../Util/GameTool";
import { NetInfoManager } from "./NetInfoManager";
import { PlayerManager } from "./PlayerManager";

import Sprite = Laya.Sprite;
	import Stage = Laya.Stage;
	import Texture = Laya.Texture;
	import Browser = Laya.Browser;
	import Handler = Laya.Handler;
    import WebGL = Laya.WebGL;
    
export class LevelController {
    private static instance: LevelController;
    public static get Instance(): LevelController {
        return LevelController.instance || (LevelController.instance = new LevelController());
    }

    private curLevelData: any;
    public _level: Level;
/******************************************************* */
    private showApe(): void {
        // 方法1：使用loadImage
        var ape: Sprite = new Sprite();
        Laya.stage.addChild(ape);
        ape.loadImage("res/apes/monkey3.png");

        // 方法2：使用drawTexture
        Laya.loader.load("res/apes/monkey2.png", Handler.create(this, function(): void {
            var t: Texture = Laya.loader.getRes("res/apes/monkey2.png");
            var ape: Sprite = new Sprite();
            ape.graphics.drawTexture(t, 0, 0);
            Laya.stage.addChild(ape);
            ape.pos(200, 0);
        }));
    }
/******************************************************* */

    public SetLevel(level?) {
        let len = NetInfoManager.Instance._level.length;
        let index =PlayerManager.Instance.stage;//TODO
        if (PlayerManager.Instance.stage > len) index = GameTool.randomInt(1, len);
        this.curLevelData = NetInfoManager.Instance.GetLevel(index);
        if (!this._level) this._level = new Level();
        this._level.SetLevel(this.curLevelData);
    }
}

export class Level {
    public road_list = [];
    public gold: number;
    public fail_gold: number = 0;
    public enemy: number;
    enemy_hp: number;
    isgold: boolean;
    enemy_cloth:number;
    constructor() {
    }
    
    /******************************************** */
    private apesCtn: Sprite;
    private createApes(): void {
        // 每只猩猩距离中心点150像素
        var layoutRadius: number = 150;
        var radianUnit: number = Math.PI / 2;

        this.apesCtn = new Sprite();
        Laya.stage.addChild(this.apesCtn);

        // 添加4张猩猩图片
        for (var i: number = 0; i < 4; i++) {
            var ape: Sprite = new Sprite();
            ape.loadImage("res/apes/monkey" + i + ".png");

            ape.pivot(55, 72);

            // 以圆周排列猩猩
            ape.pos(
                Math.cos(radianUnit * i) * layoutRadius,
                Math.sin(radianUnit * i) * layoutRadius);

            this.apesCtn.addChild(ape);
        }
        this.apesCtn.pos(Laya.stage.width / 2, Laya.stage.height / 2);
    }
    /******************************************** */

    SetLevel(data) {
        this.road_list = GameTool.SpliteString(data["road_list"]);
        this.gold = Number(data["gold"]);
        this.fail_gold = Number(data["fail_gold"]);
        this.enemy = data["enemy"];
        this.enemy_hp = data["enemy_hp"];
        this.isgold = Number(data["isgold"]) == 1;
        this.enemy_cloth=data["enemy_cloth"]?Number(data["enemy_cloth"]):1;
    }

}
