from sqlalchemy import Column, Integer

from components.component import Component, component_repr


class Attributes(Component):
    __tablename__ = 'attributes'
    id = Column(Integer, primary_key=True)
    entity = Column(Integer, unique=True, index=True, nullable=False)
    hp = Column(Integer, default=10, nullable=False)
    max_hp = Column(Integer, default=10, nullable=False)

    defense = Column(Integer, default=10, nullable=False)
    strength = Column(Integer, default=10, nullable=False)
    constitution = Column(Integer, default=10, nullable=False)
    dexterity = Column(Integer, default=10, nullable=False)
    intelligence = Column(Integer, default=10, nullable=False)
    wisdom = Column(Integer, default=10, nullable=False)
    charisma = Column(Integer, default=10, nullable=False)

    @property
    def str_mod(self):
        return mod(self.strength)

    @property
    def con_mod(self):
        return mod(self.constitution)

    @property
    def dex_mod(self):
        return mod(self.dexterity)

    @property
    def int_mod(self):
        return mod(self.intelligence)

    @property
    def wis_mod(self):
        return mod(self.wisdom)

    @property
    def cha_mod(self):
        return mod(self.charisma)

    def __repr__(self):
        return component_repr(self)


def mod(attr):
    return attr / 2 - 5
