#!/usr/bin/env python
description="""
Final Fantasy 4 Free Enterprise Post-Randomizer

This tool dumps information or makes modifications to a specified smc file.
Currently, the smc file must be a raw, uncompressed (unzipped) file.

By default, no modifications are written to disk.  The --apply/-a flag
will write a new file to disk.  The new file will be in the same location
as the original rom, with the seed appended to the original filename."""

from ff4fepr.output import error, warning, optional_output
from ff4fepr.util import parse_string_args

string_of_args="""
add-spells-to-weapons   Add spell to weapon Arg: WeaponName=Spell:Visual:Power,Weapon2=Spell:Visual:Power
shuffle-weaponatk     Shuffles weapon attack power within specified weapon categories.
vary-weaponatk        Varies weapon attack power (default is -5 to 15, see documentation for details)
rydia-random-calls    Rydia starts with additional call spells.  Arg: <1 to 4 spells to add>
rydia-starting-calls  Add additional spells to Rydia's starting calls.  Arg: spells to add.
randomize-learned     Randomizes learned spells, chance to learn a spell is 1 in 1.5 times the normal level spell is learned. Arg: list of spellsets to randomize.
buff-starting-equip   Improves starting equipment for all characters.  Arg: overworld, underworld, or moon.
randomize-spell-progression Randomizes all learned spells.  Arg is factor for chance per level to learn spell.
randomize-spell-progression-3 Randomizes all learned spells.  Arg is ratio between decreasing or increasing the level of the spell.  4:1 means an 80pct chance to lower level requirement and 20pct chance to raise requirement.
replace-commandset    Replaces the commandset for specified character.  Arg: <CharnameJoin>:<cmd1>,<cmd2>,<cmd3>
dual-wield            Sets the character's handedness to R+L.  Arg: <Charname>
set-char-stats  Sets starting stats for characters.  Example: KidRydia:hp=300,maxhp=300,maxmp=50.Rosa:agi=20
set-cast-times  Sets cast time for spells.  "Meteo*=5,Quake*=8"
add-bossbit    Add boss bit to comma-delimited list of spells (spells won't hit bosses)
remove-bossbit Removes boss bit from comma-delimited list of spells (allows spells to hit bosses)
xp-imp                Set xp value for imp enemy
dump-monster-values   Arg: keys to dump with each monster
dump-weapon-by Dump weapons by element-index
test-encoding  Prints the ff4 hex codes for provided string
"""

string_of_flags="""
rydia-allrares        Rydia starts with all four rare call spells (also renames the Dummied cockatrice spell)
dk-equip              Dark Knight Cecil gains access to most equipment shared by Kain and Paladin Cecil
randomize-drops Randomizes all drop tables.
modup-weaponatk       Runs a preprogrammed set of various randomizations on weapon attack.
uber-tellah    Tellah starts with some overpowered enemy abilities
paladin-spells Add some higher-level white spells to Cecil's Paladin magic
restore-j-drops    Restore the monster drop tables from FF4j
bird           Rename Dummied Cockatrice spell to Bird
edtest         flag for testing purposes
spoonjoke      Change spoon itemname graphic to ring.
ct-rebalance   Reduce cast delay on some Black and Summon magic (elementals and Meteo)
coral-sword    Replaces the Ancient Sword with a Lightning-elemental Coral Sword.
any-weapon     Allows any character/class to use any weapon.
yang-post60hp  Give Yang post-60 hp progression.
medusa         Replace the Medusa Sword with a randomly created weapon
dump-learned   Dump spell progressions from rom to screen.
dump-starting-spells    Dump starting spells from rom to screen.
dump-starting-stats  Dump starting stats from rom to screen.
dump-menus     Dump character battle menus from rom to screen.
dump-equip     Dump character starting equipment from rom to screen.
dump-weapons   Dump weapon data from rom to screen.
dump-weapon-racial   Dump weapon data from rom to screen.
dump-drops     Dump enemy stea/drop data from rom to screen.
dump-monsters  Dump monster data to screen.
dump-monsters-csv Dump monster data to screen in csv format. (no bosses)
dump-monsters-and-bosses-csv Dump monster data to screen in csv format.
dump-monster-itemtables  Dump monster item table numbers and drop rates to screen.
dump-monster-drops Dump monster drop data to screen. (resolved item-tables)
dump-spells    Dump spell stats
dump-eqtables  Dump class equip tables
dump-itemnames Dump item names from rom.
dump-itemkeys  Dump item names used by ff4fepr
dump-spellkeys Dump spell names used by ff4fepr
dump-levelup   Dump levelup data
dump-generator Dump generator.
generate-form  Generate web form from script options (doesn't do much atm)
hitratings     list attack * hitrate for each weapon
test-levelup   Test loading levelup tables and dumping to rom with no changes
test-monsters  Test loading monsters and dumping to rom with no changes
test-weapons   Test loading weapons into memory and dumping to rom with no changes.
test-drops     Test loading drop tables and dumping to rom with no changes.
test-spells    Test loading and writing spell stats with no changes.
test-eqtables  Test loading and writing equip tables with no changes.
tpassbuff      Add Bacchus, Silkweb, StarVeil, Elixir, and Spoon to Troia Pass Shop
special        ????
"""

if __name__ == '__main__':
    import argparse
    from argparse import RawTextHelpFormatter
    from pprint import pformat
    import sys
    flags=[
        ('--apply', '-a'),
        ('--version', '-V'),
        ('--verbose', '-v'),
        ('--debug', '-d'),
        ('--jv', '-j')
        ]
    sflags=parse_string_args(string_of_flags)
    strargs=parse_string_args(string_of_args)
    parser=argparse.ArgumentParser(description=description, formatter_class=RawTextHelpFormatter)
    parser.add_argument('--seed', '-s', help='specify a seed for random functions')
    parser.add_argument('--output', '-o', help='specify an output file')
    for sarg, kwargs in strargs:
        parser.add_argument(sarg, **kwargs)
    parser.add_argument('rompath', nargs='?')
    for flag in flags:
        parser.add_argument(*flag, action='store_true')
    for flag, kwargs in sflags:
        parser.add_argument(flag, action='store_true', **kwargs)
    args=parser.parse_args()
    args.string_of_args=string_of_args
    args.string_of_flags=string_of_flags
    debug, verbose=optional_output(args)
    debug("Args:\n")
    debug("%s\n" % pformat(args))
    if args.version:
        from ff4fepr import __version__ as V
        sys.stdout.write("ff4fepr %s\n" % V)
        sys.exit(0)
    if args.rompath == 'test':
        from ff4fepr import core
        sys.stdout.write("%s" % dir(core))
    else:
        from ff4fepr.main import main
        main(args)
