from __future__ import absolute_import

import os

from pgoapi.pokemon import Pokemon
from operator import attrgetter
from pgoapi.game_master import PokemonData
from pgoapi.protos.POGOProtos.Inventory import Item_pb2 as Enum_Items


def get_item_name(s_item_id):
    available_items = Enum_Items.ItemId.DESCRIPTOR.values_by_number.items()
    for (item_id, item) in available_items:
        if item_id == s_item_id:
            return item.name.replace('ITEM_', '', 1)
    return 'Unknown'

def parse_game_master():
    line_count = 0
    game_master = {}
    with open("GAME_MASTER_POKEMON_v0_2.tsv") as tsvfile:
        tsvreader = csv.reader(tsvfile, delimiter="\t")
        attributes = []
        for line in tsvreader:
            if line_count == 0:
                attributes = line
                line_count += 1
                continue
            pokemon_data = PokemonData()
            for x in range(0, len(line)):
                setattr(pokemon_data, attributes[x], line[x])
            game_master[int(line[0])] = pokemon_data
    return game_master

def pokemon_iv_percentage(pokemon):
    return ((pokemon.get('individual_attack', 0) + pokemon.get('individual_stamina', 0) + pokemon.get(
        'individual_defense', 0) + 0.0) / 45.0) * 100.0

def get_inventory_data(res, poke_names, order, nlimit):
    inventory_delta = res['responses']['GET_INVENTORY'].get('inventory_delta', {})
    inventory_items = inventory_delta.get('inventory_items', [])
    pokemons = map(lambda x: Pokemon(x['pokemon_data'], poke_names),
                   filter(lambda x: 'pokemon_data' in x,
                          map(lambda x: x.get('inventory_item_data', {}), inventory_items)))
    inventory_items_pokemon_list = sorted(filter(lambda x: not x.is_egg, pokemons), key=(attrgetter(order)), reverse=True)[:nlimit]
    return os.linesep.join(map(str, inventory_items_pokemon_list))


def create_capture_probability(capture_probability):
    capture_balls = capture_probability.get('pokeball_type', [])
    capture_rate = capture_probability.get('capture_probability', [])
    if not capture_probability or not capture_rate or len(capture_balls) != len(capture_rate):
        return None
    else:
        return dict(zip(capture_balls, capture_rate))


def get_pokemon_by_long_id(pokemon_id, res):
    for inventory_item in res:
        pokemon_data = inventory_item['inventory_item_data'].get('pokemon_data', {})
        if not pokemon_data.get('is_egg', False) and pokemon_data.get('id', 'NA') == pokemon_id:
            return Pokemon(pokemon_data)
    return None


def get_item_name(item):
    if item == Enum_Items.ITEM_POKE_BALL:
        return "Poke Ball"
    elif item == Enum_Items.ITEM_GREAT_BALL:
        return "Great Ball"
    elif item == Enum_Items.ITEM_ULTRA_BALL:
        return "Ultra Ball"
    elif item == Enum_Items.ITEM_MASTER_BALL:
        return "Master Ball"
    elif item == Enum_Items.ITEM_POTION:
        return "Potion"
    elif item == Enum_Items.ITEM_SUPER_POTION:
        return "Super Potion"
    elif item == Enum_Items.ITEM_HYPER_POTION:
        return "Hyper Potion"
    elif item == Enum_Items.ITEM_MAX_POTION:
        return "Max Potion"
    elif item == Enum_Items.ITEM_BLUK_BERRY:
        return "Bluk Berry"
    elif item == Enum_Items.ITEM_NANAB_BERRY:
        return "Nana Berry"
    elif item == Enum_Items.ITEM_REVIVE:
        return "Revive"
    elif item == Enum_Items.ITEM_MAX_REVIVE:
        return "Max Revive"
    elif item == Enum_Items.ITEM_RAZZ_BERRY:
        return "Razz Berry"
    else:
        return "Unknown Item"


DISK_ENCOUNTER = {0: "UNKNOWN",
                  1: "SUCCESS",
                  2: "NOT_AVAILABLE",
                  3: "NOT_IN_RANGE",
                  4: "ENCOUNTER_ALREADY_FINISHED"}
