import { world, system, MolangVariableMap } from "@minecraft/server";
import { VEHICLES } from "./VehiclesRoot.js";

export const AMMO_ITEMS = {
    MAIN: "ttav:tank_main_gun",
    SUB: "ttav:tank_sub_gun",
    FLARE: "ttav:tank_flare_gun"
};

export const AMMO_RELOAD_AMOUNTS = {
    MAIN: 10,
    SUB: 200,
    FLARE: 5
};

export const AMMO_LIMITS = {
    TANK: {
        MAIN: 30,
        SUB: 1500,
        FLARE: 10
    },
    ARTILLERY: {
        MAIN: 50,
        SUB: 1000,
        FLARE: 10
    },
    IFV_ROCKET: {
        MAIN: 120,
        SUB: 1200,
        FLARE: 5
    },
    APV_ROCKET: {
        MAIN: 10,
        SUB: 3000,
        FLARE: 10
    },
    MLRS: {
        MAIN: 15
    }
};

function getVehicleType(tank) {
    const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
    return vehicleConfig?.modelType;
}

function getAmmoLimit(tank, ammoType) {
    const vehicleType = getVehicleType(tank);
    return AMMO_LIMITS[vehicleType][ammoType];
}

export function getMainAmmo(tank) {
    const ammo = tank.getDynamicProperty("ttav:main_ammo");
    return ammo || 0;
}

export function getSubAmmo(tank) {
    if (!tank?.id) {
        return 0;
    }
    const ammo = tank.getDynamicProperty("ttav:sub_ammo");
    return ammo || 0;
}

export function getFlareAmmo(tank) {
    const ammo = tank.getDynamicProperty("ttav:flare_ammo");
    return ammo || 0;
}

export function reduceMainAmmo(tank) {
    const currentAmmo = tank.getDynamicProperty("ttav:main_ammo") || 0;
    if (currentAmmo > 0) {
        tank.setDynamicProperty("ttav:main_ammo", currentAmmo - 1);
        return true;
    }
    return false;
}

export function reduceSubAmmo(tank) {
    if (!tank?.id) {
        return false;
    }
    const currentAmmo = tank.getDynamicProperty("ttav:sub_ammo") || 0;
    if (currentAmmo > 0) {
        tank.setDynamicProperty("ttav:sub_ammo", currentAmmo - 1);
        return true;
    }
    return false;
}

export function reduceFlareAmmo(tank) {
    const currentAmmo = tank.getDynamicProperty("ttav:flare_ammo") || 0;
    if (currentAmmo > 0) {
        tank.setDynamicProperty("ttav:flare_ammo", currentAmmo - 1);
        return true;
    }
    return false;
}

export function handleAmmoReload(player, tank, itemId) {
    if (!tank?.id) return;

    const config = Object.values(VEHICLES).find(v => v.identifier === tank.typeId);
    if (config?.modelType === "MLRS") {
        if (itemId === AMMO_ITEMS.SUB) {
            player.onScreenDisplay.setActionBar("§c这辆装甲车没有辅助武器.");
            player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            return;
        }
        if (itemId === AMMO_ITEMS.FLARE) {
            player.onScreenDisplay.setActionBar("§c这辆装甲车没有信号弹系统.");
            player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            return;
        }
    }

    switch (itemId) {
        case AMMO_ITEMS.MAIN:
            const currentMainAmmo = tank.getDynamicProperty("ttav:main_ammo") || 0;
            const mainAmmoLimit = getAmmoLimit(tank, "MAIN");
            if (currentMainAmmo < mainAmmoLimit) {
                const newMainAmmo = Math.min(mainAmmoLimit, currentMainAmmo + AMMO_RELOAD_AMOUNTS.MAIN);
                tank.setDynamicProperty("ttav:main_ammo", newMainAmmo);
                tank.dimension.spawnParticle("ttav:tank_interact_fx",
                    { x: tank.location.x, y: tank.location.y, z: tank.location.z },
                    new MolangVariableMap()
                );
                player.runCommand(`clear @s ${AMMO_ITEMS.MAIN} 1`);
                player.playSound("ttavMainGunRefill", { pitch: 1.0, volume: 1.0 });
                player.onScreenDisplay.setActionBar(`§a重新装填主炮弹药! §f(§e${newMainAmmo}§f/§6${mainAmmoLimit}§f)`);
            } else {
                player.onScreenDisplay.setActionBar("§c主炮弹药已满!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
            break;

        case AMMO_ITEMS.SUB:
            const currentSubAmmo = tank.getDynamicProperty("ttav:sub_ammo") || 0;
            const subAmmoLimit = getAmmoLimit(tank, "SUB");

            if (currentSubAmmo < subAmmoLimit) {
                const newSubAmmo = Math.min(subAmmoLimit, currentSubAmmo + AMMO_RELOAD_AMOUNTS.SUB);
                tank.setDynamicProperty("ttav:sub_ammo", newSubAmmo);
                tank.dimension.spawnParticle("ttav:tank_interact_fx",
                    { x: tank.location.x, y: tank.location.y, z: tank.location.z },
                    new MolangVariableMap()
                );
                player.runCommand(`clear @s ${AMMO_ITEMS.SUB} 1`);
                player.playSound("ttavSubGunRefill", { pitch: 1.0, volume: 1.0 });
                player.onScreenDisplay.setActionBar(`§a重新装填副炮弹药! §f(§e${newSubAmmo}§f/§6${subAmmoLimit}§f)`);
            } else {
                player.onScreenDisplay.setActionBar("§c副炮弹已满!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
            break;

        case AMMO_ITEMS.FLARE:
            const currentFlareAmmo = tank.getDynamicProperty("ttav:flare_ammo") || 0;
            const flareAmmoLimit = getAmmoLimit(tank, "FLARE");
            if (currentFlareAmmo < flareAmmoLimit) {
                const newFlareAmmo = Math.min(flareAmmoLimit, currentFlareAmmo + AMMO_RELOAD_AMOUNTS.FLARE);
                tank.setDynamicProperty("ttav:flare_ammo", newFlareAmmo);
                tank.dimension.spawnParticle("ttav:tank_interact_fx",
                    { x: tank.location.x, y: tank.location.y, z: tank.location.z },
                    new MolangVariableMap()
                );
                player.runCommand(`clear @s ${AMMO_ITEMS.FLARE} 1`);
                player.playSound("ttavFlareGunRefill", { pitch: 1.0, volume: 1.0 });
                player.onScreenDisplay.setActionBar(`§a重新装填信号弹弹药! §f(§e${newFlareAmmo}§f/§6${flareAmmoLimit}§f)`);
            } else {
                player.onScreenDisplay.setActionBar("§c信号弹弹药已满!");
                player.playSound("note.bass", { pitch: 1.0, volume: 1.0 });
            }
            break;
    }
}