#!/usr/bin/env python3

"""
Extensive database of location and timezone data for nearly every airport and landing strip in the world.
"""
from __future__ import annotations

import csv
import sys
from pathlib import Path
from typing import Dict

if sys.version_info >= (3, 8):
    from typing import Literal, TypedDict
else:
    try:
        from typing_extensions import Literal, TypedDict
    except ModuleNotFoundError:
        pass


__project_name__ = __package__
# Release numbering follows the release date
__version__ = '20221017'
__min_python_version__ = (3, 7)
__author__ = 'Mike Borsetti <mike@borsetti.com>'
__copyright__ = 'Copyright 2020- Mike Borsetti'
__license__ = 'MIT'
__url__ = f'https://github.com/mborsetti/{__project_name__}'

if 'TypedDict' in globals():
    Airports = TypedDict(
        'Airports',
        {
            'icao': str,
            'iata': str,
            'name': str,
            'city': str,
            'subd': str,
            'country': str,
            'elevation': float,
            'lat': float,
            'lon': float,
            'tz': str,
        },
    )

if 'Literal' in globals():
    CodeType = Literal['ICAO', 'IATA']
else:
    CodeType = True  # type: ignore[misc]


def load(code_type: CodeType = 'ICAO') -> Dict[str, 'Airports']:  # Version 3.8 and greater this should be a Literal
    """Loads airport data into a dict

    :param code_type: optional argument defining the key in the dictionary: 'ICAO' (default if omitted) or 'IATA'
    :type code_type: str
    :return: a dict of dicts, each entry having the following keys:
        'icao': ICAO 4-letter Location Indicator or 4-alphanumeric FAA/TC LID
        'iata': IATA 3-letter Location Code or an empty string
        'name': Official name (latin script)
        'city': City
        'subd': Subdivision (e.g. state, province, region, etc.)
        'country': ISO 3166-1 alpha 2-code (plus 'XK' for Kosovo)
        'elevation': MSL elevation (the highest point of the landing area) in feet
        'lat': Latitude (decimal)
        'lon': Longitude (decimal)
        'tz': Timezone expressed as a tz database name (IANA-compliant) or empty string for country 'AQ' (Antarctica).
            Originally sourced from [TimeZoneDB](https://timezonedb.com)
    :rtype: dict
    """
    # with open(os.path.join(dir, 'airports.json'), encoding='utf8') as f:
    #     airports = json.load(f)
    # if code_type.lower() == 'icao':
    #     return airports
    # else:
    #     return {airport['iata']: airport for airport in dict(airports).values() if airport['iata']}
    #
    #
    this_dir = Path(__file__).parent
    key = 'icao' if code_type.lower() == 'icao' else 'iata'
    airports: Dict[str, Airports] = {}
    with this_dir.joinpath('airports.csv').open(encoding='utf8') as f:
        reader = csv.DictReader(f, quoting=csv.QUOTE_NONNUMERIC)
        for row in reader:
            airports[row[key]] = row  # type: ignore[assignment]
    airports.pop('', None)
    return airports


# Python 3.9 code used to save the dict to CSV:
# with open('airports.csv', 'w', newline='') as f:
#     fieldnames = airports[list(airports.keys())[0]].keys()
#     writer = csv.DictWriter(f, fieldnames=fieldnames, quoting=csv.QUOTE_NONNUMERIC)
#     writer.writeheader()
#     for data in airports.values():
#         writer.writerow(data)
