import sys

TEMPLATE = '''
<svg width="4in" height="1in" version="1.1"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">

  <rect x="0in" y="0in" width="4in" height="1in"
    style="fill: white; stroke-width: 0.02in; stroke: #ddd;" />

  <rect x="0.1in" y="0.1in" width="0.8in" height="0.8in"
    style="fill: white; stroke-width: 0.02in; stroke: #999;" />

  <line x1="1.1in" y1="0.3in" x2="1.1in" y2="0.9in" style="stroke-width: 0.02in; stroke: #999;" />

  <image x="0.1in" y="0.1in" width="0.8in" height="0.8in" xlink:href="../icons/{type}-icon.svg" />

  <text x="1in" y="0.1in"
    dominant-baseline="hanging"
    style="font-family: Fira Sans;
    font-size: 12pt;
    font-weight: bold;">
    {header}
  </text>

  {text}
</svg>
'''

TEXT_TEMPLATE = '''<text x="{x}in" y="{y}in"
  dominant-baseline="hanging"
  style="font-family: Fira Sans; font-size: 10pt; {s}">
  {t}</text>'''


WEAPONS = [
    ('Bow', 'ranged', 'Arrow-Split Shot',
     ['Split your Ranged dice to attack multiple',
      'non-adjacent targets up to 6 squares away.']),

    ('Claymore', 'melee', 'All-Out Attack',
     ['Take +1d to a Melee attack, but until the',
      'start of your next turn your Defense pool has',
      'no dice.'
      ]),

    ('Crossbow', 'ranged', 'Cover Shot',
     ['Move 1 square, make a Ranged attack with',
      '-1d, then return to your original position.'
      ]),

    ('Cutlass', 'melee', 'Pressing Attack',
     ['Melee attack a target with -1d; the target ',
      'is also pushed back 1 square and you ',
      'move into its square.'
      ]),

    ('Daggers', 'ranged', 'Sneaky Attack',
     ['You can make a +1d Ranged attack at any ',
      'engaged target in range.'
      ]),

    ("Mace", "melee", "Knockback",
     ["Melee attack at an adjacent with -1d.",
      "If the attack hits, the target is also",
      "pushed back 4 squares."]),

    ("Hammer", "melee", "Iron Wall",
        ["Your Defense pool gains 1 extra die until ",
         "the start of your next turn."]),

    ("Longsword", "melee", "Strikeback Attack",
        ["If an adjacent target has attacked you ",
         "since your last turn, you can attack that ",
         "target with +1d."]),

    ("Shortsword", "melee", "Whirlwind",
        ["Split your Melee dice to make Melee ",
         "attacks at multiple adjacent targets."]),

    ("Staff of Fire", "magic", "Flame Burst",
        ["Make 1 die Arcane attacks at all ",
         "adjacent targets, including enemies ",
         "and allies."]),

    ("Staff of Light", "magic", "Healing Touch",
        ["Remove 1 damage from yourself or an ",
         "adjacent ally."]),

    ("Staff of Telekinesis", "magic", "Wave of Force",
        ["Make a +1d Arcane attack. If it hits, do not",
         "deal damage, but move the target up to",
         "4 squares away from its current position."]),

    ("Staff of Thunder", "magic", "Fork Lightning",
        ["Split your Arcande dice to make Arcane",
         "attacks at multiple targets in range."]),

    ("Staff of Vines", "magic", "Tangling Roots",
        ["Make a -1d Arcane attack at a target in",
         "range. If it hits, the target also cannot",
         "move on its next turn."]),

    ("Staff of Water", "magic", "Freezing Strike",
        ["When you roll a 6 on an attack die, your",
         "target cannot move during their next turn."]),

    ("Whip", "ranged", "Lasso",
        ["Make a Ranged attack at a target in range.",
         "If it hits, the target takes no damage, but is",
         "pulled to an empty square closer to you."]),

    ("Longbow", "ranged", "Distant Shot",
        ["Make a -1d Ranged attack at a non-adjacent",
         "target up to 8 squares away."]),

    ("Brigand's Axe", "melee", "Brutal Slash",
        ["Re-roll any Melee attack dice that roll a 1."]),

    ("Footman's Pike", "melee", "Reaching Strike",
        ["Your Melee attacks can target characters",
         "up to 1 square away, but your Melee",
         "attacks at adjacent targets take -1d."]),

    ("Lucky Longsword", "melee", "Warrior's Luck",
        ["Once per encounter, you can re-roll all of",
         "your Melee attack dice."]),

    ("Raider's Battleaxe", "melee", "Reaping Strike",
        ["When you KO an enemy with a Melee",
         "attack, you can immediately make a Melee",
         "attack at another adjacent target."]),

    ("Swift Sword", "melee", "Following Cut",
        ["When you roll a 6 on a Melee attack die,",
         "you can immediately make a 1d Melee",
         "attack at an adjacent target."]),

    ("Bloodthirsty Blade", "melee", "Quenching Hit",
        ["The first time in each encounter that you",
         "hit with a Melee attack, the attack deals",
         "1 extra damage."]),

    ("Thundering Hammer", "melee", "Thunder Strike",
        ["Once per encounter, you can deal 1 extra",
         "damage when you hit with a Melee attack."]),

    ("Heavy Crossbow", "ranged", "Opening Shot",
        ["Once per encounter, you can use your",
         "action to make a 3d Ranged attack at",
         "a target in range."]),

    ("Faerie Wand", "magic", "Faerie Fire",
     ["When you hit a target with an Arcane attack,",
      "all attacks against that target gain +1d."]),

    ("Explosive Wand", "magic", "Arcane Burst",
     ["When you KO a target with an Arcane attack,",
      "you must immediately make 1d attacks at",
      "all characters adjacent to that target."]),

    ("Throwing Axe", "ranged", "Spinning Throw",
     ["Once per encounter, instead of moving, you",
      "can make a 2d Ranged attack at a target up",
      "to 3 squares away."]),

    ("Bandit's Bow", "ranged", "Brutal Shot",
     ["Re-roll any Ranged attack dice that roll a 1."]),

    ("Deepwood Bow", "ranged", "Precision Shot",
     ["Once per encounter, you can add +1d",
      "when you make a Ranged attack."]),

    ("Sharpshooter's Bow", "ranged", "Surprise Shot",
     ["Your first ranged attack in each encounter",
      "deals 1 extra damage if it hits."]),

    # healing equipment
    ("Staff of Light", "magic", "Healing Touch",
        ["Remove 1 damage from yourself or an ",
         "adjacent ally."]),

    ("Vampiric Staff", "magic", "Leech",
     ["On a successful Arcane attack against an",
      "enemy, you can also remove 1 damage from 1"
      "ally adjacent to that enemy."]),

    ("Staff of Dawn's Light", "magic", "Bolt of Comfort",
     ["Make an Arcane roll against an ally in range.",
      "On a hit, remove 1 damage from them."]),

    ("Divine Staff", "magic", "Godly Sigil",
     ["Mark an adjacent square. For the",
      "next 5 turns, if any creature ends its turn in",
      "that square, they remove 1 damage."]),

    ("Staff of Dusk's Shadow", "magic", "",
     ["Make an Arcane roll against yourself. On a",
      "hit, remove 2 damage from yourself or from ",
      "adjacent allies."]),

]


