from pyhamtools.consts import LookupConventions as const


def freq_to_band(freq):
    """converts a Frequency [kHz] into the band and mode according to the IARU bandplan

        Args:
            frequency (float): Frequency in kHz

        Returns:
            dict: Dictionary containing the band (int) and mode (str)

        Raises:
            KeyError: Wrong frequency or out of band

        Example:
           The following example converts the frequency *14005.3 kHz* into band and mode.

           >>> from pyhamtools.utils import freq_to_band
           >>> print freq_to_band(14005.3)
           {
                'band': 20,
                'mode': CW
           }

        Note:

            Modes are:

                - CW
                - USB
                - LSB
                - DIGITAL

    """

    band = None
    mode = None
    if ((freq >= 135) and (freq <= 138)):
        band = 2190
        mode = const.CW
    elif ((freq >= 1800) and (freq <= 2000)):
        band = 160
        if ((freq >= 1800) and (freq < 1838)):
            mode = const.CW
        elif ((freq >= 1838) and (freq < 1840)):
            mode = const.DIGITAL
        elif (freq == 1840):
            mode = const.DIGITAL #FT8
        elif ((freq > 1840) and (freq < 2000)):
            mode = const.LSB
    elif ((freq >= 3500) and (freq <= 4000)):
        band = 80
        if ((freq >= 3500) and (freq < 3573)):
            mode = const.CW
        elif (freq == 3573):
            mode = const.DIGITAL #FT8
        elif ((freq > 3573) and (freq < 3580)):
            mode = const.CW
        elif ((freq >= 3580) and (freq < 3600)):
            mode = const.DIGITAL
        elif ((freq >= 3600) and (freq < 4000)):
            mode = const.LSB
    elif ((freq >= 5000) and (freq <= 5500)):
        band = 60
    elif ((freq >= 7000) and (freq <= 7300)):
        band = 40
        if ((freq >= 7000) and (freq < 7040)):
            mode = const.CW
        elif ((freq >= 7040) and (freq < 7050)):
            mode = const.DIGITAL
        elif ((freq >= 7050) and (freq < 7074)):
            mode = const.LSB
        elif (freq == 7074):
            mode = const.DIGITAL #FT8
        elif ((freq > 7074) and (freq < 7300)):
            mode = const.LSB
    elif ((freq >= 10100) and (freq <= 10150)):
        band = 30
        if ((freq >= 10100) and (freq < 10136)):
            mode = const.CW
        elif (freq == 10136):
            mode = const.DIGITAL #FT8
        elif ((freq > 10136) and (freq < 10140)):
            mode = const.CW
        elif ((freq >= 10140) and (freq < 10150)):
            mode = const.DIGITAL
    elif ((freq >= 14000) and (freq <= 14350)):
        band = 20
        if ((freq >= 14000) and (freq < 14070)):
            mode = const.CW
        elif ((freq >= 14070) and (freq < 14074)):
            mode = const.DIGITAL
        elif (freq == 14074):
            mode = const.DIGITAL #FT8
        elif ((freq > 14074) and (freq < 14099)):
            mode = const.DIGITAL
        elif ((freq >= 14100) and (freq < 14350)):
            mode = const.USB
    elif ((freq >= 18068) and (freq <= 18268)):
        band = 17
        if ((freq >= 18068) and (freq < 18095)):
            mode = const.CW
        elif ((freq >= 18095) and (freq < 18100)):
            mode = const.DIGITAL
        elif (freq == 18100):
            mode = const.DIGITAL #FT8
        elif ((freq > 18100) and (freq < 18110)):
            mode = const.DIGITAL
        elif ((freq >= 18110) and (freq < 18268)):