/**
 * 获取职业技能
 */
import ViewObject from "../../../../../main/ViewObject";
import BaseEvent from "../../base/BaseEvent";
import { AudioMgr, GameViewType } from "../../../../../main/ViewManage";
import FSjpPanel from "./FSjpPanel";
import FqLogin from "../../../../../login/FqLogin";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FGetSkill extends BaseEvent {

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

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

    @property({
        displayName: '完成后的对话',
        type: [cc.String]
    })
    finish: Array<string> = [];//完成后的对话

    @property({
        displayName: '靠近的提示',
        type: cc.Node
    })
    icon: cc.Node = null;

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

    @property({
        displayName: '需要的物品id'
    })
    public goodId = 2006;

    @property({
        displayName: '需要存储的ID'
    })
    public saveId: string = "";

    @property({
        displayName: '需要打开的门',
        type: cc.Node
    })
    mOpenDoor: cc.Node = null;

    private isEnd = false

    onLoad() {
        super.onLoad()
        let stage = this.ff.main.player.stage;
        if (stage.element.indexOf(this.saveId) > -1) {
            this.mOpenDoor.active = false;
        }
    }

    onBegin(tag: number) {
        if (this.isEnd) {
            return
        }
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                this.startDialog()
            })
        }
    }
    onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
        }
    }

    public startDialog() {
        this.closeOpt()
        let head = this.ff.mFFheader;
        let count = head.getTmpCount(this.goodId);
        if (count > 0) {
            head.removeTmpGood(this.goodId, 1);
            //已经打开了技能
            let role = this.ff.main.player.role
            if (role.openSkill) {
                this.pause()
                this.moveCamera(this.mOpenDoor.getPosition(), 1, () => {
                    this.openGear()
                })
            } else {
                // this.showDialog2();
                this.showDialog(this.node, this.finish, ()=>{
                    this.pause()
                    this.moveCamera(this.mOpenDoor.getPosition(), 1, () => {
                        this.openGear()
                    })
                    this.getMapObject(this.saveId, null);
                    FqLogin.commitEvent(this.node.name,'','');
                })
            }
        } else {
            this.showDialog1(0);
        }
    }

    // private showDialog2() {
    //     this.showDialog(this.node, this.finish, () => {
    //         this.openSjpPanel()
    //     });
    // }
    private showDialog1(index: number) {
        if (index >= this.text.length) {
            return;
        }
        let texts = this.text[index].split('|')
        let mid = parseInt(texts.shift());
        if (mid == -1) {//主角
            let my = this.ff.mainSprite.node;
            this.showDialog(my, texts, () => {
                index++;
                this.showDialog1(index);
            });
        } else {
            let my = this.node;
            this.showDialog(my, texts, () => {
                index++;
                this.showDialog1(index);
            });
        }
    }

    private openGear() {
        this.isEnd = true
        if (this.icon) {
            this.icon.active = false;
        }
        let nodes = this.mOpenDoor.children;
        nodes.forEach(element => {
            let spine = element.getComponent(sp.Skeleton);
            if (spine) {
                spine.setCompleteListener(() => {
                    element.active = false;
                });
                spine.setAnimation(0, 'close', false);
                this.ff.main.playerEffectByPath(AudioMgr.openDoor);
            }
        });
        cc.tween(this.ff.mMap.node).sequence(
            cc.delayTime(1),
            cc.callFunc(() => {
                this.mOpenDoor.active = false;

                this.resume()
            })
        ).start()
    }
    /**
     * 打开水晶瓶界面
     */
    // private openSjpPanel() {
    //     let main = this.ff.main

    //     main.viewManage.loadFunc(GameViewType.fight_map_sjp_tips, (viewObject: ViewObject) => {
    //         viewObject.show();
    //         let sjp = viewObject as FSjpPanel;
    //         sjp.setCallback(() => {
    //             this.pause()
    //             this.ff.control.showSkill2()
    //             this.openGear()
    //             // this.moveCamera(this.mOpenDoor.getPosition(), 1, () => {
    //             //     this.openGear()
    //             // })
    //         })
    //     });

    // }


}
