import { _decorator, Component, Node, JsonAsset, SpriteComponent, LabelComponent, loader, SpriteFrame } from 'cc';
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 { SceneManager } from '../../../engines/scenes/SceneManager';
import StringUtils from '../../../engines/utils/StringUtils';
import { PlatformManager } from '../../../Platform/PlatformManager';
import { branchIdType } from '../../../Platform/WeChat/branchIdType';
import { WeChatPlatform } from '../../../Platform/WeChat/WeChatPlatform';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { GamePropertys } from '../../models/GamePropertys';
import { UIConst } from '../UIConst';
const { ccclass, property } = _decorator;

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

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

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

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


    OnShow(data?:any):void{
        super.OnShow(data);
        this.AddEvent();
        this.RefreshGlod();
        this.RefreshDiamond();
    }

    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();
        }
    }

    /**
     * 视频按钮点击
     */
    VideoButtonClickHandler():void{
        PlatformManager.showRewardedVideo(()=>{
            GameModel.single.trialFenceId=30301;
            //看视频获得
            let weChat = PlatformManager.impl as WeChatPlatform;
            if(weChat instanceof WeChatPlatform){
                weChat.branchAnalytics(branchIdType.AdGet, String(GameModel.single.currentLevel))
            }
            this.startGame();
        },()=>{
            NoticeManager.ShowPrompt("看视频失败");
        });
    }

    CloseButtonClickHandler():void{
        this.startGame();
    }

    private startGame():void{
         //隐藏
         GUIManager.single.Hide(UIConst.PREPARE_UI);
         this.HideSelf();
         PlatformManager.hideBanner();
         SceneManager.single.Swicth("FightingScene");
         let weChat = PlatformManager.impl as WeChatPlatform
         if(weChat instanceof WeChatPlatform){
            //  weChat.startBranchAnalytics(String(GameModel.single.currentLevel));
            weChat.branchAnalytics(branchIdType.EnterGame, String(GameModel.single.currentLevel))
         }
    }

    start () {
        // Your initialization goes here.
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
