import { _decorator, UIOpacity, UITransform, Enum, Widget, Button, BlockInputEvents,Node } from 'cc';
import { ResKeeper } from '../res/ResKeeper';
const { ccclass, executeInEditMode, requireComponent, menu, property } = _decorator;

export enum UIShowType {
    Single,             //单独显示
    Overlap,            //重叠显示
    FullScreen,         //全屏显示
};

@ccclass('BaseView')
@requireComponent([UIOpacity, UITransform])
@menu('私有组件/BaseView')
export class BaseView extends ResKeeper {
    @property({ type: Enum(UIShowType), displayName: "UI显示模式" })
    private ui_type: UIShowType = UIShowType.Single;
    @property({ displayName: "是否铺满全屏", visible: true })
    private _base_view_full = false;
    @property({ displayName: "是否拦截层事件", visible() { return this._base_view_full && !this._base_quick_close } })
    private _base_view_block = false;
    @property({
        displayName: "是否快速关闭", visible() {
            let show = this._base_view_full && !this._base_view_block;
            (!show) && (this._base_quick_close_cache = false);
            return show;
        }
    })
    private _base_quick_close = false;
    @property({ type: Node, displayName: "快速关闭排除节点", visible() { return this._base_quick_close && this._base_view_full && !this._base_view_block } })
    private _base_quick_close_exclude_node: Node[] = [];
    @property({ displayName: "快速关闭时是否摧毁UI", visible() { return this._base_quick_close && this._base_view_full && !this._base_view_block } })
    private _base_quick_close_destroy = true;

    private _$timer_uuid = 0;
    private _view_id = -1;
    private _schedule_map = new Map<number, Function>();
    protected _ui_opacity: UIOpacity = null;
    protected _ui_transform: UITransform = null;
    private _opening = true;

    protected onLoad() {
        this._ui_opacity = this.getComponent(UIOpacity) || this.addComponent(UIOpacity);
        this._ui_transform = this.getComponent(UITransform) || this.addComponent(UITransform);
        this._schedule_map.clear();
        if (this._base_view_full) {
            let widget = this.getComponent(Widget) || this.addComponent(Widget);
            widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true;
            widget.left = widget.right = widget.top = widget.bottom = 0;
            widget.alignMode = Widget.AlignMode.ON_WINDOW_RESIZE;
            widget.enabled = true;
            (this._base_view_block) && (this._base_quick_close = false);
            (this._base_quick_close) && (this._base_view_block = false);
        } else {
            this._base_view_block = false;
            this._base_quick_close = false;
            this._base_quick_close_destroy = true;
        }
        if (this._base_view_block) {
            (!this.getComponent(BlockInputEvents) && !this.getComponent(Button)) && this.addComponent(BlockInputEvents);
        }
        if (this._base_quick_close && this._base_quick_close_exclude_node && this._base_quick_close_exclude_node.length > 0) {
            if (!this.getComponent(BlockInputEvents) && !this.getComponent(Button)) {
                for (let node of this._base_quick_close_exclude_node) {
                    node.addComponent(BlockInputEvents);
                }
            }
            // if ((!this.getComponent(BlockInputEvents) && !this.getComponent(Button))) {
            //     this.node.on(Node.EventType.TOUCH_END, () => {
            //         (!this._opening) && Framework.layer.close(this);
            //     }, this);
            // }
        }
    }

    /**
     * 自定义定时器
     * @param callback 回调
     * @param interval 以秒为单位的时间间隔
     * @param repeat 重复次数
     * @param delay 开始延时
     */
    protected _schedule(callback: Function, interval?: number, repeat?: number, delay?: number) {
        if (callback) {
            let uuid = ++this._$timer_uuid;
            this._schedule_map.set(uuid, callback);
            this.schedule(callback, interval, repeat, delay);
            return uuid;
        }
        return -1;
    }

    /**
     * 释放定时器
     * @param uuid 任务标识
     */
    protected _unschedule(uuid: number) {
        if (uuid > 0) {
            let func = this._schedule_map.get(uuid)
            if (func) {
                this._schedule_map.delete(uuid);
                this.unschedule(func);
            }
        }
    }

    /**
     * 全局定时器
     * @param callback 回调
     * @param delay 开始延时
     */
    protected _scheduleOnce(callback: Function, delay: number = 0) {
        if (callback) {
            let uuid = ++this._$timer_uuid;
            this._schedule_map.set(uuid, callback);
            this.scheduleOnce(() => {
                let func = this._schedule_map.get(uuid);
                func && func();
                this.unschedule(uuid);
            }, Math.max(delay, 0));
            return uuid;
        }
    }

    protected _clearAllTimer() {
        this._schedule_map.forEach((func, key) => {
            this.unschedule(func);
        });
        this._schedule_map.clear();
    }

    get show_type() {
        return this.ui_type;
    }

    set view_id(id: number) {
        this._view_id = id;
    }

    get view_id() {
        return this._view_id;
    }

    /**
     * UI打开后会调用该函数
     * @param args 自定义参数
     */
    onOpen(...args: any[]) { }

    /**
     * UI被销毁前会调用该函数
     */
    onClose() { }

    /**
     * 显示UI
     */
    onShow() {
        this.node.active = true;
        // (this._ui_opacity) && (this._ui_opacity.opacity = 255);
    }

    /**
     * 隐藏UI
     */
    onHide() {
        // (this._ui_opacity) && (this._ui_opacity.opacity = 0);
        this.node.active = false;
    }
}