import FqLogin from "../../../../../login/FqLogin";
import { AudioMgr } from "../../../../../main/ViewManage";
import BaseEvent from "../../base/BaseEvent";
import FFruit1_3 from "./FFruit1_3";


/**
 * 摘果子任务 奖励编号26
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FPick1_3 extends BaseEvent {

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

    @property({
        type: cc.Node,
        displayName: '任务提示'
    })
    public iconTouch: cc.Node = null;

    @property({
        displayName: '收集的道具id'
    })
    public goodId = 2005;

    @property({
        displayName: '给以的道具id'
    })
    public giveGoodId = 2004;

    @property({
        type: [FFruit1_3],
        displayName: '收集的地图上的果子'
    })
    public goodNodes: Array<FFruit1_3> = [];

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

    @property({
        displayName: "对话过一次之后的提示",
        type: ""
    })
    text_1: string = "";

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

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

    /**
     * 控制的栅栏
     */
    @property([cc.Node])
    mFenceTrigger: Array<cc.Node> = [];

    /**
     * 记录当前状态
     * 0:从来没有过对话
     * 1:对话过一次
     */
    public state: number = 0;

    public isOver = false;

    onLoad() {
        super.onLoad()
        for (let i = 0; i < this.goodNodes.length; i++) {
            const element = this.goodNodes[i];
            element.setPick26(this);
        }
    }

    onBegin(tag: number) {
        if (this.isOver) {
            return;
        }
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                let head = this.ff.mFFheader;
                let count = head.getTmpCount(this.goodId);
                if (count < 3) {//未收集到的对话
                    if (this.state == 0) {
                        this.dialog1()
                    } else {
                        this.dialog1_1()
                    }
                } else {//收集到物品对话
                    this.dialog2()
                }
            })
        }

    }
    onEnd(tag: number) {
        if (tag == 1) {
            this.closeOpt()
        }
    }

    public dialog1() {
        this.closeOpt()
        this.showDialog(this.node, this.text, () => {
            this.state = 1;
            this.activateGood();
        });
    }
    public dialog1_1() {
        this.closeOpt()
        this.showDialog(this.node, [this.text_1], () => {
        });
    }
    public dialog2() {
        this.closeOpt()
        this.showDialog(this.node, this.finish, () => {
            let head = this.ff.mFFheader;
            head.removeTmpGood(this.goodId, 3)
            if (this.giveGoodId) {
                head.addTmpGood(this.giveGoodId, 1)
            }
            FqLogin.commitEvent(this.node.name, '', '');
            this.node.active = false;
            this.isOver = true
            this.iconTouch.active = false
            this.openmFenceTrigger();
        });
    }

    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()
                    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);
            }
        }
    }

    public activateGood() {
        for (let i = 0; i < this.goodNodes.length; i++) {
            this.goodNodes[i].node.getChildByName("lizi_gs").active = true;
        }
    }

}
