/*
 * @Descripttion: 基础view base
 * @version: 1.0.0
 * @Author: YeeChan
 * @Date: 2020-07-21 11:49:25
 */

import { FMListener, callFM_custom } from "../Interface/FMInterface";

export default abstract class FMViewBase extends cc.Component {

    //是否初始化
    protected _isInit_custom: number = 0;

    protected _listenerCallView_custom: FMListener;

    //各个节点自定义事件
    public abstract EventEnumView_custom : {};


    //------------------------------------------

    protected onLoad() {
        this._initView_custom();
    }

    //初始view的数据 一般如果需要在onLoad前调用该对象中的方法,则手动调用 此方法来实现数据初始化
    public _initView_custom(): boolean {
        if (this._isInit_custom != 0) {
            return;
        }
        this._isInit_custom = 1;
        this.initView_custom();
        this.addEvent_custom();
        this._isInit_custom = 2;
        return true;
    }


    //初始view 的相关数据 子类中不需要实现 onLoad
    protected abstract initView_custom(): void;

    //添加事件 会在initView方法后执行
    protected abstract addEvent_custom(): void;

    //移除事件 会在 removeView 或 销毁的时候执行
    protected abstract removeEvent_custom(): void;

    //设置监听
    public onListenerEventView_custom(listener: FMListener) {
        this._listenerCallView_custom = listener;
    }

    //发送事件
    protected emitListenerEvent_custom(event: string) {
        console.log("发送 " + event)
        if (this._listenerCallView_custom) {
            callFM_custom(this._listenerCallView_custom, event, this);
        }
    }

    /**
     * 移除节点
     */
    public removeView_custom(): void {
        this.onDestroy();
        this.node.destroy();
    }

    /**
     * 隐藏view
     */
    public hideView_custom(): void {
        this.node.active = false;
    }

    /**
     * 显示view
     */
    public showView_custom(): void {
        this.node.active = true;
    }

    /**
     * 判断view 是否激活
     */
    public isActiveView_custom(): boolean {
        if (cc.isValid(this, true) && this.node.activeInHierarchy) { //没有被销毁 && 在场景中是激活的
            return true;
        }
        return false;
    }

    /**
     * 显示banner
     */
    public showBanner_custom() {

    }

    /**
     * 隐藏banner
     */
    public hideBanner_custom() {

    }

    //当该组件被启用,并且它的节点也激活时
    protected onEnable(): void {

    }

    //当该组件被禁用或节点变为无效时调用
    protected onDisable(): void {
        this.hideBanner_custom();
    }

    //当该组件被销毁时调用
    protected onDestroy(): void {
        this.removeEvent_custom();
        this.hideBanner_custom();
    }

}