import sys
import os
import argparse
import math

parser = argparse.ArgumentParser()
parser.add_argument('-dir', '--directory', '--game-directory', dest='game_dir',
                    help='set path of GAME_DIR', required=True)

print("Type arguments and tap enter. If there are not any arguments tap enter with empty string")
print("Use -h or --help to get list of arguments")
args = parser.parse_args(input().split())
path_to_game = args.game_dir


def get_lvl(path):
    lvl = 0
    for file in os.listdir(path=path):
        if file == 'unitname.txt':
            cur_file = open(path + '\\' + file, 'r')
            # print(cur_file)
            name = cur_file.read()
            if 'IV' in name:
                lvl = 4
            elif 'III' in name:
                lvl = 3
            elif 'II' in name:
                lvl = 2
            elif 'I' in name:
                lvl = 1
            cur_file.close()
    return lvl


def get_baseclass(path):
    for file in os.listdir(path=path):
        if file == 'baseclass.txt':
            cur_file = open(path + '\\' + file, 'r')
            name = cur_file.read()
            cur_file.close()
    return name


def print_traits(file, lvl, baseclass):
    file.write('lvl=')
    file.write(str(lvl) + '\n')
    file.write('cost=')
    file.write(str(125 + 150 * (lvl - 1)) + '\n')
    file.write('starting_ap=')
    file.write(str(max(2, lvl ** 2 - 2 * lvl - 2 * lvl // 4)) + '\n')
    if baseclass == 'Warrior\n' or baseclass == 'Rider\n':
        file.write('strength=')
        file.write(str(lvl + 1) + '\n')
        file.write('agility=')
        stat = max(lvl - 1, 0)
        file.write(str(stat) + '\n')
        file.write('intelligence=')
        stat = max(lvl - 2, 0)
        file.write(str(stat) + '\n')
    elif baseclass == 'Archer\n' or baseclass == 'Rogue\n':
        stat = max(lvl - 2, 0)
        file.write('strength=')
        file.write(str(stat) + '\n')
        file.write('agility=')
        stat = max(lvl, 0)
        file.write(str(lvl + 1) + '\n')
        file.write('intelligence=')
        stat = max(lvl - 2, 0)
        file.write(str(stat) + '\n')
    else:
        stat = max(lvl - 2, 0)
        file.write('strength=')
        file.write(str(stat) + '\n')
        file.write('agility=')
        stat = max(lvl - 2, 0)
        file.write(str(stat) + '\n')
        file.write('intelligence=')
        file.write(str(lvl + 1) + '\n')

    file.write('initiative=')
    if baseclass == 'Rider\n' or 'Rogue\n':
        file.write(str(math.ceil(lvl * 2.5)) + '\n')
    elif baseclass == 'Archer\n' or 'Warrior\n':
        file.write(str(math.ceil(lvl * 2)) + '\n')
    elif baseclass == 'Mage\n':
        file.write(str(math.ceil(lvl * 1.5)) + '\n')

    if baseclass == 'Rogue\n':
        file.write('magic_def=' + str(math.ceil(lvl * 1.5)) + '\n')
        file.write('physic_def=' + str(math.ceil(lvl)) + '\n')
    elif baseclass == 'Rider\n':
        file.write('magic_def=' + str(max(1, int(math.ceil(lvl * 0.75)))) + '\n')
        file.write('physic_def=' + str(math.ceil(lvl * 1.5)) + '\n')
    elif baseclass == 'Warrior\n':
        file.write('magic_def=' + str(max(1, int(math.ceil(lvl * 0.75)))) + '\n')
        file.write('physic_def=' + str(math.ceil(lvl * 2)) + '\n')
    elif baseclass == 'Mage\n':
        file.write('magic_def=' + str(math.ceil(lvl * 2)) + '\n')
        file.write('physic_def=' + str(max(1, int(math.ceil(lvl * 0.75)))) + '\n')
    elif baseclass == 'Archer\n':
        file.write('magic_def=' + str(math.ceil(lvl)) + '\n')
        file.write('physic_def=' + str(max(1, int(math.ceil(lvl * 0.75)))) + '\n')


# -d D:\Honourihed\repo


def race_traits_fill(racename):
    path_to_race = path_to_game + '\\assets\\units\\' + racename
    for class_dir in os.listdir(path=path_to_race):
        if '.' not in class_dir:
            for file in os.listdir(path=path_to_race + '\\' + class_dir):
                if file.startswith('traits'):
                    path = path_to_race + '\\' + class_dir + '\\' + file
                    try:
                        lvl = get_lvl(path_to_race + '\\' + class_dir)
                        baseclass = get_baseclass(path_to_race + '\\' + class_dir)

                        os.rename(path, path_to_race + '\\' + class_dir + '\\' + 'traits.ini')
                        path = path_to_race + '\\' + class_dir + '\\' + 'traits.ini';

                        cur_file = open(path, 'w')
                        print_traits(cur_file, lvl, baseclass)
                        cur_file.close()
                        print(path + ' - OK!')

                    except OSError:
                        print(path + ' - FAILED!')


def main():
    race_traits_fill('undead')
    race_traits_fill('human')


if __name__ == '__main__':
    main()
