# Game imports 
from utils.serializable import Serializable
from utils.item import Item

# Native imports
import uuid

class Entity(Serializable):
    def __init__(self, name:str, strength:int, dexterity:int, intelligence:int, wisdom:int, charisma:int, hp:int, armor:int, speed:int, equipped_item:Item):
        self.id = str(uuid.uuid4())
        self.name = name
        self.strength = 20
        self.dexterity = 20
        self.intelligence = 20
        self.wisdom = 20
        self.charisma = 20
        self.hp = 20
        self.armor = 100
        self.speed = 100

        self.set_strength(strength)
        self.set_dexterity(dexterity)
        self.set_intelligence(intelligence)
        self.set_wisdom(wisdom)
        self.set_charisma(charisma)
        self.set_hp(hp)
        self.set_armor(armor)
        self.set_speed(speed)

        self.equipped_item = None

        if equipped_item:
            self.set_equipped_item(equipped_item)

    def clamp(self, valeur, min_val, max_val):
        return max(min_val, min(valeur, max_val))

    def get_id(self):
        return self.id
    
    def get_strength(self):
        if self.equipped_item and "strength" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.strength + self.equipped_item.stat_modifier["strength"], 0, 20)
        return self.clamp(self.strength, 0, 20)
    
    def get_dexterity(self):
        if self.equipped_item and "dexterity" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.dexterity + self.equipped_item.stat_modifier["dexterity"], 0, 20)
        return self.clamp(self.dexterity, 0, 20)
    
    def get_intelligence(self):
        if self.equipped_item and "intelligence" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.intelligence + self.equipped_item.stat_modifier["intelligence"], 0, 20)
        return self.clamp(self.intelligence, 0, 20)
    
    def get_wisdom(self):
        if self.equipped_item and "wisdom" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.wisdom + self.equipped_item.stat_modifier["wisdom"], 0, 20)
        return self.clamp(self.wisdom, 0, 20)
    
    def get_charisma(self):
        if self.equipped_item and "charisma" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.charisma + self.equipped_item.stat_modifier["charisma"], 0, 20)
        return self.clamp(self.charisma, 0, 20)
    
    def get_hp(self):
        if self.equipped_item and "hp" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.hp + self.equipped_item.stat_modifier["hp"], 0, 40)
        return self.clamp(self.hp, 0, 100)
    
    def get_armor(self):
        if self.equipped_item and "armor" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.armor + self.equipped_item.stat_modifier["armor"], 0, 100)
        return self.clamp(self.armor, 0, 100)
    
    def get_speed(self):
        if self.equipped_item and "speed" in self.equipped_item.stat_modifier.keys():
            return self.clamp(self.speed + self.equipped_item.stat_modifier["speed"], 0, 100)
        return self.clamp(self.speed, 0, 100)
    
    
    def get_equipped_item(self):
        return self.equipped_item
    
    def set_strength(self, value:int):
        self.strength =  self.clamp(value, 0, 20)
    
    def set_dexterity(self, value:int):
        self.dexterity = self.clamp(value, 0, 20)

    def set_intelligence(self, value:int):
        self.intelligence = self.clamp(value, 0, 20)

    def set_wisdom(self, value:int):
        self.wisdom = self.clamp(value, 0, 20)

    def set_charisma(self, value:int):
        self.charisma = self.clamp(value, 0, 20)

    def set_hp(self, value:int):
        self.hp = self.clamp(value, 0, 40)
    
    def set_armor(self, value:int):
        self.armor = self.clamp(value, 0, 100)

    def set_speed(self, value:int):
        self.speed = self.clamp(value, 0, 100)

    def set_equipped_item(self, item:Item):
        self.equipped_item = item

    def deal_damage(self, dmg_amount:int):
        current_hp = self.get_hp()
        self.set_hp(current_hp - dmg_amount)