CARDS = [
    # items
    ("Bomb",
     "item",
     ["Make 1d attacks at a target or square up to",
      "5 squares away and all adjacent targets."]),

    # wards
    ("Ward of Force",
     "item",
     ["Mark an adjacent square. The next creature to",
      "enter that square takes 2 damage."]),

    ("Ward of Healing",
     "item",
     ["Mark an adjacent square. The next creature to",
      "enter that square recovers 2 damage."]),

    # oils
    ("Cruel Oil",
     "item",
     ["On your next successful hit, deal 1 extra damage."]),

    ("Greedy Oil",
     "item",
     ["On your next successful hit, remove 1 damage from yourself."]),

    # tools
    ("Rope",
     "item",
     ["Take +1d to any ability test to climb or",
      "traverse obstacles or difficult terrain."]),

    ("Bait",
     "item",
     ["Toss this item on an adjacent square. On",
      "their next turn, one enemy will ignore",
      "everything else and move straight for the",
      "targeted square."]),

    ("Goldleaf Herb",
     "plant",
     ["Can be used to brew a Potion of Healing.",
      "Requires an Intelligence (Alchemy) roll of",
      "5 or higher to brew."]),

    ("Frostberry Seeds",
     "plant",
     ["Can be used to brew a Potion of Dexterity.",
      "Requires an Intelligence (Alchemy) roll of",
      "5 or higher to brew."]),

    ("Oxroot Strands",
     "plant",
     ["Can be used to brew a Potion of Strength.",
      "Requires an Intelligence (Alchemy) roll of",
      "5 or higher to brew."]),

    ("Coldwine Flowers",
     "plant",
     ["Can be used to brew a Potion of Intelligence.",
      "Requires an Intelligence (Alchemy) roll of",
      "5 or higher to brew."]),

    ("Potion of Healing",
     "item",
     ["Restore yourself or an ally to full health."]),

    ("Potion of Charisma",
     "item",
     ["You become extra-charismatic for 5",
      "minutes. You gain +2d to Intelligence",
      "(Talking) tests."]),

    ("Potion of Dexterity",
     "item",
     ["You become nimble for 5 minutes.",
      "You gain +2d to your Ranged pool and when",
      "making Dexterity ability tests."]),

    ("Potion of Intelligence",
     "item",
     ["You become smarter for 5 minutes.",
      "You gain +2d to your Arcane pool and when",
      "making Intelligence ability tests."]),

    ("Potion of Invisibility",
     "item",
     ["You become invisible for 5 minutes.",
      "You gain +2d to your Defense pool and when",
      "making Dexterity (Stealth) ability tests."]),

    ("Potion of Quickness",
     "item",
     ["You become quicker for 5 minutes.",
      "If you don't move on your turn, you can",
      "take 2 actions."]),

    ("Potion of Strength",
     "item",
     ["You become stronger for 5 minutes.",
      "Gain +2d to your Melee pool and when",
      "making Strength ability tests."]),

    ("Stack of Gold",
     "treasure",
     ["Treasure. Can be exchanged for goods and",
      "services."]),

    ("Pile of Gems",
     "treasure",
     ["Treasure. Can be exchanged for goods and",
      "services."]),

    # abilities
    ("Teamwork", "ability",
     ["When your target is engaged, your Melee",
      "attacks gain +1d."]),

    ("Nimble", "ability",
     ["You can move up to 5 squares on your turn,",
      "ignoring obstacles, enemies, and allies."]),

    ("Revenge Strike", "ability",
     ["Take an extra +1d when attacking an enemy",
      "that has previously attacked you."]),

    ("Retaliation", "ability",
     ["When you hit a target that has damaged",
      "you since your last turn, deal 2 damage."]),

    ("Power Surge", "ability",
     ["When you are not at full health, your",
      "attacks gain +1d."]),

    ("Keen Reflexes", "ability",
     ["When defending against Melee attacks,",
      "you gain +1d to your Defense pool."]),

    ("Evasive Maneuver", "ability",
     ["When you're damaged by an attack, you",
      "can immediately move 1 square."]),

    ("Alchemical Training", "ability",
     ["Take +1d on all Intelligence (Alchemy)",
      "ability tests."]),

    ("Rhetorical Training", "ability",
     ["Take +1d on all Intelligence (Talking)",
      "ability tests."]),

    ("Stealth Training", "ability",
     ["Take +1d on all Dexterity (Stealth)",
      "ability tests."]),

    ("Keen Vision", "ability",
     ["You can see even in dark or low-light",
      "conditions."]),

    ("Academic Background", "ability",
     ["Take +1d on all Intelligence (Lore)",
      "ability tests."]),

    ("Defender", "ability",
     ["When an adjacent ally is hit, you can take",
      "the damage instead of the ally."]),

    ("Hard to Kill", "ability",
     ["When you take damage that would KO you,",
      "you can still act on your next turn. If you're",
      "still KO'd at the end of your next turn,",
      "you are knocked out."]),

    ("Unstoppable", "ability",
     ["You can move through spaces occupied",
      "by enemies."]),

    ("Aggravated", "ability",
     ["When you miss an enemy with an attack,",
      "you can add +1d to your next attack",
      "against that enemy."]),

    ("Momentum", "ability",
     ["When you deal damage to an enemy,",
      "you can add +1d to your next attack",
      "against that enemy."]),

    ("Barkskin", "ability",
     ["When you are not at full health, your Defense",
      "pool gains +1d."]),

    # equipment
    ("Boomerang Buckler", "equipment",
     ["Ranged attack with your Defense dice at a ",
      "target up to 4 squares away, but until the ",
      "start of your next turn your Defense pool",
      "has no dice."]),

    ("Defender's Shield", "equipment",
     ["Move 1 die from your Defense pool to an",
      "adjacent ally's Defense pool until the start",
      "of your next turn."]),

    ("Everpresent Shield", "equipment",
     ["When you are attacked by more than ",
      "1 enemy, your Defense pool gains 1 extra die",
      "until the start of your next turn."]),

    ("Guard's Shield", "equipment",
     ["Use your action to add all of your Melee,",
      "Ranged, or Arcane dice to your Defense pool",
      "until the start of your next turn."]),

    ("Heavy Shield", "equipment",
     ["Gain 1 extra Defence die. Your movement is",
      "reduced by 2 squares."]),

    ("Light Shield", "equipment",
     ["Re-roll any Defence dice that roll a 1."]),

    ("Striker's Spauldron", "equipment",
     ["When you are attacked by an enemy that",
      "attacked last turn, you gain 1 die to your",
      "Defence pool."]),

    ("Wrist Bracers", "equipment",
     ["You can add 1 extra die to your Defence",
      "pool, but on your next turn your attacks",
      "have 1 fewer dice."]),

    ("Charged Amulet", "equipment",
     ["Once per encounter, you can add +1d to",
      "any Arcane attack."]),

    ("Crystal Amulet", "equipment",
     ["Re-roll any Arcane attack die that rolls a 1."]),

    ("Amulet of Protection", "equipment",
     ["At the start of your turn, you can move 1d",
      "from your Arcane pool to your Defense pool",
      "until the start of your next turn."]),

    ("Amulet of Retreat", "equipment",
     ["After you have been attacked, you can",
      "teleport up to 3 squares away."]),

    ("Amulet of Healing", "equipment",
     ["Extend the range of healing powers 1 square",
      "(including touch or adjacent powers.)"]),

    ("Lucky Charm", "equipment",
     ["Once per encounter, you can re-roll 1d in",
      "an attack, Defence, or ability test."]),

    ("Vengeful Pendant", "equipment",
     ["When you take attack damage that will ",
      "KO you, you can immediately make a",
      "normal attack."]),

    ("Focus Charm", "equipment",
     ["Once per encounter, you can add +1d to",
      "any Ranged attack."]),

    ("Charm of Power", "equipment",
     ["Once per encounter, you can add +1d to",
      "any Melee attack."]),

    ("Studded Armor", "equipment",
     ["Take +1d to your Defense dice when",
      "defending against any Melee attack."]),

    ("Leather Armor", "equipment",
     ["Take +1d to your Defense dice when",
      "defending against any Ranged attack."]),

    ("Consecrated Armor", "equipment",
     ["Take +1d to your Defense dice when",
      "defending against any Arcane attack."]),

    ("Banded Armor", "equipment",
     ["Take +1d to your Defense dice when",
      "defending against any Arcane attack."]),

]


