"""Provides general component classes."""

from sqlalchemy import Column, Float
from attrs_sqlalchemy import attrs_sqlalchemy
from .base import Base, ComponentMixin


@attrs_sqlalchemy
class Battery(Base, ComponentMixin):
    """
    A battery.

    current_charge and capacity work the same as hitpoints and max_hitpoints.
    Use obj.charge to get the actual charge."""
    __tablename__ = 'batteries'
    current_charge = Column(Float, nullable=True)
    capacity = Column(Float, nullable=False, default=20000.0)

    @property
    def charge(self):
        """Get the actual charge of this battery."""
        if self.current_charge is None:
            return self.capacity
        return self.current_charge

    @charge.setter
    def charge(self, value):
        """Set the current charge."""
        if value is None or value >= self.capacity:
            self.current_charge = None
        else:
            self.current_charge = value


@attrs_sqlalchemy
class Hull(Base, ComponentMixin):
    """A starship hull."""
    __tablename__ = 'hulls'
