#!/usr/bin/env python

from databaseconnect import Player, Rooms, Mobs, SqlDataEdit
from parser import *
from fight import *

class CommandProcessor:

    @staticmethod
    def is_number(var):
        try:
            float(var)
            return True
        except ValueError:
            print 'Not a number value'
            return False

    def practice(self, attribute):
        """
        The method defines what the command practice does:
        The command can only be used when in the room of Trainer: 3

        :param attribute: is a list of string objects that is supplied
                          with the original syntax via
                          prac <attribute> <amount>
        :return:
        """
        if len(attribute) < 1:
            Parser.color('|WMust supply a command|x')

        elif len(attribute) == 1:
            Parser.color('|WMust supply an amount|x')

        elif len(attribute) > 2:
            Parser.color('Too many arguments')
        else:
            amount = attribute[1]
            attribute = Parser().text_parser(attribute[0], command_list="trainer")
            if attribute is None:
                available_commands = "Wrong Syntax\nYou have the option to practice the following:\n\n"
                for i in attributes:
                    available_commands += i + '\n'
                available_commands += "\n\nprac <attribute> <amount>"
                Parser.color(available_commands)

            elif CommandProcessor().is_number(amount):
                prac_value = Player().loadplayer_pracs()[0]
                Player().modify_attr(attribute, prac_value, amount)

    def score(self):
        pdata = Player()
        name     = pdata.loadplayer_name()[0]
        Class    = pdata.loadplayer_class()[0]
        room     = pdata.loadplayer_room()[0]
        hp       = pdata.loadplayer_hp()[0]
        hp_max   = pdata.loadplayer_hpmax()[0]

        mana     = pdata.loadplayer_mana()[0]
        mana_max = pdata.loadplayer_manamax()[0]

        move     = pdata.loadplayer_move()[0]
        move_max = pdata.loadplayer_movemax()[0]

        strength = pdata.loadplayer_strength()[0]
        dex      = pdata.loadplayer_dex()[0]
        con      = pdata.loadplayer_con()[0]
        wis      = pdata.loadplayer_wis()[0]
        luck     = pdata.loadplayer_luck()[0]

        level    = pdata.loadplayer_level()[0]
        tnl      = pdata.loadplayer_tnl()[0]
        exp      = pdata.loadplayer_exp()[0]
        prac     = pdata.loadplayer_pracs()[0]

        room_title = Rooms().room_title(room)[0][0]
        score_string =\
            "\nName: |W%s|M (|R%s|M)|x\t\t\tRoom: |G%s |W/ |RID: |G%s|x\n"\
            "\nLevel: |W%s\n"\
            "|GStr:   |W%s\t\t\t\t|RHP:   |W%s / %s\n"\
            "|GDex:   |W%s\t\t\t\t|BMana: |W%s / %s\n"\
            "|GCon:   |W%s\t\t\t\t|YMove: |W%s / %s\n"\
            "|GWis:   |W%s\n"\
            "|GLuck:  |W%s\n\n"\
            "\t|GTNL: |W%s |GExp: |W%s  \t|GPracs: |W%s\n\n"\
            "|GEquipped:|x\n"\
            % (name.capitalize(),Class.capitalize(), room_title, room, level, strength,hp, hp_max,
            dex, mana, mana_max,
            con, move, move_max,
            wis, luck,exp, tnl, prac)
        Parser.color(score_string)

    def look_at(self, string):
        # load all mobs, items into room
        all_mobs = Rooms().load_allmobs_inroom(Player().loadplayer_room(), element_data=1)
        # all_items = Rooms().load_allitems_inroom()

        # Cross check if key word of each mob matches string
        all_mobs = SqlDataEdit.sqltext_to_list(all_mobs)

        found_mobs = []
        for mob in all_mobs:
            if mob.startswith(string[0]):
                # Display long description of whatever it found
                found_mobs.append(mob)

        if len(found_mobs) > 0:
            target_mob = found_mobs[0]
            Parser.color(Mobs().mob_getlong(target_mob))

        elif len(found_mobs) > 1:
            print "Narrow your search!"
        else:
            Parser.color("Couldn't find that")


    def begin_fight(self, mob_name):
        Fight().fight(mob_name)




# CommandProcessor().begin_fight(['cat'])