import units from "../data/units";
import { getBuilding } from "../utils";
import type { VillageState } from "../village";


export default function recruitUnits(
    V: VillageState, buildingId: number, unitType: string, unitNumber: number
) {
    const unit = units.find(u => u.type === unitType);
    if (!unit) {
        return false;
    }

    const cost = {
        wood: unit.cost.wood * unitNumber,
        stone: unit.cost.stone * unitNumber,
        iron: unit.cost.iron * unitNumber,
        food: unit.cost.food * unitNumber,
    };

    if (
        cost.wood > V.resources.wood
        || cost.stone > V.resources.stone
        || cost.iron > V.resources.iron
        || cost.food > V.resources.food
    ) {
        return false;
    }

    V.resources.wood -= cost.wood;
    V.resources.stone -= cost.stone;
    V.resources.iron -= cost.iron;
    V.resources.food -= cost.food;

    const building = getBuilding(V, buildingId);
    if (!building.state.recruitment) {
        building.state.recruitment = {
            count: 0,
            elapsedTime: 0,
        };
    }

    building.state.recruitment.count += unitNumber;

    return true;
}
