import { HttpStateType, ReveData } from "../../../../util/CHttp";
import FMap from "../../map/FMap";
import FAltarLight from "./FAltarLight";

const SpineName = {
    IDLE: "idle",
    IDLE2: "idle2",
    CLOSE: "close",
    OPEN: "open"
}

/**
 * 事件机关
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FAltarGear extends cc.Component {

    @property({
        displayName: '对应的地图物件'
    })
    public mapGoodId: string = '27';

    @property({
        type: [cc.Node],
        displayName: '祭坛灯柱'
    })
    public altarLight: Array<cc.Node> = [];

    private map: FMap = null;

    onLoad() {
        this.map = this.node.parent.parent.getComponent(FMap);
    }

    /**
    * 检查灯柱点亮情况
    */
    public check() {
        for (let i = 0; i < this.altarLight.length; i++) {
            const element = this.altarLight[i];
            let altarLight = element.getComponent(FAltarLight)
            if (!altarLight.spine.active) {
                return false;
            }
        }
        this.openGear();
        return true
    }

    private openGear() {
        let ff = this.map.ff;
        ff.pauseSprite(true);
        this.moveCamera(() => {
            this.node.zIndex = -9999;
            let nodes = this.node.children;
            nodes.forEach(element => {
                let spine = element.children[0].getComponent(sp.Skeleton);
                if (spine) {
                    spine.setCompleteListener(() => {
                        element.getComponent(cc.PhysicsBoxCollider).enabled = false;
                    });
                    spine.setAnimation(0, SpineName.OPEN, false);
                }
            });
        });
    }

    private moveCamera(callFunc: Function) {
        let map = this.map;
        let camera = map.mCamera;

        let pos = cc.v2();
        let winsize = cc.winSize;
        pos.x = this.node.x - winsize.width / 2;
        pos.y = this.node.y - winsize.height / 2;


        cc.tween(camera.node).sequence(
            cc.moveTo(1, pos).easing(cc.easeOut(1)),
            cc.callFunc(() => {
                callFunc && callFunc();
            }),
            cc.delayTime(1),
            cc.callFunc(() => {
                map.ff.pauseSprite(false);
            })
        ).start()

    }
}
