from datetime import date
# Converts a latitude and longitude that are both in SXXI format (degrees, minutes, seconds)
#     into a single value in decimal degrees format
def formatLatLong(SXXI_latitude, SXXI_longitude):
    lat_degrees, lat_minutes, lat_seconds, lat_sign = [(SXXI_latitude[i:i+2]) for i in range(0, len(SXXI_latitude), 2)]
    long_degrees, long_minutes, long_seconds, long_sign = SXXI_longitude[:3], SXXI_longitude[3:5], SXXI_longitude[5:7], SXXI_longitude[7]
    if lat_sign == 'N':
        lat_sign = 1
    else:
        lat_sign = -1
    if long_sign == 'E':
        long_sign = 1
    else:
        long_sign = -1
    lat_degrees = lat_sign * DMSToDD(lat_degrees, lat_minutes, lat_seconds)
    long_degrees = long_sign * DMSToDD(long_degrees, long_minutes, long_seconds)
    return str(lat_degrees) + ',' + str(long_degrees)

# Converts a latitude or longitude from degrees, minutes, seconds into decimal degrees
def DMSToDD(degrees, minutes, seconds):
    return float(degrees) + (float(minutes) / 60) + (float(seconds) / 3600)

# Extracts the necessary bandwidth from the emission designator in Hz
def extractBandwidthFromEMS(emissionDesignator):
    before_point = ''
    after_point = ''
    unit = None
    for char in emissionDesignator:
        if unit is None:
            if char.isnumeric():
                before_point += char
            else:
                unit = char
        else:
            if char.isnumeric():
                after_point += char
            else:
                break
    if unit == 'N':
        return None
    else:
        if after_point == '':
            value = float(before_point)
        else:
            value = float(before_point + '.' + after_point)
        return convertSXXIFrequency(value, unit, 'N')
        
# Converts a frequency in SXXI format into kHz. The purpose of this function is to create a sortable
#     format for frequencies.
# Future versions will include formatting options.
def formatFrequency(SXXI_frequency):
    unit = SXXI_frequency[0]
    quantity = SXXI_frequency[1:]
    return str(convertSXXIFrequency(quantity, unit, 'N'))

# Applies the formatFrequency function to each end of a band in SXXI format (UXXX-YYY)
#     and returns a tuple of bounds
def formatFrequencyBand(SXXI_frequency_band):
    unit = SXXI_frequency_band[0].lower()
    lower_end, upper_end = map(formatFrequency, list(map(lambda x: unit + x, SXXI_frequency_band[1:].split('-'))))
    return (lower_end, upper_end)

# Converts a date in GMF format into standard dd/mm/yyyy format.
#    60 is chosen as an arbitrary cut-off for assuming the century
def formatGMFDate(GMF_date):
    year = GMF_date[:2]
    month = GMF_date[2:4]
    day = GMF_date[4:]
    if int(year) > 60:
        year = '19' + year
    else:
        year = '20' + year
    return date(int(year), int(month), int(day))

# Converts a date in SFAF format into standard dd/mm/yyyy format.
def formatSFAFDate(SFAF_date):
    year = SFAF_date[:4]
    month = SFAF_date[4:6]
    day = SFAF_date[6:]
    return date(int(year), int(month), int(day))

# Converts a power in SXXI format into Watts.
def formatPower(SXXI_power):
    if SXXI_power is None or SXXI_power == '':
        return 0
    unit = SXXI_power[0]
    quantity = SXXI_power[1:]
    return convertSXXIPower(quantity, unit, 'N')

# Converts a value from one SI prefix to another return the converted value as a float
#     'N' is used for none
def convertPrefix(value, currentPrefix, desiredPrefix):
    match currentPrefix:
        case 'N':
            conversion = 0
        case 'k':
            conversion = 3
        case 'M':
            conversion = 6
        case 'G':
            conversion = 9
        case 'T':
            conversion = 12
    match desiredPrefix:
        case 'N':
            division = 0
        case 'k':
            division = 3
        case 'M':
            division = 6
        case 'G':
            division = 9
        case 'T':
            division = 12
    return float(value) * (10 ** (conversion - division))

# Helper to convert SXXI power prefixes into standard SI prefixes
def convertSXXIPowerPrefix(prefix):
    match prefix:
        case 'W':
            return 'N'
        case 'K':
            return 'k'
        case _:
            return prefix

# Converts SXXI powers from one SI prefix to another returns float
def convertSXXIPower(value, currentPrefix, desiredPrefix):
    return convertPrefix(value, convertSXXIPowerPrefix(currentPrefix), desiredPrefix)

# Helper to convert SXXI frequency prefixes into standard SI prefixes
def convertSXXIFrequencyPrefix(prefix):
    match prefix:
        case 'H':
            return 'N'
        case 'K':
            return 'k'
        case _:
            return prefix

# Converts SXXI frequencies from one SI prefix to another returns float
def convertSXXIFrequency(value, currentPrefix, desiredPrefix):
    return convertPrefix(value, convertSXXIFrequencyPrefix(currentPrefix), desiredPrefix)

# Removes the leading and trailing "" or ''
def unquote(string):
    if string[0] != '"' and string[0] != "'":
        return string
    if string[len(string)-1] != '"' and string[len(string)-1] != "'":
        return string
    if string[0] != string[len(string)-1]:
        return string
    return string[1:len(string)-1]

# Detects if final character of a string is non-numeric and returns it if so
def detectSuffix(string):
    suffix = string[len(string)-1]
    if suffix.isnumeric():
        return None
    else:
        return suffix