import FqLogin from "../../../../login/FqLogin";
import { AudioMgr } from "../../../../main/ViewManage";
import FF from "../../FF";
import FMap from "../../map/FMap";
import { GroupType } from "../../object/FObject";
import FSprite from "../../object/FSprite";
import BaseEvent from "../base/BaseEvent";
import FMapDialog from "../dialog/FMapDialog";
import FAltar from "./FAltar";
import FAltarGear from "./FAltarGear";

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

/**
 * 祭坛灯柱
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FAltarLight extends BaseEvent {

    @property({
        displayName: '对应的地图物件'
    })
    public mapGoodId: string = '27';
    @property({
        displayName: '需要的物品id'
    })
    public goodId = 2002;

    @property({
        type: cc.Node,
        displayName: '点亮火焰动画'
    })
    public spine: cc.Node = null;

    @property({
        type: cc.Node,
        displayName: '完成后触发对象'
    })
    public altar: cc.Node = null;

    @property({
        displayName: '靠近的提示',
        type: cc.Sprite
    })
    mIcon: cc.Sprite = null;//靠近后的提示

    @property({
        type: [cc.SpriteFrame],
        displayName: '不同状态的图标'
    })
    mIconFrame: Array<cc.SpriteFrame> = [];

    @property(cc.Prefab)
    mMapDialog: cc.Prefab = null;

    @property({
        displayName: '提示图标',
        type: cc.SpriteFrame
    })
    mTipsIcon: cc.SpriteFrame = null;

    @property({
        displayName: '点亮后的对话',
        type: [cc.String]
    })
    text: Array<string> = [];

    // /**
    //  * 控制的栅栏机关
    //  */
    //  @property({
    //     displayName: '控制的机关',
    //     type: [cc.Node],
    // })
    // mFenceTrigger: Array<cc.Node> = [];

    onLoad() {
        super.onLoad()
        if (this.mIcon) {
            this.mIcon.node.active = false;
        }
    }
    start() {
        this.spine.active = false;
    }
    /**
     * 
     * @param show 是否显示提示
     */
    private iconTips(show) {
        if (this.mIcon) {
            if (show) {
                this.mIcon.node.active = true;
                let head = this.ff.mFFheader;
                let count = head.getTmpCount(this.goodId);
                if (count > 0) {
                    this.mIcon.spriteFrame = this.mIconFrame[0]
                } else {
                    this.mIcon.spriteFrame = this.mIconFrame[1]
                }
            } else {
                this.mIcon.node.active = false;
            }
        }
    }

    onBegin(tag: number) {
        if (this.spine.active) {
            return;
        }
        if (tag == 1) {
            this.iconTips(true);
            this.showOpt(this.mTipsIcon, () => {
                this.submit()
            })
        }

    }
    onEnd(tag: number) {
        if (tag == 1) {
            this.closeButton();
        }
    }
    public closeButton() {
        this.iconTips(false);
        this.closeOpt()
    }

    public submit() {
        if (this.spine.active) {
            this.ff.main.showTips('祭火已经点亮');
            return;
        }
        this.closeButton();
        let head = this.ff.mFFheader;
        let count = head.getTmpCount(this.goodId);
        if (count > 0) {
            head.removeTmpGood(this.goodId, 1);
            this.spine.active = true;
            let fAltar = this.altar.getComponent(FAltar);
            let fAltarGear = this.altar.getComponent(FAltarGear);
            if (fAltar) {
                fAltar.check();
            } else if (fAltarGear) {
                let ok = fAltarGear.check();
                if (this.text.length > 0 && !ok) {
                    this.d0()
                }
                if (ok) {
                    FqLogin.commitEvent(this.node.name, '', '');
                }
            }
        } else {
            this.ff.main.showTips('需要祭火');
        }

    }

    private d0() {
        this.ff.pauseSprite(true);
        let dialogs = this.text;
        let mapDialog = new FMapDialog(this.ff, this.mMapDialog);
        let pos = cc.v2();
        let ga = this.ff.getGroupBy(GroupType.A);
        let spine = ga[0]
        pos.x = spine.node.x;
        pos.y = spine.node.y + spine.node.height;
        mapDialog.showDialog(dialogs,
            pos,
            spine.spine,
            () => {
                this.ff.pauseSprite(false);
            }
        );
    }


    // private checkOpen() {
    //     //检查其它开关是否打开
    //     this.pause();
    //     this.moveCamera(this.mFenceTrigger[0].getPosition(), 1, () => {
    //         cc.tween(this.node).sequence(
    //             cc.callFunc(() => {
    //                 for (let i = 0; i < this.mFenceTrigger.length; i++) {
    //                     const element = this.mFenceTrigger[i];
    //                     this.showFence(element, SpineName.OPEN);
    //                 }
    //                 this.ff.main.playerEffectByPath(AudioMgr.openDoor);
    //             }),
    //             cc.delayTime(1),
    //             cc.callFunc(() => {
    //                 this.resume()
    //                 for (let i = 0; i < this.mFenceTrigger.length; i++) {
    //                     const element = this.mFenceTrigger[i];
    //                     element.getComponent(cc.PhysicsBoxCollider).enabled = false;
    //                 }
    //             })
    //         ).start();

    //     })
    // }
    // private showFence(element, action) {
    //     let nodes = element.children;
    //     for (let i = 0; i < nodes.length; i++) {
    //         const element = nodes[i];
    //         let spine: sp.Skeleton = element.getComponent(sp.Skeleton);
    //         if (spine) {
    //             spine.setAnimation(0, action, false);
    //         }
    //     }
    // }

}
