import { instantiate, JsonAsset, LabelComponent, LayoutComponent, Node, Prefab, ScrollViewComponent, _decorator } from 'cc';
import { GUIManager } from "../../../engines/gui/GUIManager";
import { GUIMediator } from "../../../engines/gui/GUIMediator";
import { DataModelEventType } from "../../../engines/models/DataModelEventType";
import StringUtils from "../../../engines/utils/StringUtils";
import GameConfigManager from "../../models/GameConfigManager";
import { GameModel } from "../../models/GameModel";
import { GamePropertys } from "../../models/GamePropertys";
import { UIConst } from "../UIConst";
import { ExchangeItemRenderScript } from "./ExchangeItemRenderScript";

const { ccclass, property } = _decorator;

@ccclass('ExchangeMediator')
export default class ExchangeMediator extends GUIMediator
{
    @property({
        type: LabelComponent
    })
    glodLabel: LabelComponent = null;

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

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

    @property({
        type:Prefab
    })
    itemRenderPrefab:Prefab=null;

    @property({
        type:ScrollViewComponent
    })
    list:ScrollViewComponent=null;

    @property({
        type:LayoutComponent
    })
    listContent:LayoutComponent=null;

    OnShow(data?:any):void{
        super.OnShow(data);
        this.RefreshList();
        this.RefreshGlod();
        this.RefreshDiamond();
        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();
        }
    }

    /**
     * 关闭界面
     */
    CloseButtonClickHandler():void{
        GUIManager.single.Show(UIConst.PREPARE_UI);
        this.HideSelf();
    }

    private RefreshList():void{
        let jsonAsset:JsonAsset=GameConfigManager.GetConfig("ExchangeGold");
        let config:any=jsonAsset.json;
        let itemView:Node;
        let dataList:any[]=config;
        if(dataList.length!=this.listContent.node.children.length){
            if(dataList.length<this.listContent.node.children.length){
                //删除多余的
                while(dataList.length<this.listContent.node.children.length){
                    this.listContent.node.removeChild(this.listContent.node.children[0]);
                }
            }else{
                //添加缺少的
                while(dataList.length>this.listContent.node.children.length){
                    itemView=instantiate(this.itemRenderPrefab);
                    this.listContent.node.addChild(itemView);
                }
            }
        }
        let itemScript:ExchangeItemRenderScript;
        let count:number=dataList.length;
        for (let index = 0; index < count; index++) {
            itemView=this.listContent.node.children[index];
            itemScript=itemView.getComponent(ExchangeItemRenderScript);
            if(itemScript==null){
                throw new Error("商城列表项未挂载ExchangeItemRenderScript脚本!");
            }
            itemScript.UpdateItemRender(dataList[index]);
        }
        this.list.scrollToTop(0.1);
    }
}