
import FqLogin from "../../../../login/FqLogin";
import { HttpStateType, ReveData } from "../../../../util/CHttp";
import FFCalAttr from "../../../data/FFCalAttr";
import AIPet from "../../object/AI/AIPet";
import { GroupType } from "../../object/FObject";
import FSprite, { SpriteActionType } from "../../object/FSprite";
import PSprite from "../../object/PSprite";
import BaseEvent from ".././base/BaseEvent";
/**
 * 解救射手
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FCageShooter extends BaseEvent {

    @property({
        displayName: '解救的伙伴id',
    })
    mPetId: number = 1;

    @property({
        displayName: '牢笼动画',
        tooltip: '显示牢笼的动画',
        type: sp.Skeleton
    })
    spine: sp.Skeleton = null;//牢笼

    @property({
        displayName: '牢笼中的伙伴',
        type: cc.Node
    })
    mPet: cc.Node = null;//伙伴

    @property({
        displayName: '伙伴预制体',
        type: cc.Prefab
    })
    mPetPrefab: cc.Prefab = null;

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

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

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

    @property({
        type: [cc.Node],
        displayName: '需要打开的门'
    })
    mDoor: Array<cc.Node> = [];

    @property([cc.String])
    text: Array<string> = [];

    private isOver = false;
    onLoad() {
        super.onLoad()
        if (this.mIcon) {
            this.mIcon.node.active = false;
        }
        let petAttr = this.ff.main.player.getPet(this.mPetId)
        if (petAttr) {
            this.isOver = true
            if (this.spine.findAnimation("open3")) {
                this.spine.setAnimation(0, 'open3', false);
            } else {
                this.spine.setAnimation(0, 'open', false);
            }
            this.mPet.destroy()
            for (let i = 0; i < this.mDoor.length; i++) {
                const element = this.mDoor[i];
                element.destroy()
            }
        }
    }

    onBegin(tag: number) {
        if (this.isOver) {
            return;
        }
        if (tag == 1) {
            this.iconTips(true);
            this.showOpt(this.mTipsIcon, () => {
                this.iconTips(false);
                this.closeOpt()
                this.openCage()
            })
        }

    }
    onEnd(tag: number) {
        if (tag == 1) {
            this.iconTips(false);
            this.closeOpt()
        }
    }
    /**
     * 
     * @param show 是否显示提示
     */
    private iconTips(show) {
        if (this.mIcon) {
            if (show) {
                this.mIcon.node.active = true;
                let head = this.ff.mFFheader;
                let count = head.getTmpCount(2001);
                if (count > 0) {
                    this.mIcon.spriteFrame = this.mIconFrame[0]
                } else {
                    this.mIcon.spriteFrame = this.mIconFrame[1]
                }
            } else {
                this.mIcon.node.active = false;
            }
        }
    }

    //打开牢笼
    private openCage() {
        let head = this.ff.mFFheader;
        let count = head.getTmpCount(2001);
        if (count > 0) {
            this.isOver = true;

            this.spine.setCompleteListener(() => {
                this.spine.setCompleteListener(null);
                this.movePet()
            });
            if (this.spine.findAnimation("open3")) {
                this.spine.setAnimation(0, 'open3', false);
            } else {
                this.spine.setAnimation(0, 'open', false);
            }
            FqLogin.commitEvent(this.node.name, '', '');
        } else {
            this.ff.main.showTips('需要一把牢笼钥匙');
        }
    }


    private movePet() {
        let anim = this.mPet.getComponent(cc.Animation);
        let spine = this.mPet.getComponent(sp.Skeleton);

        spine.setAnimation(0, SpriteActionType.run, true);
        anim.on('finished', this.onFinished, this);
        anim.play('cage_pet_move');
    }
    private onFinished(num, string) {
        let anim = this.mPet.getComponent(cc.Animation);
        let spine = this.mPet.getComponent(sp.Skeleton);
        anim.off('finished', this.onFinished, this);
        spine.setAnimation(0, SpriteActionType.stand, true);

        //开始对话
        this.dialog(0)
    }
    private dialog(index: number) {
        if (index >= this.text.length) {
            this.getPet()
            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.dialog(index);
            });
        } else {
            let my = this.mPet;
            let pos = cc.v2()
            pos.x = this.node.x + my.x
            pos.y = this.node.y + my.y
            pos.y += my.height
            this.showDialogPos(pos, texts, () => {
                index++;
                this.dialog(index);
            });
        }
    }

    /**
     * 解救伙伴
     */
    private getPet() {
        this.isOver = true
        let head = this.ff.mFFheader;
        head.removeTmpGood(2001, 1);

        let msg = {
            petId: this.mPetId
        }
        let main = this.ff.main
        main.gameHttp.sendJson('stage/v1/getPet', msg, (state, reve: ReveData) => {
            main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    let player = main.player;
                    let petAttr = reve.data.petObject
                    player.pet[this.mPetId] = petAttr;
                    petAttr.id = this.mPetId;

                    let node = cc.instantiate(this.mPetPrefab);
                    node.group = GroupType.A;
                    node.x = this.node.x + this.mPet.x
                    node.y = this.node.y + this.mPet.y
                    let sp = node.addComponent(PSprite);
                    let attrData = FFCalAttr.getAttr(this.ff.main, petAttr)
                    sp.setAttrData(attrData)
                    this.ff.addRole(sp);
                    sp.hp = sp.attrData.hp
                    sp.addComponent(AIPet);
                    this.mPet.removeFromParent();

                    for (let i = 0; i < this.mDoor.length; i++) {
                        const element = this.mDoor[i];
                        element.destroy()
                    }

                } else {
                    main.showTips(reve.message);
                }
            } else {
                main.showTips('网络异常');
            }
        });



    }






}
