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

const { ccclass, property } = cc._decorator;

enum BoomStatus {
    safe,       //安全阶段
    warning,    //警告阶段
    boom,       //爆炸阶段
    end,        //爆炸结束
}

enum RoleArea {
    safe,   // 安全
    hit,    // 受伤
}
/**
 * 定时炸弹
 */
@ccclass
export default class FBomb extends cc.Component {

    @property({
        displayName: "怪物ID"
    })
    monsterId: number = 0;

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

    @property({
        displayName: "警告区域",
    })
    warningArea = new cc.Vec2();

    @property({
        displayName: "爆炸范围",
    })
    boomArea = new cc.Vec2();

    @property({
        displayName: "警告时间(秒)"
    })
    warningTime: number = 0;

    @property({
        displayName: "爆炸时间(秒)"
    })
    boomTime: number = 0;

    ff: FF;
    public spriteList: Array<FSprite> = [];

    boomStatus: BoomStatus = BoomStatus.safe;
    // roleArea: RoleArea = RoleArea.safe;
    tempTime: number = 0;
    isHit: boolean = false;
    isWarning: boolean = false;

    start() {
        let boxs = this.node.getComponents(cc.PhysicsBoxCollider)
        boxs[0].size = new cc.Size(this.warningArea.x, this.warningArea.y);
        boxs[1].size = new cc.Size(this.boomArea.x, this.boomArea.y);
    }

    // public onBegin(tag: number) {
    //     // this.roleArea = tag == 1 ? RoleArea.hit : RoleArea.safe;
    //     if (tag == 0) {
    //         if (!this.isWarning) {
    //             this.warning();
    //         }
    //     }
    // }

    // public onEnd(tag: number) {
    //     if (tag == 1) {
    //         // this.roleArea = RoleArea.safe;
    //     }
    // }

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

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

    private 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
            }
        }
    }

    // 警告
    warning() {
        this.isWarning = true;
        this.spine.setAnimation(0, "boom", true);
        this.boomStatus = BoomStatus.warning;
        let tempTime = 0;
        let call = () => {
            tempTime += 0.1;
            if (tempTime >= this.warningTime) {
                // console.log("===警告结束===");
                this.boom();
                this.unschedule(call);
            }
        }
        this.schedule(call, 0.1);
    }

    //爆炸
    boom() {
        this.spine.setAnimation(0, "boom2", false);
        this.boomStatus = BoomStatus.boom;
        let tempTime = 0;
        let call = () => {
            tempTime += 0.1;
            if (tempTime >= this.boomTime) {
                this.end();
                this.unschedule(call);
            } else {
                if (!this.isHit) {
                    this.hit();
                }
                // if (this.roleArea == RoleArea.hit && !this.isHit) {
                //     this.hit();
                // }
            }
        }
        this.schedule(call, 0.1);
    }

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

    }

    //爆炸结束
    end() {
        this.boomStatus = BoomStatus.end;
        this.node.active = false;
    }

}
