import { ItemComponentTypes, ItemStack, world } from "@minecraft/server";
import { expansionEvents, timerManager } from "./serverExpansion";
import * as util from './serverUtils';
let charging_start_text = { rawtext: [
        { translate: "accessibility.purerain_dino.message.charging_start" }
    ]
};
let charging_end_text = { rawtext: [
        { translate: "accessibility.purerain_dino.message.charging_end" }
    ]
};
let no_ammo_text = { rawtext: [
        { translate: "accessibility.purerain_dino.message.energy_depletion" }
    ]
};
class RayManager {
    constructor() {
        this.Guns = new Map();
        this.Shooters = new Map();
        this.reloading = new Map();
        //取消攻击实体造成的耐久损耗，此处需额外在数驱自定义组件中注册'purerain_dino:lacer'组件
        world.beforeEvents.worldInitialize.subscribe((args => {
            args.itemComponentRegistry.registerCustomComponent('purerain_dino:lacer', {
                onBeforeDurabilityDamage(args) {
                    args.durabilityDamage = 0;
                },
            });
        }));
        world.afterEvents.itemStartUse.subscribe((args => {
            const item = args.itemStack;
            const gunComp = this.Guns.get(item.typeId);
            if (gunComp) {
                const player = args.source;
                const duraComp = item.getComponent(ItemComponentTypes.Durability);
                const magazine = new ItemStack(gunComp.magazine);
                if (duraComp) {
                    if (duraComp.damage < duraComp.maxDurability) {
                        duraComp.damage += 1;
                        this.shoot(player, gunComp);
                        util.item.setItem(player, item);
                        if (gunComp.isAuto) {
                            const timerId = timerManager.createTimer(() => {
                                const duraComp = item.getComponent(ItemComponentTypes.Durability);
                                if (duraComp && duraComp.damage < duraComp.maxDurability) {
                                    duraComp.damage += 1;
                                    this.shoot(player, gunComp);
                                    util.item.setItem(player, item);
                                }
                            }, { time: gunComp.interval, repeat: true });
                            this.Shooters.set(player.id, timerId);
                        }
                    }
                    else if (duraComp.damage = duraComp.maxDurability) {
                        args.source.onScreenDisplay.setActionBar(no_ammo_text);
                        if (this.reloading.has(player.id))
                            return;
                        if (util.item.hasItem(player, magazine)) {
                            args.source.onScreenDisplay.setActionBar(charging_start_text);
                            const timer = timerManager.createTimer(() => {
                                const itemNow = util.item.getEquipment(player);
                                if (itemNow?.matches(item.typeId) && util.item.consumeItem(player, magazine)) {
                                    args.source.onScreenDisplay.setActionBar(charging_end_text);
                                    duraComp.damage = 0;
                                    util.item.setItem(player, item);
                                }
                                this.reloading.delete(player.id);
                            }, { time: gunComp.reload });
                            this.reloading.set(player.id, timer);
                        }
                    }
                }
            }
        }));
        world.afterEvents.itemStopUse.subscribe((args => {
            const player = args.source;
            const timerId = this.Shooters.get(player.id);
            if (timerId) {
                timerManager.removeTimer(timerId);
                this.Shooters.delete(player.id);
            }
        }));
        expansionEvents.selectedItemChange.subscribe(args => {
            const player = args.player;
            if (this.reloading.has(player.id)) {
                const timer = this.reloading.get(player.id);
                timerManager.removeTimer(timer);
                this.reloading.delete(player.id);
            }
        });
    }
    addGun(typeId, comp1) {
        let comp2 = {
            magazine: comp1.magazine,
            interval: comp1.interval !== undefined ? comp1.interval : 20,
            reload: comp1.reload !== undefined ? comp1.reload : 36,
            isAuto: comp1.isAuto !== undefined ? comp1.isAuto : true,
            tracer: comp1.tracer,
            dmgfunc: comp1.dmgfunc,
            itemId: typeId
        };
        this.Guns.set(typeId, comp2);
    }
    getGun(typeId) {
        return this.Guns.get(typeId);
    }
    shoot(player, gunComp) {
        //player.runCommand(`title @s actionbar here`)
        player.runCommand(`function purerain_dino/purerain/rays/${gunComp.dmgfunc}`);
        player.runCommand(`function purerain_dino/purerain/tracers/${gunComp.tracer}`);
    }
}
export const rayMng = new RayManager();
