import FF from "../../FF";
import FSprite from "../../object/FSprite";

/**
 * 掉落的石头
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FStoneDrop extends cc.Component {

    @property({
        displayName: '数值(怪物id)'
    })
    public monsterId = 1001;

    @property({
        displayName: '每次滚动停留时间(秒)'
    })
    public interval = 2;

    @property({
        type: sp.Skeleton,
        displayName: '滚动动画'
    })
    public spine: sp.Skeleton = null;

    @property({
        displayName: "警告",
        type: cc.Node
    })
    warnNode: cc.Node = null;

    private ff: FF;
    private isRoll = false;
    private isHit: boolean = false;
    private isWarn: boolean = false;
    private tempTime: number = 0;
    public spriteList: Array<FSprite> = [];

    callFunc: Function = null;

    onLoad() {
        cc.tween(this.node).sequence(
            cc.delayTime(this.interval),
            cc.fadeIn(0),
            cc.callFunc(() => {
                this.isRoll = true;
                this.spine.setAnimation(0, "atk", false);
                // this.spine.setEndListener(() => {
                //     this.isRoll = false;
                //     this.isHit = false;
                // })
            }),
            cc.delayTime(1),
            cc.fadeOut(0.2),
            cc.callFunc(() => {
                this.isRoll = false;
                this.isHit = false;
                this.tempTime = 0;
                this.isWarn = false;
            })
        ).repeatForever().start();
    }

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (other.node.group == 'A' && other.tag == 1) {
            let obj = other.node.getComponent(FSprite);
            this.ff = obj.ff;
            if (this.spriteList.indexOf(obj) == -1) {
                this.spriteList.push(obj);
            }
        }
    }

    onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (other.node.group == 'A' && other.tag == 1) {
            let obj = other.node.getComponent(FSprite);
            this.removeSprite(obj);
            this.isHit = false;
        }
    }

    removeSprite(sprite: FSprite) {
        for (let i = 0; i < this.spriteList.length; i++) {
            const element = this.spriteList[i];
            if (element == sprite) {
                this.spriteList.splice(i, 1);
                break
            }
        }
    }

    hit() {
        this.isHit = true;
        this.spriteList.forEach(sprite => {
            let main = this.ff.main;
            let attrData = main.sManage.getMonsterData(this.monsterId);
            sprite.bAtkjs(attrData)
        })
    }

    warnAction() {
        if (this.isWarn) return
        this.warnNode.stopActionByTag(1);
        this.isWarn = true;
        this.warnNode.opacity = 0;
        let action = cc.tween(this.warnNode).sequence(
            cc.fadeIn(0.1),
            cc.fadeOut(0.1),
        ).repeat(6).start();
        action.tag(1);
    }


    update(dt) {
        if (this.isRoll && !this.isHit) {
            this.hit();
        }

        this.tempTime += dt;
        if (this.interval - this.tempTime <= 1) {
            this.warnAction();
        }
    }
}
