import { _decorator, Component, Node, JsonAsset, find, LabelComponent } 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 StringUtils from '../../../engines/utils/StringUtils';
import { PlatformManager } from '../../../Platform/PlatformManager';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { GamePropertys } from '../../models/GamePropertys';
import { UIConst } from '../UIConst';
import { DayItemScript } from './DayItemScript';
const { ccclass, property } = _decorator;

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

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

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

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

    OnShow(data?:any):void{
        this.data=data;
        this.RefreshView();
        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.signGetRecord:
                this.CallNextFrame(this.RefreshView.bind(this));
                break;
            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();
        }
    }

    private RefreshView():void{
        let jsonAsset:JsonAsset=GameConfigManager.GetConfig("Sign");
        let json:any=jsonAsset.json;
        let config:any[]=json;
        let itemConfig:any;
        
        let itemScript:DayItemScript;
        let itemNode:Node;
        for (let index = 0; index < config.length; index++) {
            itemConfig=config[index];
            itemNode=find("Day"+(index+1),this.dayListNode);
            itemScript=itemNode.getComponent(DayItemScript);
            itemScript.UpdateItemRender(itemConfig);
        }
    }

    GetAward():void{
        if(GameModel.single.GetSignAwardIndex()<0){
            NoticeManager.ShowPrompt("没有可领取的奖励！");
            return;
        }
        PlatformManager.showRewardedVideo(()=>{
            GameModel.single.AutoSignAward();
        },()=>{
            NoticeManager.ShowPrompt("看视频失败");
        });
    }
    
    CloseButtonClickHandler():void{
        GUIManager.single.Show(UIConst.PREPARE_UI);
        this.HideSelf();
    }
}
