import { _decorator, Component, Node, 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 { PlatformManager } from '../../../Platform/PlatformManager';
import { branchIdType } from '../../../Platform/WeChat/branchIdType';
import { WeChatPlatform } from '../../../Platform/WeChat/WeChatPlatform';
import AutoSyntheticBuffer from '../../buffers/AutoSyntheticBuffer';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { GamePropertys } from '../../models/GamePropertys';
import { UIConst } from '../UIConst';
const { ccclass, property } = _decorator;

@ccclass('AutoSynthesisMediator')
export class AutoSynthesisMediator extends GUIMediator {

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

    private consume:number=20;
    private time:number=300*1000;
    start():void{
        this.consume=GameConfigManager.getGlobalValue("AutoSyntheticDiamondConsume");
        this.time=GameConfigManager.getGlobalValue("autoSyntheticTime")*1000;
    }

    OnShow(data?:any):void{
        PlatformManager.hideBanner();
        super.OnShow(data);
        this.RefreshView();
    }

    OnHide():void{
        PlatformManager.showBanner();
    }

    private RefreshView():void{
        this.diamondLabel.string=this.consume.toString();
    }

    /**
     * 钻石按钮点击
     */
    DiamondButtonClickHandler():void{
        if(GameModel.single.diamond>this.consume){
            if(this.AddBuffer()){
                //扣钱
                GameModel.single.diamond-=this.consume;
                this.CloseButtonClickHandler();
            }
        }else{
            NoticeManager.ShowPrompt("钻石不足");
        }
    }

    /**
     * 视频按钮点击
     */
    VideoButtonClickHandler():void{
        PlatformManager.showRewardedVideo(()=>{
            this.AddBuffer();
            let weChat = PlatformManager.impl as WeChatPlatform;
            if(weChat instanceof WeChatPlatform){
                weChat.branchAnalytics(branchIdType.AdAutoSynthetic)
            }
            this.CloseButtonClickHandler();
        },()=>{
            NoticeManager.ShowPrompt("看视频失败");
        });
    }

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

    private AddBuffer():boolean{
        let buffers:IBuffer[]=BufferManager.GetBufferGroup("AutoSynthesis");
        if(buffers!=null&&buffers.length>0){
            NoticeManager.ShowPrompt("自动合成已开启");
            return false;
        }
        let buffer:AutoSyntheticBuffer=new AutoSyntheticBuffer("AutoSynthesis",this.time);
        BufferManager.RunBuffer(buffer);
        return true;
    }
}
