import FqLogin from "../../../../login/FqLogin";
import FSprite from "../../object/FSprite";
import BaseEvent from "../base/BaseEvent";

const { ccclass, property } = cc._decorator;

enum BoomStatus {
    safe,       //安全阶段
    warning,    //警告阶段
    boom,       //爆炸阶段
    end,        //爆炸结束
}
/**
 * 定时炸弹
 */
@ccclass
export default class FBigBomb extends BaseEvent {

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

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

    @property({
        displayName: "提示文字"
    })
    text: string = "";

    @property({
        displayName: "需要的物品ID"
    })
    goodId: 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;

    public spriteList: Array<FSprite> = [];

    isFire: boolean = false;
    boomStatus: BoomStatus = BoomStatus.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);
    }

    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.isFire) {
                    this.fire();
                }
            }
        }
    }

    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);
        }
        if(self.tag == 0){
            this.closeOpt();
        }
    }

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

    fire() {
        let count = this.ff.mFFheader.getTmpCount(this.goodId);
        this.showOpt(this.mTipsIcon, () => {
            this.closeOpt();
            if (count > 0) {
                this.isFire = true;
                this.ff.mFFheader.removeTmpGood(this.goodId, 1);
                if (!this.isWarning) {
                    this.warning();
                }
            } else {
                this.ff.main.showTips(this.text);
            }
        })
    }

    // 警告
    warning() {
        this.isWarning = true;
        this.spine.setAnimation(0, "boom1", 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) {
                // console.log("===爆炸结束===");
                FqLogin.commitEvent(this.node.name, '', '');
                this.end();
                this.unschedule(call);
            } else {
                if (!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;
    }

}
