import { SpriteActionType } from "../object/FSprite";
import BaseEvent from "./base/BaseEvent";

/**
 * 土堆
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FHitMound extends BaseEvent {

    @property(sp.Skeleton)
    spine: sp.Skeleton = null;

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

    @property({
        displayName: '打碎后显示',
        type: cc.Node
    })
    hide: cc.Node = null;

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

    @property({
        displayName: '敲击次数',
    })
    mHitCount: number = 1;

    private hitCount = 0;
    onLoad() {
        super.onLoad()
        if (this.icon) {
            this.icon.active = false;
        }
        if (this.hide && this.hide.isValid) {
            this.hide.active = false;
        }
    }

    onBegin(tag: number) {
        if (tag == 1) {
            this.showOpt(this.mTipsIcon, () => {
                this.hitCount ++
                let element = this.ff.mainSprite
                element.useHammer()
                element.playAction2(SpriteActionType.chuizi, false, () => {
                    this.ff.pauseSprite(false)
                    element.playAction2(SpriteActionType.stand, true);
                    element.updateSkin()
                })
                if(this.hitCount >= this.mHitCount){
                    this.hitBox()
                }else{
                    this.spine.setAnimation(0, 'hit', false);
                }
            })
        } 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;
            }
        }
    }

    private hitBox() {
        this.onEnd(1)
        this.ff.pauseSprite(true)
        this.node.removeComponent(cc.PhysicsBoxCollider)
        this.node.removeComponent(cc.PhysicsBoxCollider)
        let element = this.ff.mainSprite
        element.useHammer()
        element.playAction2(SpriteActionType.chuizi, false, () => {
            this.ff.pauseSprite(false)
            element.playAction2(SpriteActionType.stand, true);
            element.updateSkin()
        })
        cc.tween(element.node).sequence(
            cc.delayTime(0.2),
            cc.callFunc(() => {
                this.spine.setCompleteListener(() => {
                    this.node.destroy();
                })
                if (this.hide && this.hide.isValid) {
                    this.hide.active = true;
                }
                this.spine.setAnimation(0, 'dead', false);
            })
        ).start();
    }
}
