"""
    types.state
    ~~~~~~~~~~~

    schematics united states, state type
"""

import us

from schematics.exceptions import ConversionError
from schematics.types import BaseType


class Type(BaseType):
    """ U.S. state field with validation """

    MESSAGES = {
        'convert': 'invalid U.S. state'
    }

    def to_native(self, value, context=None):
        """ Schematics deserializer override

        We return a us.states.State object so the abbreviation
        or long name can be trivially accessed. Additionally,
        some geo type ops are available.

        TIP: The us.states library expects unicode so we
             must ensure value is encoded properly.

        :return: us.states.State
        """

        if isinstance(value, us.states.State):
            return value

        try:
            state = us.states.lookup(unicode(value))
            if not state:
                raise TypeError
            return state
        except TypeError:
            raise ConversionError(self.messages['convert'])

    def to_primitive(self, value, context=None):
        """ Schematics serializer override

        By default we convert to a str & use the states long name.

        :return: str
        """

        if value:
            return str(value)
        return value
