// 网络遮罩层
import { NetworkEvent } from "../../../data/const/EventConst";
import { ViewZOrder } from "../../../data/const/ViewZOrder";
import EventMng from "../../../manager/EventMng";
import { uiCommon } from "../../../utils/UICommon";

const { ccclass, menu, property } = cc._decorator;

@ccclass
@menu("UI/persistNode/NetMask")
export default class NetMask extends cc.Component {
    @property(cc.Node)
    imgBg: cc.Node = null

    private _waitCount: number = 0
    private life: number = 0
    protected onEnable() {
        cc.game.addPersistRootNode(this.node)
        this.node.zIndex = ViewZOrder.MASK

        this.imgBg.setContentSize(cc.winSize)
        this.node.x = cc.winSize.width / 2
        this.node.y = cc.winSize.height / 2

        this.setUITween()

        EventMng.on(NetworkEvent.WAIT, this.addWaitCount, this)
        EventMng.on(NetworkEvent.WAIT_CLOSE, this.delWait, this)
        uiCommon.onRegisterEvent(this.imgBg, this.onClick, this)
        this.imgBg.active = false
    }

    protected onDisable() {
        EventMng.off(NetworkEvent.WAIT, this.addWaitCount, this)
        EventMng.off(NetworkEvent.WAIT_CLOSE, this.delWait, this)
        uiCommon.unRegisterEvent(this.imgBg)
    }

    setUITween() {
        // this.animNode.scale = 0
        // this.animNode.angle = 360
        // this.tipsMask.width = 0
        // let dotAnim = cc.tween().to(0.3, { y: 22 }).to(0.6, { y: 10 }).to(0.3, { y: 16 })
        // cc.tween(this.animNode).to(0.5, { scale: 1, angle: 0 }).call(() => {
        // 	cc.tween(this.gAnim).by(4, { angle: -360 }).repeatForever().start()
        // 	cc.tween(this.tipsMask).to(0.3, { width: 180 }).call(() => {
        // 		let index: number = 0
        // 		let cd = () => {
        // 			index++
        // 			cc.tween(this[`dot${index}`]).then(dotAnim.clone(this[`dot${index}`])).repeatForever().start()
        // 			if (index == 3) {
        // 				this.unschedule(cd)
        // 			}
        // 		}
        // 		this.schedule(cd, 0.15)
        // 	}).start()
        // }).start()
    }

    addWaitCount(val: number = 1) {
        this.waitCount += val
    }
    delWait() {
        this.waitCount = 0
        this.life = 0
        this.imgBg.active = false
    }

    set waitCount(val: number) {
        this._waitCount = val
        if (this._waitCount <= 0) {
            this._waitCount = 0
        }
        // this.imgBg.active = this._waitCount != 0
    }
    get waitCount() {
        return this._waitCount
    }

    update(dt: number) {
        if (this.waitCount > 0) {
            this.life++
            if (this.imgBg.active == false) {
                this.imgBg.active = this.life > 120
            }
            //遮罩展示2s后自动关闭，避免卡死
            if (this.imgBg.active == true && this.life > 240) {
                this.waitCount--
            }
        } else {
            this.life = 0
            this.imgBg.active = false
        }
    }
    onClick() {
        console.warn("net遮罩层被点击")
    }
}
