import FqLogin from "../../../login/FqLogin";
import { AudioMgr } from "../../../main/ViewManage";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import FF from "../FF";
import FMap from "../map/FMap";
import FSprite from "../object/FSprite";
import BaseEvent from "./base/BaseEvent";
import WOneByone from "./map1/WOneByone";

/**
 * 闲聊脚本
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FDialogNone extends BaseEvent {

    @property({
        displayName: '地图元素编号'
    })
    public mapGoodId: string = '23';

    @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([cc.Node])
    mFenceTrigger: Array<cc.Node> = [];

    onLoad() {
        super.onLoad()
        if (this.icon) {
            this.icon.active = false;
        }
    }

    onBegin(tag: number) {
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                this.startDialog()
            })
        } else if (tag == 2) {
            if (this.icon) {
                this.icon.active = true;
            }
        }

    }
    onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
        } else if (tag == 2) {
            if (this.icon) {
                this.icon.active = false;
            }
        }
    }

    public startDialog() {
        if (this.icon) {
            this.icon.active = false;
        }
        if (this.mapGoodId == '') {
            this.showDialog1();
        } else {
            let ff = this.ff;
            let stage = ff.main.player.stage;
            if (stage.element.indexOf(this.mapGoodId) > 0) {
                this.showDialog2();
            } else {
                this.showDialog1();
            }
        }
    }

    public closeButton() {
        if (this.icon) {
            this.icon.active = false;
        }
        this.closeOpt()
    }

    private showDialog2() {
        this.ff.pauseSprite(true);
        this.ff.mBlockInputEvents.active = true;
        let dialogs = this.finish;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x;
        node.y = this.node.y + this.node.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);

        obo.dialogs = dialogs;
        obo.setCallback(() => {
            this.closeButton();
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;
            this.openmFenceTrigger()
        });
        this.ff.setBlockInputCallback(() => {
            obo.jump();
        });
        obo._start();
    }
    private showDialog1() {
        this.ff.pauseSprite(true);
        this.ff.mBlockInputEvents.active = true;
        let dialogs = this.text;

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x;
        node.y = this.node.y + this.node.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);

        obo.dialogs = dialogs;
        obo.setCallback(() => {
            this.closeButton();
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;
            if (this.mapGoodId != "") {
                this.getMapObject(this.mapGoodId);
            }
            if (!this.finish.length) {
                this.openmFenceTrigger()
            }
        });
        this.ff.setBlockInputCallback(() => {
            obo.jump();
        });
        obo._start();
    }

    /**
     * 捡起地图上的物品
     * @param objectId 
     */
    public getMapObject(objectId: string) {
        let msg = {
            objectId: objectId
        }
        let ff = this.ff;
        ff.main.gameHttp.sendJson('stage/v1/stageObject', msg, (state, reve: ReveData) => {
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    let player = ff.main.player;
                    let stage = player.stage;
                    stage.element.push(objectId);
                    ff.main.showReward(reve, () => {
                        this.openmFenceTrigger()
                    });
                } else {
                    ff.main.showTips(reve.message);
                }
            } else {
                ff.main.showTips('网络异常');
            }
        });
    }

    public openmFenceTrigger() {
        if (this.mFenceTrigger.length <= 0) {
            return
        }
        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, 'close');
                    }
                    this.ff.main.playerEffectByPath(AudioMgr.openDoor);
                }),
                cc.delayTime(1),
                cc.callFunc(() => {
                    this.resume()
                    FqLogin.commitEvent(this.node.name, '', '');
                    for (let i = 0; i < this.mFenceTrigger.length; i++) {
                        const element = this.mFenceTrigger[i];
                        element.active = false;

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

}
