import { _decorator, Component, Node, ProgressBarComponent, LabelComponent } from 'cc';
import BufferManager from '../../../engines/buffers/BufferManager';
import IBuffer from '../../../engines/buffers/IBuffer';
import { GUIManager } from '../../../engines/gui/GUIManager';
import { GUIMediator } from '../../../engines/gui/GUIMediator';
import { DataModelEventType } from '../../../engines/models/DataModelEventType';
import { NoticeManager } from '../../../engines/notices/NoticeManager';
import StringUtils from '../../../engines/utils/StringUtils';
import { PlatformManager } from '../../../Platform/PlatformManager';
import AccelerateBuffer from '../../buffers/AccelerateBuffer';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { GamePropertys } from '../../models/GamePropertys';
import { UIConst } from '../UIConst';
const { ccclass, property } = _decorator;

@ccclass('AccelerateMediator')
export class AccelerateMediator extends GUIMediator {
    
    @property({
        type:ProgressBarComponent
    })
    progressBar:ProgressBarComponent=null;

    @property({
        type:LabelComponent
    })
    timeLabel:LabelComponent=null;

    @property({
        type:LabelComponent
    })
    diamondCLabel:LabelComponent=null;

    @property({
        type: LabelComponent
    })
    glodLabel: LabelComponent = null;

    @property({
        type: LabelComponent
    })
    glodLabel1: LabelComponent = null;

    @property({
        type: LabelComponent
    })
    diamondLabel: LabelComponent = null;

    /**
     * 最大时间
     */
    private max:number=150*1000*10;
    private time:number=150*1000;
    private consume:number=20;
    start():void{
        this.time=GameConfigManager.getGlobalValue("accelerateTime")*1000;
        this.max=GameConfigManager.getGlobalValue("accelerateMaxTime")*1000;
        this.consume=GameConfigManager.getGlobalValue("accelerateDiamondConsume");
    }

    OnShow(data?:any):void{
        super.OnShow(data);

        this.RefreshGlod();
        this.RefreshDiamond();
        //消耗
        this.diamondCLabel.string=this.consume.toString();

        this.AddEvent();
    }

    OnHide():void{
        this.RemoveEvent();
    }

    private AddEvent():void{
        GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
    }

    private RemoveEvent():void{
        GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
    }

    private GameModelPropertyChanged(key: string): void {
        switch (key) {
            case GamePropertys.gold:
                this.CallNextFrame(this.RefreshGlod.bind(this));
                break;
            case GamePropertys.diamond:
                this.CallNextFrame(this.RefreshDiamond.bind(this));
                break;
        }
    }

    private RefreshGlod(): void {
        if (this.glodLabel != null) {
            this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
        }
        this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
    }

    private RefreshDiamond(): void {
        if (this.diamondLabel != null) {
            this.diamondLabel.string = GameModel.single.diamond.toString();
        }
    }

    DiamondButtonClickHandler():void{
        if(GameModel.single.diamond>this.consume){
            if(this.CurrentBufferTime>=this.max){
                NoticeManager.ShowPrompt("加速时间已满!");
                return;
            }
            if(this.AddBuffer()){
                //扣钱
                GameModel.single.diamond-=this.consume;
                this.CloseButtonClickHandler();
            }
        }else{
            NoticeManager.ShowPrompt("钻石不足");
        }
    }

    /**
     * 视频按钮点击
     */
    VideoButtonClickHandler():void{
        if(this.CurrentBufferTime>=this.max){
            NoticeManager.ShowPrompt("加速时间已满!");
            return;
        }
        PlatformManager.showRewardedVideo(()=>{
            this.AddBuffer();
        },()=>{
            NoticeManager.ShowPrompt("看视频失败");
        });
    }

     /**
     * 关闭按钮点击
     */
    CloseButtonClickHandler():void{
        GUIManager.single.Show(UIConst.PREPARE_UI);
        this.HideSelf();
    }

    private AddBuffer():boolean{
        let buffer:AccelerateBuffer=new AccelerateBuffer("Accelerate",this.time);
        BufferManager.RunBuffer(buffer);
        return true;
    }


    private get CurrentBufferTime():number{
        let buffers:IBuffer[]=BufferManager.GetBufferGroup("Accelerate");
        if(buffers==null||buffers.length==0){
            return 0;
        }
        return buffers[0].GetTimeRemaining();
    }

    update(dt:number):void{
        this.progressBar.progress=this.CurrentBufferTime/this.max;
        this.timeLabel.string=StringUtils.TimeFormatting(this.CurrentBufferTime,":",":",":",":",":","");
        super.update(dt);
    }
}
