from typing import Optional

from pydantic.main import BaseModel

from server.encoding import encode_value, encode_color
from server.model.color import Color
from server.model.value import Value


class Card(BaseModel):
    value: Value
    color: Optional[Color] = None

    class Config:
        json_encoders = {
            Value: encode_value,
            Color: encode_color,
        }

    def __cmp__(self, other: "Card"):
        my = self.score()
        their = other.score()
        return (my > their) - (my < their)

    def __lt__(self, other: "Card"):
        return self.score() < other.score()

    def __gt__(self, other: "Card"):
        return self.score() > other.score()

    def __eq__(self, other: "Card"):
        return self.score() == other.score()

    def __str__(self) -> str:
        return f"{self.value.name} of {self.color.value if self.color else '?'}"

    def __hash__(self) -> int:
        return hash(self.value) + 100 * hash(self.color)

    def score(self) -> int:
        return int(self.value.value)


def lowest_value_and_rest():
    lowValue: Value = Value.Two
    otherValues = list(Value)
    otherValues.remove(lowValue)

    return lowValue, otherValues
