
import { gameMethod } from "../../../common/gameMethod";
import { WindowEvent } from "../../../data/const/EventConst";
import { AudioConst } from "../../../data/const/TypeConst";
import { ViewZOrder } from "../../../data/const/ViewZOrder";
import GameDataCenter from "../../../data/GameDataCenter";
import EventMng from "../../../manager/EventMng";
import { LoadingAssetItem, ResCollector } from "../../compment/ResCollector";
import Context from "../Context";
import FguiMgr from "../FguiMgr";
import { ILateUpdate } from "../Interface/ILateUpdate";
import { IUpdate } from "../Interface/IUpdate";
import { FguiViewModel } from "./FguiViewModel";
import { IUIController } from "./IUIController";

export interface FguiClass<T extends FguiView> {
    new(): T;
    /**
     * fgui的包名
     */
    getPkgName(): string;
    /**
     * fgui包内的UI名字
     */
    getPrefabName(): string;
    /**
     * 类名，用于给UI命名
     */
    getViewName(): string;
}
export enum ViewType {
    None,
    /** 非全屏or半屏 */
    Part,
    /** 全屏 */
    Full,
}

export abstract class FguiView implements IUpdate, ILateUpdate {
    //#region  静态函数
    /**
     * fgui的包名
     */
    protected static pkgName;
    /**
     * fgui包内的UI名字
     */
    protected static prefabName;
    /**
     * 类名，用于给UI命名
     */
    protected static viewName;

    public static getPkgName(): string {
        return this.pkgName;
    }
    public static getPrefabName(): string {
        return this.prefabName;
    }
    public static getViewName(): string {
        return this.viewName;
    }
    /** 页面类型 */
    public viewType: ViewType = ViewType.None;
    /** 全屏时隐藏页面次数 */
    public fullHideCount: number = 0;
    public addFullHideCount(num: number) {
        this.fullHideCount += num;
        if (this.fullHideCount <= 0) {
            this.fullHideCount = 0;
            this.Panel.visible = true;
            this.OnDisplay(true);
        } else {
            this.Panel.visible = false;
            this.OnDisplay(false);
        }
    }
    public zOrder: number;
    public uiPkgName: string;
    public getZOrder(): number {
        return this.zOrder;
    }

    public OnDisplay(visible: boolean) {
        this._uiController.OnDisplay(visible);
    }

    /**
     * 打开界面的动画是否结束
     */
    public isFinishOpenAni: boolean;
    public getOpenAniState(): boolean {
        return this.isFinishOpenAni;
    }
    //#endregion

    private _context: Context;
    get Context(): Context {
        return this._context;
    }
    private _viewModel: FguiViewModel;
    private _uiController: IUIController;
    private _panel: fgui.GComponent;
    public get Panel(): fgui.GComponent {
        return this._panel;
    }

    public get Controller(): IUIController {
        return this._uiController;
    }

    initForward(panel: fgui.GComponent, context: Context): void {
        this._panel = panel;
        this._context = context;
        this._viewModel = this.createVM(panel);

        let controller = this.CreateCtrl();
        controller[Context.CONTEXT_KEY] = this._context;
        controller[FguiViewModel.VIEW_MODEL_KEY] = this._viewModel;
        this._uiController = controller;
    }

    /**
     * 初始化，资源加载完成后回调
    */
    init(): void {
        this._uiController.OnInited();
    }
    /**
     * 显示，被打开时回调
     * @param intent 上下文
     */
    show(intent?: any): void {
        //避免注册没有被注销
        this._uiController.offEvent()
        this._uiController.RemoveTimers();
        this._uiController.RemoveAllClick();
        if (this.zOrder == ViewZOrder.Pop) {
            //默认打开动画、音效
            GameDataCenter.audio.playEffect(AudioConst.effect_open)
            this.isFinishOpenAni = false
            let animNode = this._panel.node
            animNode.anchorX = 0.5
            animNode.anchorY = 0.5
            cc.Tween.stopAllByTarget(animNode)
            animNode.scale = 0.9
            cc.tween(animNode).to(0.1, { scale: 1 })
                .call(() => {
                    this.isFinishOpenAni = true
                }).start()
        } else {
            this.isFinishOpenAni = true
        }
        this._panel.node.active = true;
        this._uiController.ThirdPkgs.forEach(element => {
            FguiMgr.Instance.AddPkgCount(element);
        });

        this._uiController.OnShow(intent);
    }


    /**
     * 隐藏，被关闭时回调
     */
    hide(isDispose?: boolean): void {
        if (this.zOrder == ViewZOrder.Pop) {
            //默认关闭动画、音效
            GameDataCenter.audio.playEffect(AudioConst.effect_open)
        }
        this._uiController.offEvent()
        this._uiController.OnHide(isDispose);
        setTimeout(() => {
            this._uiController.ThirdPkgs.forEach(element => {
                FguiMgr.Instance.DelPkgCount(element);
            });
        }, 1000);
        this._panel.node.active = false;
        this._uiController.RemoveTimers();
        this._uiController.RemoveAllClick();
        EventMng.emit(WindowEvent.CLOSE_UI, this.Panel.node.name + "View");
    }
    /**
     * 释放节点，资源被卸载时回调
     */
    Dispose(): void {
        this._uiController.OnDestroy();
        this._panel = null;
    }

    //#region  接口实现
    OnUpdate(elapseTime: number): void {
        this._uiController.OnUpdate(elapseTime);
    }

    OnLateUpdate(elapseTime: number): void {
        this._uiController.OnLateUpdate(elapseTime);
    }
    //#endregion

    protected abstract createVM(panel: fgui.GComponent): FguiViewModel;
    protected abstract CreateCtrl(): IUIController;


}