"""Code for parsing the backend of the Pokedex program
Starter code by David Szeto. Implementation by the CSCA20 student.
"""


import const


def get_pokemon_stats():
    """Reads the data contained in a pokemon data file and parses it into
    several data structures.

    Args:
        None

    Returns: a tuple of:
        -a dict where:
            -each key is a pokemon name (str)
            -each value is a tuple containing a tuple of the following stats:
                -pokemon name (str)
                -species_id (int)
                -height (float)
                -weight (float)
                -type_1 (str)
                -type_2 (str)
                -url_image (str)
                -generation_id (int)
                -evolves_from_species_id (str)
        -a dict where:
            -each key is a pokemon species_id (int)
            -each value is the corresponding pokemon name (str)
        -a list of all pokemon names (strs)
        -a dict where:
            -each key is a pokemon type (str). Note that type_1 and type_2
            entries are all considered types. There should be no special
            treatment for the type NA; it is considered a type as well.
            -each value is a list of all pokemon names (strs) that fall into
            the corresponding type
    """
    name_to_stats = {}
    id_to_name = {}
    names = []
    pokemon_by_type = {}
    with open(const.DATA_FILENAME) as f:
        header_dict = parse_header(f)
        for line in f:
            line = line.strip().split(const.SEP)
            pokemon = line[header_dict['pokemon']]
            species_id = int(line[header_dict['species_id']])
            height = float(line[header_dict['height']])
            weight = float(line[header_dict['weight']])
            type_1 = line[header_dict['type_1']]
            type_2 = line[header_dict['type_2']]
            url_image = line[header_dict['url_image']]
            generation_id = int(line[header_dict['generation_id']])
            evolves_from_species_id = str(line[header_dict[
                'evolves_from_species_id']])
            name_to_stats[pokemon] = (pokemon, species_id, height, weight,
                                      type_1, type_2, url_image, generation_id,
                                      evolves_from_species_id)
            id_to_name[species_id] = pokemon
            names.append(pokemon)
            if type_1 not in pokemon_by_type:
                pokemon_by_type[type_1] = []
            if type_2 not in pokemon_by_type:
                pokemon_by_type[type_2] = []
            pokemon_by_type[type_1].append(pokemon)
            pokemon_by_type[type_2].append(pokemon)
    return name_to_stats, id_to_name, names, pokemon_by_type


def parse_header(f):
    """Parses the header and builds a dict mapping column name to index

    Args:
        f: a freshly opened file in the format of pokemon.csv

    Returns:
        a dict where:
            -each key is one of:
                'pokemon', 'species_id', 'height', 'weight', 'type_1',
                'type_2', 'url_image', 'generation_id',
                'evolves_from_species_id'
            -each value is the index of the corresponding key in the CSV file
                starting from column 0.
                eg. If 'pokemon' is in the second column, then its index will
                be 1. If 'species_id' is the third column, then its index will
                be 2.
    """
    header = f.readline()
    header = header.strip().split(const.SEP)
    columns = ['pokemon', 'species_id', 'height', 'weight', 'type_1', 'type_2',
               'url_image', 'generation_id', 'evolves_from_species_id',
               'evolution_chain_id', 'shape']
    result = {}
    for column in columns:
        result[column] = header.index(column)
    return result
