/// #pkgName FGUI包名
/// #prefabName ui预制名字
/// #viewName uiview名字
/// #UIName ui的名字，prefabName首字母转大写
/// ui逻辑处理类
/// created by chenwb 2024

import { gameMethod } from "../../../../common/gameMethod";
import { FguiEvent } from "../../../../data/const/EventConst";
import FguiMgr from "../../../../frameWork/fgui/FguiMgr";
import { FguiViewCtrl } from "../../../../frameWork/fgui/mvc/FguiViewCtrl";
import EventMng from "../../../../manager/EventMng";
import {ShowTipsItem} from "./ShowTipsItem";
import { ShowTipsVM } from "./ShowTipsVM";
import { ShowTipsView } from "./ShowTipsView";

export class ShowTipsCtrl extends FguiViewCtrl<ShowTipsVM> {
    private delayList:string[];
    private delayShow:number; // 0.2秒内重复触发的,过滤掉

    private showTipsList:ShowTipsItem[];

    OnInited(): void {
        this.delayShow = 0;
        this.delayList = new Array();
        this.showTipsList = new Array();
    }
    OnShow(intent?: any): void {
        this.AddListeners();
        this.delayShow = 0.2;
        this.AddDelayMessage(intent);
        this.ShowTipsUI();
    }
    OnHide(): void {
        this.RemoveListeners();
        for (let index = 0; index < this.showTipsList.length; index++) {
            const element = this.showTipsList[index];
            if (element.IsUsing()){
                element.ResetUI();
            }
        }
    }
    //#region UI事件
    private AddListeners() : void {
        this.initEvent(FguiEvent.SHOWTIPS, this.AddDelayMessage)
    }
    private RemoveListeners() : void {
    }

    private GetShowTipsItem(){
        if (gameMethod.isEmpty(this.showTipsList)){
            return this.CreateShowTipsItem();
        }
        let count = this.showTipsList.length;
        for (let index = 0; index < count; index++) {
            const element = this.showTipsList[index];
            if (!element.IsUsing()){
                return element;
            }
        }
        return this.CreateShowTipsItem();
    }

    private CreateShowTipsItem(){
        let tipsItem = fgui.UIPackage.createObject("Common","TipsItem", ShowTipsItem) as ShowTipsItem;;
        this.showTipsList.push(tipsItem);
        if (gameMethod.isEmpty(this.VM.Panel) || gameMethod.isEmpty(tipsItem)) {
            //还未初始化完成
            return null
        }
        this.VM.Panel.addChild(tipsItem);
        tipsItem.x = this.VM.Panel.width/2;
        tipsItem.touchable = false;
        return tipsItem;
    }

    private ShowTipsUI(){
        if (gameMethod.isEmpty(this.delayList)){
            return;
        }
        let tipsItem = this.GetShowTipsItem();
        if (gameMethod.isEmpty(tipsItem)) {
            return
        }
        this.delayShow = 0.2;
        tipsItem.setData({tips:this.delayList.pop(), callback:()=>{
            if (this.IsHideUI()){
                this.Close();
            }
        }});
    }

    // 是否关闭界面
    private IsHideUI(){
        if (!gameMethod.isEmpty(this.delayList)){
            return false;
        }
        let count = this.showTipsList.length;
        for (let index = 0; index < count; index++) {
            if (this.showTipsList[index].IsUsing()){
                return false;
            }
        }
        return true;
    }
    
    private AddDelayMessage(tips:string) {
        if (gameMethod.isEmpty(tips)) {
            return
        }
        this.delayShow = 0.2;
        if (this.delayShow > 0) {
            this.delayList.push(tips)
            return
        }
    }

    OnUpdate(dt:number): void {
        if (this.delayShow > 0) {
            this.delayShow -= dt
            if (!gameMethod.isEmpty(this.delayList)) {
                this.ShowTipsUI();
            }
        }
    }


    private Close(): void{
        FguiMgr.Instance.closeUI(ShowTipsView);
    }
    //#endregion
}