import ConfigManager from "../../../engines/configs/ConfigManager";
import { GUIManager } from "../../../engines/gui/GUIManager";
import GUIMediator from "../../../engines/gui/GUIMediator";
import SceneManager from "../../../engines/scenes/SceneManager";
import UI_Egg from "../../../ui/Egg/UI_Egg";
import Main from "../../Main";
import ConfigKeys from "../../models/ConfigKeys";
import GameModel from "../../models/GameModel";
import UIKeys from "../../UIKeys";




export default class EggMediator extends GUIMediator{
    
    private downSpeed:number=0.35;
    private clickSpeed:number=0.15;
    private progress:number=0;

    private __state:number=0;
    private __startTime:number;
    constructor(){
        super();
    }

    protected OnShow():void{
        this.__state=0;
        this.progress=0;
        this.view.m_progressBar.min=0;
        this.view.m_progressBar.max=1;
        if(Main.TEST){
            this.__state=2;
            this.view.m_btnContinue.visible=true;
            this.view.m_btnDD.visible=false;
        }else{
            this.view.m_btnContinue.visible=false;
            this.view.m_btnDD.visible=true;
        }
        this.RandomCar();
    }

    /**
     * 随机一辆车
     */
    private RandomCar():void{
        let list:any[]=ConfigManager.single.GetConfigList(ConfigKeys.Car);
        let index:number=Math.floor(Math.random()*list.length);
        let config:any=list[index];
        GameModel.single.currentCarId=config.id;
        //车的名称
        this.view.m_txtName.text=config.name;
        let attr:string="行驶速度:"+config.speed+"m/s"+"\n";
        attr+="转向速度:"+config.turnSpeed+"m/s"+"\n";
        attr+="冲刺速度:"+config.sprintSpeed+"m/s"+"\n";
        this.view.m_txtAttribute.text=attr;
        //加载图标
        this.view.m_groupCar.url = config.icon;
        // cc.assetManager.loadBundle(config.packageName,(err,bundle:cc.AssetManager.Bundle)=>{
        //     if(err){
        //         cc.log("加载AssetBundle出错："+config.packageName);
        //     }
        //     bundle.load(config.icon,(err,asset:cc.SpriteFrame)=>{
        //         if(err){
        //             cc.log("加载图标出错："+config.icon);
        //         }
        //         this.view.m_groupCar.texture=asset;
        //     })
        // });
    }

    protected OnHide():void{

    }

    protected AddEvents():void{
        this.view.m_btnDD.onClick(this.AddButtonClick,this);
        this.view.m_btnContinue.onClick(this.ContinueButtonClick,this);
    }

    protected RemoveEvents():void{
        this.view.m_btnDD.offClick(this.AddButtonClick,this);
        this.view.m_btnContinue.offClick(this.ContinueButtonClick,this);
    }

    private ContinueButtonClick():void{
        if(this.__state==2){
            this.GotoFighting();
        }
    }

    private AddButtonClick():void{
        if(this.__state==0){
            this.progress+=this.clickSpeed;
        }
    }

    Tick(dt:number):void{
        if(this.__state==0){
            this.progress-=dt*this.downSpeed;
            if(this.progress<0){
                this.progress=0;
            }
            if(this.progress>0.4&&this.progress<0.8){
                let r:number=Math.random()*1;
                if(r>0.5){
                    this.CallBanner(true);
                    this.__state=1;
                    this.__startTime=cc.sys.now();
                }
            }
            this.view.m_progressBar.value=this.progress;
        }else if(this.__state==1){
            this.view.m_progressBar.value=1;
            let curTime:number=cc.sys.now();
            if(curTime-this.__startTime>1000){
                this.view.m_btnContinue.visible=true;
                this.view.m_btnDD.visible=false;
                this.__state=2;
            }
        }
    }

    private CallBanner(isShow:boolean):void{
        if(isShow){
            cc.log("显示Banner");
        }else{
            cc.log("隐藏Banner");
        }
    }

    private GotoFighting():void{
        SceneManager.single.Swicth("mainPackage","Fighting");
        GUIManager.single.Show(UIKeys.Fighting);
        this.HideSelf();
    }
    
    private get view():UI_Egg{
        return this._view as UI_Egg;
    }
}