import GUIMediator from "../../../engines/gui/GUIMediator";
import UI_Fight from "../../../ui/Fight/UI_Fight";
import GameModel from "../../models/GameModel";
import GameController from "./GameController";



export default class FightingMediator extends GUIMediator
{
    private sprintTime:number=2000;
    private sprintCDTime:number=3000;
    protected OnShow():void{
        this.RefreshView();
    }

    protected OnHide():void{
        
    }

    private RefreshView():void{
        this.view.m_txtGg.text="第"+GameModel.single.currentLevel+"关";
    }

    protected AddEvents():void{
        this.view.m_btnLeft.on(fgui.Event.TOUCH_BEGIN,this.LeftButtonTouchBegin,this);
        this.view.m_btnLeft.on(fgui.Event.TOUCH_END,this.LeftButtonTouchEnd,this);

        this.view.m_btnRight.on(fgui.Event.TOUCH_BEGIN,this.RightButtonTouchBegin,this);
        this.view.m_btnRight.on(fgui.Event.TOUCH_END,this.RightButtonTouchEnd,this);

        this.view.m_btnTop.onClick(this.TopButtonClick,this);
    }

    protected RemoveEvents():void{
        this.view.m_btnLeft.off(fgui.Event.TOUCH_BEGIN,this.LeftButtonTouchBegin,this);
        this.view.m_btnLeft.off(fgui.Event.TOUCH_END,this.LeftButtonTouchEnd,this);

        this.view.m_btnRight.off(fgui.Event.TOUCH_BEGIN,this.RightButtonTouchBegin,this);
        this.view.m_btnRight.off(fgui.Event.TOUCH_END,this.RightButtonTouchEnd,this);

        this.view.m_btnTop.offClick(this.TopButtonClick,this);
    }

    private LeftButtonTouchBegin():void{
        GameController.single.player.isLeft=true;
    }

    private LeftButtonTouchEnd():void{
        GameController.single.player.isLeft=false;
    }

    private RightButtonTouchBegin():void{
        GameController.single.player.isRight=true;
    }

    private RightButtonTouchEnd():void{
        GameController.single.player.isRight=false;
    }

    private TopButtonClick():void{
        this.sprintTime=GameController.single.carConfig.sprintTime*1000;
        this.sprintCDTime=GameController.single.carConfig.sprintCDTime*1000;

        let currentTime:number=cc.sys.now();
        //CD
        if(currentTime-GameModel.single.lastSprintTime<this.sprintCDTime){
            return;
        }else{
            GameController.single.player.isSprint=true;
            GameModel.single.lastSprintTime=currentTime;
        }
    }

    Tick(dt:number):void{
        if(GameController.single!=null&&GameController.single.player!=null&&GameController.single.player.isSprint==true){
            let currentTime:number=cc.sys.now();
            if(currentTime-GameModel.single.lastSprintTime>this.sprintTime){
                GameController.single.player.isSprint=false;
            }
        }
    }

    get view():UI_Fight{
        return this._view as UI_Fight;
    }
}