import FqLogin from "../../../../login/FqLogin";
import { AudioMgr, GameViewType } from "../../../../main/ViewManage";
import ViewObject from "../../../../main/ViewObject";
import FqPay, { UM_EVENT_ID } from "../../../../pay/FqPay";
import BaseEvent from "../base/BaseEvent";
import FTipsEvent from "../tips/FTipsEvent";

/**
 * 按照顺序打开机关
 * 
 * 踩的顺序记录在第一个节点上
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FopenDoorOrder extends BaseEvent {

    @property({
        displayName: '顺序编号',
        type: cc.Integer
    })
    mNumber: number = 1;

    @property({
        displayName: '替换的图片',
        type: cc.Sprite,
    })
    mIcon: cc.Sprite = null;

    @property({
        displayName: '未踩上图片',
        type: cc.SpriteFrame,
    })
    mIcon0: cc.SpriteFrame = null;

    @property({
        displayName: '踩上后的图片',
        type: cc.SpriteFrame,
    })
    mIcon1: cc.SpriteFrame = null;

    @property({
        displayName: '踩上后点亮的火把',
        type: cc.Node,
    })
    mFireNode: cc.Node = null;

    @property({
        displayName: '失败后传送的位置',
        type: cc.Node,
    })
    mSiteNode: cc.Node = null;

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

    @property({
        displayName: '提示文字',
    })
    mTipsWord = '';

    /**
     * 记录踩的顺序
     */
    private checkOrder = []
    private isHand = false
    /**
     * 玩家选择错误次数
     */
    private errorCount = 0;
    onLoad() {
        super.onLoad()
        this.mFireNode.active = false
    }
    onBegin(tag: number) {
        if (this.isHand) {
            return
        }
        this.isHand = true
        this.mIcon.spriteFrame = this.mIcon1
        this.mFireNode.active = true;
        this.checkOpen()
    }

    public checkOpen() {
        let firstNode = this.mButtons[0].getComponent(FopenDoorOrder)
        firstNode.checkOrder.push(this.mNumber)
        if (firstNode.checkOrder.length >= this.mButtons.length) {
            if (this.check(firstNode.checkOrder)) {
                this.openDoor()
            } else {
                this.resumeButton()
                firstNode.errorCount++
                if (firstNode.errorCount >= 3) {
                    firstNode.errorCount = 0
                    this.openEventTips()
                }
            }
        }
    }
    /**
     * 显示提示的提示
     */
    private openEventTips() {
        this.ff.main.viewManage.loadFunc(GameViewType.fight_map_event_tips, (viewObject: ViewObject) => {
            let tipsEvent: FTipsEvent = viewObject as FTipsEvent
            tipsEvent.init('查看提示', '点击查看提示，可偷窥答案')
            tipsEvent.setCallback(() => {
                viewObject.exitDistroy()

                let fqPay: FqPay = new FqPay(this.ff.main)
                if (this.node.name == 'event12') {
                    fqPay.adVideo((result: number) => {
                        if (result == 1) {
                            this.openTips()
                        }else if (result == -1) {
                            this.ff.main.showTips('广告还未加载好,请稍后在试')
                        }
                    }, UM_EVENT_ID.ad_color_2_0, UM_EVENT_ID.ad_color_2_1)
                } else {
                    fqPay.adVideo((result: number) => {
                        if (result == 1) {
                            this.openTips()
                        }else if (result == -1) {
                            this.ff.main.showTips('广告还未加载好,请稍后在试')
                        }
                    }, UM_EVENT_ID.ad_color_5_0, UM_EVENT_ID.ad_color_5_1)
                }
            })
            viewObject.show();
        });
    }
    /**
     * 显示提示的提示
     */
    private openTips() {
        let firstNode = this.mButtons[0].getComponent(FopenDoorOrder)
        this.ff.main.viewManage.loadFunc(GameViewType.fight_map_event_tips, (viewObject: ViewObject) => {
            let tipsEvent: FTipsEvent = viewObject as FTipsEvent
            tipsEvent.init('确定', firstNode.mTipsWord)
            tipsEvent.setCallback(() => {
                viewObject.exitDistroy()
            })
            viewObject.show();
        });
    }
    /**
     * 检查顺序是否正确
     * @param array 
     * @returns 
     */
    private check(array: Array<number>): boolean {
        for (let i = 0; i < array.length; i++) {
            const element = array[i];
            if (element != i + 1) {
                return false
            }
        }
        return true
    }
    /**
     * 恢复所有按钮
     */
    private resumeButton() {
        for (let i = 0; i < this.mButtons.length; i++) {
            const element = this.mButtons[i];
            let fdo = element.getComponent(FopenDoorOrder)
            fdo.mIcon.spriteFrame = fdo.mIcon0
            fdo.mFireNode.active = false;
            fdo.isHand = false
            fdo.checkOrder = []
        }
        let firstNode = this.mButtons[0].getComponent(FopenDoorOrder)
        cc.tween(this).sequence(
            cc.delayTime(0.1),
            cc.callFunc(() => {
                this.ff.mainSprite.node.setPosition(firstNode.mSiteNode.getPosition())
            })
        ).start()
    }

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



}
