import logging
import random

from lemonyellow.core.helpers import translate_moves
from lemonyellow.core.stats import get_IV, calculate_stats
from lemonyellow.data import STATUS, dialogue
from lemonyellow.models import Slot


class Trainer(object):
    slot = [Slot() for x in range(7)]

    name = "TRAINER"
    t_type = ""
    cash = 123

    dialogue_intro = "I'M A TRAINER"
    dialogue_loss = "CRAP, I SUCK AT THIS GAME"
    dialogue_extra = "This is used for if I need to give an item after battle!"
    dialogue_after = "I'M THE BEST!"


def clear_trainer():
    """
    Reset all the most common fields for the Trainer class so that extra
    data doesn't get left over.
    """
    for _ in range(1, 7):
        Trainer.slot[_].status = STATUS.INACTIVE

    Trainer.name = "TRAINER"
    Trainer.t_type = ""
    Trainer.cash = 123

    Trainer.dialogue_intro = None
    Trainer.dialogue_loss = None
    Trainer.dialogue_after = None
    Trainer.dialogue_extra = None


def load_trainer(trainernum, special=None):
    """
    "special" is an integer value used to determine a specific line of text.
    For example: load_trainer(001, special=1) would load rival 001, using
    rival text number 1.
    """

    def load_slot(t_slot, slotnum, trainer_n):
        try:
            t_slot.pokebase = trainer_n[slotnum + 2][0]
            t_slot.level = trainer_n[slotnum + 2][1]
            t_slot.moves = translate_moves(trainer_n[slotnum + 2][2])
            t_slot.IVs = get_IV()
            t_slot.stats = calculate_stats(
                t_slot.pokebase, t_slot.level, None, t_slot.IVs, trainer_n
            )
            t_slot.status = STATUS.OK
            logging.debug(
                "load_slot: Loaded data for {} into Trainer slot {}"
                "".format(t_slot.pokebase.name, slotnum)
            )
        except IndexError:
            pass

    # invoke clear_trainer() here to make sure that leftover data isn't
    # displayed
    clear_trainer()
    Trainer.name = trainernum[0]
    Trainer.t_type = trainernum[1]
    Trainer.cash = trainernum[2]
    for x in range(1, 7):
        load_slot(Trainer.slot[x], x, trainernum)

    logging.debug("load_trainer: Loaded pokemon for Trainer #{}".format(trainernum))

    # load in that dialogue!
    if special is None:
        # Trainer type is 2nd item in npc entry
        trainer_text = dialogue.trainers.translate(trainernum[1])
        randphrase = random.randrange(0, len(trainer_text))
        for i, phrase in trainer_text.items():
            if randphrase == i:
                Trainer.dialogue_intro = phrase[0]
                Trainer.dialogue_loss = phrase[1]
                Trainer.dialogue_after = phrase[2]
        logging.debug(
            "load_trainer: Loaded dialogue for Trainer #{}" "".format(trainernum)
        )
    else:
        # all hardcoded Trainer dialogue
        trainer_text = dialogue.special_dialogue
        try:
            for i, phrase in trainer_text.items():
                if int(special) == i:
                    Trainer.dialogue_intro = phrase[0]
                    Trainer.dialogue_loss = phrase[1]
                    Trainer.dialogue_after = phrase[2]
                    # since this is the special category, try and load any
                    # extra item-based dialogue there may be
                    try:
                        Trainer.dialogue_extra = phrase[3]
                    except IndexError:
                        pass
            logging.debug(
                "load_trainer: Loaded special dialogue for "
                "Trainer #{}".format(trainernum)
            )
        except AttributeError:
            pass

    return Trainer
