""" Catalog utils """
import logging

from catalog.constants import (
    OUTSTATE,
    INSTATE,
    FOREIGN,
    MAINE,
    HANCOCK,
    UNITED_STATES,
    INCOUNTY,
)
from catalog.models import ZipCode

from django.contrib.localflavor.us.us_states import STATE_CHOICES


LOG = logging.getLogger(__name__)


def subscription_type(the_zip, state, country):
    """ For a zip code, looks up the state and country to determine the
    subscription type

    :returns: INCOUNTY, INSTATE, OUTSTATE, FOREIGN
    """
    LOG.debug("Subscription type: {0}, {1}, {2}".format(the_zip, state, country))
    if country == UNITED_STATES:
        try:
            zipcode = ZipCode.objects.get(zipcode=the_zip)
            if zipcode.county == HANCOCK:
                return INCOUNTY

            return _sub_by_state(zipcode.state)
        except ZipCode.DoesNotExist:
            return _sub_by_state(state)
    else:
        return FOREIGN


def get_type(
    postal_code,
    region,
    country_code,
    postal_code2=None,
    region2=None,
    country_code2=None,
):
    """ get the type, only use the second address if there's values """
    if country_code2 and region2:
        shipping_type1 = subscription_type(postal_code, region, country_code)
        shipping_type2 = subscription_type(postal_code2, region2, country_code2)

        if _type_index(shipping_type1) > _type_index(shipping_type2):
            return shipping_type1
        else:
            return shipping_type2
    else:
        return subscription_type(postal_code, region, country_code)


def get_type_by_profile(profile, shipping_type=None):
    """ For a profile, return the subscription type and first class option.

    type is In-State, Out of State, or Foreign
    first class is a boolean value, true if its an option

    """

    def _address_not_empty(address):
        """ is address not empty """
        return (
            address
            and address.postal_code
            and address.region
            and address.postal_code != ""
        )

    if profile:
        shipping_type = subscription_type(
            profile.postal_code, profile.region, profile.country
        )

        address = profile.secondary_address
        if _address_not_empty(address):
            secondary_type = subscription_type(
                address.postal_code, address.region, address.country
            )

            if _type_index(secondary_type) > _type_index(shipping_type):
                shipping_type = secondary_type

    return shipping_type


def _type_index(value):
    """ type index """
    return [INCOUNTY, INSTATE, OUTSTATE, FOREIGN].index(value)


def _is_maine(state):
    """ Is the state maine? """
    return state.lower() == MAINE.lower()


def _is_us_state(my_state):
    """ is a us state? """
    states = [state.lower() for state, state_string in STATE_CHOICES]
    return my_state.lower() in states


def _sub_by_state(state):
    """ For a state, determines the subscription type

    :returns: INSTATE, OUTSTATE
    """
    if _is_maine(state):
        return INSTATE

    if _is_us_state(state):
        return OUTSTATE
