from .race_class import RaceClass
from .race_grade import RaceGrade


class RaceLevel:
    def __init__(self, value: str | RaceGrade | RaceClass):
        if isinstance(value, RaceClass) and value.value == 1:
            raise ValueError("Class 1 race needs a specified grade")

        self.grade = (
            value
            if isinstance(value, RaceGrade)
            else RaceGrade(value if "G" in str(value) else None)
        )
        self.class_ = (
            value
            if isinstance(value, RaceClass)
            else RaceClass(1)
            if self.grade
            else RaceClass(value)
        )

    def __repr__(self):
        return f"<RaceLevel: {repr(self.grade) if self.grade else repr(self.class_)}>"

    def __str__(self):
        return f"({self.class_.value}) {self.grade if self.grade.value else ''}".strip()

    def __eq__(self, other):
        return self.grade == other.grade and self.class_ == other.class_

    def __gt__(self, other):
        return self.grade > other.grade or self.class_ > other.class_

    def __lt__(self, other):
        return self.grade < other.grade or self.class_ < other.class_