def weapon(name, type, special, special_text):
    raw_text = ['Special: {0}'.format(special)] + special_text
    text = ''
    for n, t in enumerate(raw_text):
        s = '' if n else 'font-weight: bold;'
        y = 0.3 + n * 0.16
        x = '1.2' if n else '1.15'
        text += TEXT_TEMPLATE.format(x=x, t=t, y=y, s=s)

    with open('cards/weapon_{0}.svg'.format(slug(name)), 'w') as f:
        f.write(TEMPLATE.format(
            type=type,
            header='{0} (Weapon, {1})'.format(name, type.capitalize()),
            text=text
        ))


def slug(name):
    return ''.join(
        c for c in '_'.join(name.lower().split())
        if c.isalpha() or c == '_')


def card(name, type, raw_text):
    text = ''
    for n, t in enumerate(raw_text):
        s = ''
        y = 0.3 + n * 0.16
        x = '1.15'
        text += TEXT_TEMPLATE.format(x=x, t=t, y=y, s=s)

    if type == 'plant' or type == 'treasure':
        named_type = 'item'
    else:
        named_type = type

    with open('cards/{0}_{1}.svg'.format(named_type.lower(), slug(name)), 'w') as f:
        f.write(TEMPLATE.format(
            type=type,
            header='{0} ({1})'.format(name, named_type.capitalize()),
            text=text
        ))


def main():
    for thing in WEAPONS:
        weapon(*thing)

    for thing in CARDS:
        card(*thing)


def to_rec():
    print('%rec: Weapon')
    for (name, typ, ability, effect) in WEAPONS:
        print('Name: {0}'.format(name))
        print('Type: {0}'.format(typ))
        print('Ability: {0}'.format(ability))
        print('Effect: {0}'.format(' '.join(effect)))
        print()

    stuffs = {}
    for (name, typ, effect) in CARDS:
        if not stuffs.get(typ):
            stuffs[typ] = []
        stuffs[typ].append((name, ' '.join(effect)))

    for (typ, things) in stuffs.items():
        print('%rec: {0}'.format(typ.capitalize()))
        for (name, effect) in things:
            print('Name: {0}'.format(name))
            print('Effect: {0}'.format(effect))
            print()


if __name__ == '__main__':
    if sys.argv[1:] and sys.argv[1] == 'rec':
        to_rec()
    else:
        main()
