from ttfrog.db.schema.constants import DamageType
from ttfrog.db.schema.prototypes import Armor, BaseItem, Rarity, Shield, Weapon

from .base import Parser

DAMAGE_TYPES = {
    "P": DamageType.piercing,
    "S": DamageType.slashing,
    "B": DamageType.bludgeoning,
    "R": DamageType.radiant,
    "Y": DamageType.psychic,
    "N": DamageType.necrotic,
    "C": DamageType.cold,
    "F": DamageType.fire,
    "T": DamageType.thunder,
    "": None,
}


class ItemParser(Parser):
    @property
    def object_type(self):
        objtype = BaseItem
        if "type" not in self.data:
            return objtype

        if self.data["type"][:2] in ["LA", "MA", "HA"]:
            objtype = Armor
        elif self.data["type"][0] in ["R", "M"]:
            objtype = Weapon
        elif self.data["type"][0] == "S":
            objtype = Shield
        else:
            objtype = BaseItem
        return objtype

    @property
    def _properties(self):
        return self.data.get("property", [])

    @property
    def damage_die(self):
        return self.data.get("dmg1", None)

    @property
    def damage_die_two_handed(self):
        return self.data.get("dmg2", None)

    @property
    def damage_type(self):
        return DAMAGE_TYPES[self.data.get("dmgType", "")]

    @property
    def attack_range(self):
        rangeval = self.data.get("range", None)
        if not rangeval:
            return None
        return rangeval.split("/")[0]

    @property
    def attack_range_long(self):
        rangeval = self.data.get("range", None)
        if not rangeval:
            return None
        try:
            return rangeval.split("/")[1]
        except IndexError:
            return None

    @property
    def martial(self):
        return self.data.get("weaponCategory", "") == "martial"

    @property
    def melee(self):
        return self.data.get("type", "") == "M"

    @property
    def ammunition(self):
        return self.data.get("type", "") == "A"

    @property
    def finesse(self):
        return "F" in self._properties

    @property
    def heavy(self):
        return "H" in self._properties

    @property
    def light(self):
        return "L" in self._properties

    @property
    def loading(self):
        return "RLD" in self._properties

    @property
    def loading(self):
        return "RLD" in self._properties

    @property
    def thrown(self):
        return "T" in self._properties

    @property
    def two_handed(self):
        return "2H" in self._properties

    @property
    def versatile(self):
        return "V" in self._properties

    @property
    def magical(self):
        return True if self.data.get("bonusWeapon", False) or self.requires_attunement else False

    @property
    def rarity(self):
        return (
            Rarity.Common
            if self.data.get("rarity", "none") == "none"
            else getattr(Rarity, self.data["rarity"].title(), None)
        )

    @property
    def armor_class(self):
        return self.data.get("ac", None)


BaseitemParser = ItemParser
