import Boy from "./Boy";
import Bot from "./Bot";
import SettlePanel from "../Views/SettlePanel";
import Camera from "./Camera";
import SocketPlayer from "../FrameSync/Game/SocketPlayer";
import GameSystem from "../FrameSync/Game/GameSystem";
import PIFS from "../PIFS";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {
    @property({
        type: cc.Float,
        range: [0, 1, 0.1],
        slide: true,
    })
    botStrength = 1;
    @property({
        type: cc.Label,
    })
    text_countDown:cc.Label = null;    

    
    boys:Boy[] = new Array();
    mainCamera:cc.Node;
    isGameOver:boolean = false;

    socketPlayer: SocketPlayer = null;

    onLoad(){
        window.gm = this;
        
        cc.director.getCollisionManager().enabled = true;
        cc.director.getPhysicsManager().enabled = true;

        window.gameSystem = new GameSystem();
    }

    start(){
        //获取相机，添加相机跟踪脚本
        this.mainCamera = this.node.getChildByName('Main Camera');
        this.mainCamera.addComponent(Camera);
        //添加匹配界面
        cc.find("Canvas").addChild(cc.instantiate(window.rs.pf_matchView));
        //触发联机        
        this.socketPlayer = new SocketPlayer();
    }

    protected onDestroy(): void {
        this.socketPlayer.close();
        window.gm = null;
    }

    protected update(): void {
        this.text_countDown.string = GameManager.timeFormat(window.gameSystem.countDown / 1000);
        this.socketPlayer.uploadFrames();
    }

    createBoys() {
        for (let i = 0; i < 4; i++) {
            let box = window.gc.boxs[i];
            let boyPosition = box.position.add(cc.v2(0,box.height/2+10));
            if (window.gameSystem.playerInfos[i]) {
                this.addBoy(i, boyPosition, false);
            } else {
                this.addBoy(i, boyPosition, window.gameSystem.isBotSupporter());
            }
        }
    }

    /**
     * 时间格式化
     * @param seconds 秒数 
     */
    public static timeFormat(seconds:number):string{
        let minute = Math.floor(seconds/60);
        let second = Math.floor(seconds%60);
        return (minute<10?'0':'')+minute+':'+(second<10?'0':'')+second;
    }

    addBoy(index:number,position:cc.Vec2,isBot:boolean){
        let node = cc.instantiate(window.rs.pf_boy);
        node.setPosition(position);
        let boy = node.addComponent(Boy);
        boy.index = index;
        window.gc.boyGroup.addChild(node);
        if (isBot) {
            console.log("添加机器人");
            node.addComponent(Bot);
        } else {
            console.log("添加boy");
        }
        this.boys.push(boy);
        if (window.gameSystem.masterId === index) window.boy = boy;
    }
    /**游戏结束 */
    gameOver(){
        if (this.isGameOver) return;
        let winBoyIndex = this.boys[0].index;
        this.isGameOver = true;
        this.scheduleOnce(()=>{
            console.log("胜利的人", winBoyIndex);
            SettlePanel.Show(PIFS.matchPlayerInfos[winBoyIndex]);    
        }, window.gameSystem.countDown === 0 ? 0 : 1);
    }

    rankingTag:number = 4;
    removeBoy(boy:Boy){
        boy.rankingNum = this.rankingTag;
        this.rankingTag-=1;
        // console.log("removeBoy------------",boy.index);
        let newBoys = new Array();
        this.boys.forEach((elem)=>{
            if(elem!=boy){
                newBoys.push(elem);
            }
        });
        this.boys = newBoys;
        boy.node.destroy();
        if(this.boys.length==1){
            this.gameOver();
        }
    }
}
