import { stringToHex } from "viem";
import { TemplatesConfig } from "./templateConfig";
import config from "../../mud.config";

const settlementTemplate = {
  Combat: {
    health: 250_000,
    maxHealth: 250_000,
    armor: 0,
    strength: 0,
    structureStrength: 0,
    counterStrength: 0,
    minRange: 0,
    maxRange: 0,
    archetype: 10,
  },
  StructureType: { value: 1 },
  Untraversable: { value: true },
  Capturable: { value: true },
  Charger: { value: 0 },
  ChargeCap: { cap: 0, totalCharged: 0 },
  Factory: {
    prototypeIds: [
      stringToHex("Swordsman", { size: 32 }),
      stringToHex("Pikeman", { size: 32 }),
      stringToHex("Pillager", { size: 32 }),
      stringToHex("Archer", { size: 32 }),
      stringToHex("Knight", { size: 32 }),
      stringToHex("Brute", { size: 32 }),
      stringToHex("Catapult", { size: 32 }),
    ],
    goldCosts: [100, 150, 200, 250, 400, 550, 700],
  },
} satisfies TemplatesConfig<typeof config>["Settlement"];

export const templates: TemplatesConfig<typeof config> = {
  Swordsman: {
    Combat: {
      health: 120_000,
      maxHealth: 120_000,
      armor: 0,
      strength: 50_000,
      structureStrength: 0,
      counterStrength: -30,
      minRange: 0,
      maxRange: 1,
      archetype: 1,
    },
    UnitType: { value: 1 },
    GoldOnKill: { value: 25 },
    Movable: { value: 3_000 },
    Untraversable: { value: true },
  },
  Pikeman: {
    Combat: {
      health: 120_000,
      maxHealth: 120_000,
      armor: 0,
      strength: 50_000,
      structureStrength: 0,
      counterStrength: -30,
      minRange: 0,
      maxRange: 1,
      archetype: 2,
    },
    UnitType: { value: 2 },
    GoldOnKill: { value: 50 },
    Movable: { value: 3_000 },
    Untraversable: { value: true },
  },
  Halberdier: {
    Combat: {
      health: 500_000,
      maxHealth: 500_000,
      armor: 0,
      strength: 70_000,
      structureStrength: 0,
      counterStrength: -30,
      minRange: 0,
      maxRange: 1,
      archetype: 3,
    },
    UnitType: { value: 3 },
    GoldOnKill: { value: 500 },
    Movable: { value: 3_000 },
    Untraversable: { value: true },
  },
  GodUnit: {
    Combat: {
      health: 1000000,
      maxHealth: 1000000,
      armor: 0,
      strength: 300000,
      structureStrength: 0,
      counterStrength: 100,
      minRange: 0,
      maxRange: 1,
      archetype: 0,
    },
    UnitType: { value: 3 },
    GoldOnKill: { value: 250 },
    Movable: { value: 6000 },
    Untraversable: { value: true },
  },
  Pillager: {
    Combat: {
      health: 120_000,
      maxHealth: 120_000,
      armor: 0,
      strength: 50_000,
      structureStrength: 0,
      counterStrength: -30,
      minRange: 0,
      maxRange: 1,
      archetype: 4,
    },
    UnitType: { value: 4 },
    GoldOnKill: { value: 50 },
    Movable: { value: 4_000 },
    Untraversable: { value: true },
  },
  Knight: {
    Combat: {
      health: 180_000,
      maxHealth: 180_000,
      armor: 0,
      strength: 50_000,
      structureStrength: 0,
      counterStrength: -30,
      minRange: 0,
      maxRange: 1,
      archetype: 5,
    },
    UnitType: { value: 5 },
    GoldOnKill: { value: 150 },
    Movable: { value: 4_500 },
    Untraversable: { value: true },
  },
  Dragoon: {
    Combat: {
      health: 400_000,
      maxHealth: 400_000,
      armor: 0,
      strength: 70_000,
      structureStrength: 0,
      counterStrength: -30,
      minRange: 0,
      maxRange: 1,
      archetype: 6,
    },
    UnitType: { value: 6 },
    GoldOnKill: { value: 500 },
    Movable: { value: 4_500 },
    Untraversable: { value: true },
  },
  Archer: {
    Combat: {
      health: 100_000,
      maxHealth: 100_000,
      armor: 0,
      strength: 50_000,
      structureStrength: 0,
      counterStrength: -100,
      minRange: 2,
      maxRange: 3,
      archetype: 7,
    },
    UnitType: { value: 7 },
    GoldOnKill: { value: 100 },
    Movable: { value: 3_500 },
    Untraversable: { value: true },
  },
  Catapult: {
    Combat: {
      health: 120_000,
      maxHealth: 120_000,
      armor: 0,
      strength: 60_000,
      structureStrength: 0,
      counterStrength: -100,
      minRange: 3,
      maxRange: 4,
      archetype: 8,
    },
    UnitType: { value: 8 },
    GoldOnKill: { value: 250 },
    RequiresSetup: { value: true },
    Movable: { value: 2_000 },
    Untraversable: { value: true },
  },
  Marksman: {
    Combat: {
      health: 250_000,
      maxHealth: 250_000,
      armor: 0,
      strength: 70_000,
      structureStrength: 0,
      counterStrength: -100,
      minRange: 2,
      maxRange: 2,
      archetype: 9,
    },
    UnitType: { value: 9 },
    GoldOnKill: { value: 500 },
    Movable: { value: 3_500 },
    Untraversable: { value: true },
  },
  Brute: {
    Combat: {
      health: 300_000,
      maxHealth: 300_000,
      armor: 0,
      strength: 50_000,
      structureStrength: 0,
      counterStrength: -30,
      minRange: 0,
      maxRange: 1,
      archetype: 13,
    },
    UnitType: { value: 10 },
    GoldOnKill: { value: 200 },
    Movable: { value: 2_500 },
    Untraversable: { value: true },
  },
  Grass: {
    TerrainType: { value: 1 },
    MoveDifficulty: { value: 1000 },
  },
  Mountain: {
    TerrainType: { value: 2 },
    MoveDifficulty: { value: 2000 },
    ArmorModifier: { value: -30 }, // 30% less damage received
  },
  Forest: {
    TerrainType: { value: 3 },
    MoveDifficulty: { value: 1500 },
    ArmorModifier: { value: -15 }, // 15% less damage received
  },
  MapCenterMarker: {
    MapCenter: { value: true },
  },

  // Structures
  Settlement: settlementTemplate,
  SpawnSettlement: {
    ...settlementTemplate,
    Combat: {
      health: 500_000,
      maxHealth: 500_000,
      armor: 0,
      strength: 0,
      structureStrength: 0,
      counterStrength: 0,
      minRange: 0,
      maxRange: 0,
      archetype: 11,
    },
    StructureType: { value: 2 },
    Charger: { value: 50 },
    ChargeCap: { cap: 500, totalCharged: 0 },
    SpawnPoint: { value: true },
    GoldOnKill: { value: 500 },
    // Overriding the value on `settlementTemplate`
    Capturable: { value: false },
  },
  GoldMine: {
    StructureType: { value: 4 },
    Charger: { value: 50 },
    ChargeCap: { cap: 1000, totalCharged: 0 },
    Capturable: { value: true },
    Untraversable: { value: true },
    Combat: {
      health: 150_000,
      maxHealth: 150_000,
      armor: 0,
      strength: 0,
      structureStrength: 0,
      counterStrength: 0,
      minRange: 0,
      maxRange: 0,
      archetype: 12,
    },
  },
  GoldCache: {
    Combat: {
      health: 100_000,
      maxHealth: 100_000,
      armor: 0,
      strength: 0,
      structureStrength: 0,
      counterStrength: 0,
      minRange: 0,
      maxRange: 0,
      archetype: 11,
    },
    StructureType: { value: 5 },
    Untraversable: { value: true },
    GoldOnKill: { value: 250 },
  },
  WoodenWall: {
    StructureType: { value: 3 },
    Combat: {
      health: 200_000,
      maxHealth: 200_000,
      armor: 0,
      strength: 0,
      structureStrength: 0,
      counterStrength: 0,
      minRange: 0,
      maxRange: 0,
      archetype: 12,
    },
    Untraversable: { value: true },
  },
};
