"""All functions related to region and country handling in GEB.

Contains functions to set up donor countries for countries with missing data,
finding similar countries with data.

Contains several dictionaries to convert between different country coding systems.

"""

from __future__ import annotations

from typing import TYPE_CHECKING, Iterable

import geopandas as gpd
from pyproj import Geod

if TYPE_CHECKING:
    from ..data_catalog import NewDataCatalog

# Trade regions as used in GLOBIOM, but with minor modifications. See comments below for respective countries.
TRADE_REGIONS: dict[str, str] = {
    "ARG": "ArgentinaReg",
    "AUS": "AustraliaReg",
    "BRA": "BrazilReg",
    "CAN": "CanadaReg",
    "CHN": "ChinaReg",
    "CMR": "CongoBasin",
    "CAF": "CongoBasin",
    "COD": "CongoBasin",
    "COG": "CongoBasin",
    "GNQ": "CongoBasin",
    "GAB": "CongoBasin",
    "EST": "EU_Baltic",
    "LVA": "EU_Baltic",
    "LTU": "EU_Baltic",
    "BGR": "EU_CentralEast",
    "HRV": "EU_CentralEast",
    "CZE": "EU_CentralEast",
    "HUN": "EU_CentralEast",
    "POL": "EU_CentralEast",
    "ROU": "EU_CentralEast",
    "SVK": "EU_CentralEast",
    "SVN": "EU_CentralEast",
    "AUT": "EU_MidWest",
    "BEL": "EU_MidWest",
    "FRA": "EU_MidWest",
    "DEU": "EU_MidWest",
    "LUX": "EU_MidWest",
    "NLD": "EU_MidWest",
    "DNK": "EU_North",
    "FIN": "EU_North",
    "IRL": "EU_North",
    "SWE": "EU_North",
    "GBR": "EU_North",
    "CYP": "EU_South",
    "GRC": "EU_South",
    "ITA": "EU_South",
    "MLT": "EU_South",
    "PRT": "EU_South",
    "ESP": "EU_South",
    "ARM": "Former_USSR",
    "AZE": "Former_USSR",
    "BLR": "Former_USSR",
    "GEO": "Former_USSR",
    "KAZ": "Former_USSR",
    "KGZ": "Former_USSR",
    "MDA": "Former_USSR",
    "TJK": "Former_USSR",
    "TKM": "Former_USSR",
    "UZB": "Former_USSR",
    "IND": "IndiaReg",
    "IDN": "IndonesiaReg",
    "JPN": "JapanReg",
    "MYS": "MalaysiaReg",
    "MEX": "MexicoReg",
    "BHR": "MiddleEast",
    "IRN": "MiddleEast",
    "IRQ": "MiddleEast",
    "ISR": "MiddleEast",
    "JOR": "MiddleEast",
    "KWT": "MiddleEast",
    "LBN": "MiddleEast",
    "OMN": "MiddleEast",
    "PSE": "MiddleEast",
    "QAT": "MiddleEast",
    "SAU": "MiddleEast",
    "SYR": "MiddleEast",
    "ARE": "MiddleEast",
    "YEM": "MiddleEast",
    "NZL": "NewZealandReg",
    "DZA": "NorthernAf",
    "EGY": "NorthernAf",
    "LBY": "NorthernAf",
    "MAR": "NorthernAf",
    "TUN": "NorthernAf",
    "ESH": "NorthernAf",
    "FJI": "Pacific_Islands",
    "PYF": "Pacific_Islands",
    "NCL": "Pacific_Islands",
    "PNG": "Pacific_Islands",
    "WSM": "Pacific_Islands",
    "SLB": "Pacific_Islands",
    "VUT": "Pacific_Islands",
    "BHS": "RCAM",
    "BLZ": "RCAM",
    "CRI": "RCAM",
    "CUB": "RCAM",
    "DOM": "RCAM",
    "SLV": "RCAM",
    "GLP": "RCAM",
    "GTM": "RCAM",
    "HTI": "RCAM",
    "HND": "RCAM",
    "JAM": "RCAM",
    "NIC": "RCAM",
    "PAN": "RCAM",
    "TTO": "RCAM",
    "ALB": "RCEU",
    "BIH": "RCEU",
    "MKD": "RCEU",
    "SRB": "RCEU",
    "GRL": "ROWE",
    "ISL": "ROWE",
    "NOR": "ROWE",
    "CHE": "EU_MidWest",  # Switzerland is grouped with EU_MidWest
    "LIE": "EU_MidWest",  # Liechtenstein is grouped with EU_MidWest
    "BOL": "RSAM",
    "CHL": "RSAM",
    "COL": "RSAM",
    "ECU": "RSAM",
    "FLK": "RSAM",
    "GUF": "RSAM",
    "GUY": "RSAM",
    "PRY": "RSAM",
    "PER": "RSAM",
    "SUR": "RSAM",
    "URY": "RSAM",
    "VEN": "RSAM",
    "AFG": "RSAS",
    "BGD": "RSAS",
    "BTN": "RSAS",
    "NPL": "RSAS",
    "PAK": "RSAS",
    "LKA": "RSAS",
    "BRN": "RSEA_OPA",
    "MMR": "RSEA_OPA",
    "PHL": "RSEA_OPA",
    "SGP": "RSEA_OPA",
    "THA": "RSEA_OPA",
    "TLS": "RSEA_OPA",
    "KHM": "RSEA_PAC",
    "PRK": "RSEA_PAC",
    "LAO": "RSEA_PAC",
    "MNG": "RSEA_PAC",
    "VNM": "RSEA_PAC",
    "RUS": "RussiaReg",
    "ZAF": "SouthAfrReg",
    "KOR": "SouthKorea",
    "BDI": "EasternAf",
    "ETH": "EasternAf",
    "KEN": "EasternAf",
    "RWA": "EasternAf",
    "TZA": "EasternAf",
    "UGA": "EasternAf",
    "AGO": "SouthernAf",
    "BWA": "SouthernAf",
    "COM": "SouthernAf",
    "LSO": "SouthernAf",
    "MDG": "SouthernAf",
    "MWI": "SouthernAf",
    "MUS": "SouthernAf",
    "MOZ": "SouthernAf",
    "NAM": "SouthernAf",
    "REU": "SouthernAf",
    "SWZ": "SouthernAf",
    "ZMB": "SouthernAf",
    "ZWE": "SouthernAf",
    "BEN": "WesternAf",
    "BFA": "WesternAf",
    "CPV": "WesternAf",
    "TCD": "WesternAf",
    "CIV": "WesternAf",
    "DJI": "WesternAf",
    "ERI": "WesternAf",
    "GMB": "WesternAf",
    "GHA": "WesternAf",
    "GIN": "WesternAf",
    "GNB": "WesternAf",
    "LBR": "WesternAf",
    "MLI": "WesternAf",
    "MRT": "WesternAf",
    "NER": "WesternAf",
    "NGA": "WesternAf",
    "SEN": "WesternAf",
    "SLE": "WesternAf",
    "SOM": "WesternAf",
    "SDN": "WesternAf",
    "TGO": "WesternAf",
    "TUR": "TurkeyReg",
    "UKR": "UkraineReg",
    "PRI": "USAReg",
    "USA": "USAReg",
}


def get_geodesic_distance(
    geod: Geod, geom1: gpd.GeoSeries, geom2: gpd.GeoSeries
) -> float:
    """Calculate geodesic distance between two geometries.

    Args:
        geod: Geod object for geodesic calculations.
        geom1: Geometry of the first country.
        geom2: Geometry of the second country.

    Returns:
        Geodesic distance in meters between the centroids of the two geometries.
    """
    # Using centroids for point-to-point geodesic distance
    p1 = geom1.centroid
    p2 = geom2.centroid
    _, _, distance = geod.inv(p1.x, p1.y, p2.x, p2.y)
    return distance


def setup_donor_countries(
    data_catalog: NewDataCatalog,
    global_countries: gpd.GeoDataFrame,
    countries_with_data: Iterable[str],
    alternative_countries: list[str],
) -> dict[str, str]:
    """Sets up the donor countries for GEB.

    Args:
        data_catalog: The data catalog instance.
        global_countries: GeoDataFrame with global countries geometries.
        countries_with_data: Countries (ISO3 codes) that have data available.
        alternative_countries: Alternative countries to consider as donors, e.g. trade regions inside the model domain.

    Returns:
        A dictionary with the keys representing the country with missing data, and the values the country that is selected as donor.
    """
    # load HDI index
    dev_index = data_catalog.fetch("un_hdi").read()

    dev_index.rename(columns={"Human Development Index": "HDI"}, inplace=True)
    dev_index = (
        dev_index.groupby("Code", as_index=False)["HDI"].mean().set_index("Code")
    )  # calculate mean HDI for each country

    # find potential donors
    potential_donors = global_countries.loc[
        global_countries.index.isin(countries_with_data)
    ]
    potential_donors = potential_donors[
        potential_donors.index.isin(dev_index.index)
    ]  # delete potential donors that do not have HDI data

    potential_donors["HDI"] = dev_index.loc[potential_donors.index, "HDI"].values

    region_countries = alternative_countries.copy()

    # find countries in model domain that do not have data
    countries_without_data = list(set(region_countries) - set(countries_with_data))

    # empty dictionary to store farm size donor countries
    donor_country_dict = {}
    for country in countries_without_data:
        # calculate the HDI of the target country
        if country not in dev_index.index:  # if the country does not have HDI data
            # take the closest country with HDI data (HDI donor)
            region_countries_with_hdi = global_countries.loc[
                global_countries.index.isin(region_countries)
                & global_countries.index.isin(dev_index.index)
            ]
            current_country_geometry = global_countries.loc[country].geometry
            geod = Geod(ellps="WGS84")

            distances = region_countries_with_hdi.geometry.apply(
                lambda x: get_geodesic_distance(
                    geod=geod, geom1=x, geom2=current_country_geometry
                )
            )
            distances = distances[
                distances > 0
            ]  # remove zero distances (self-distance)
            closest_country = region_countries_with_hdi.loc[distances.idxmin()].name

            print(
                f"Country {country} does not have HDI data available, as it is not an official UN country. Taking HDI from the closest country with HDI data: {closest_country}."
            )
            hdi = dev_index.loc[closest_country, "HDI"]

        else:
            hdi = dev_index.loc[country, "HDI"]

        donors = potential_donors.copy()

        donors["HDI_diff"] = (potential_donors["HDI"] - hdi).abs()

        # calculate distances from target country to donors
        donors = donors.sort_values("HDI_diff").head(10)

        current_country_geometry = global_countries.loc[country].geometry
        geod = Geod(ellps="WGS84")

        donors["distance"] = donors.geometry.apply(
            lambda x: get_geodesic_distance(geod, x, current_country_geometry)
        ).values

        # select the top 3 closest donors based on distance
        donor_country = (
            donors.sort_values("distance").head(1).index.item()
        )  # get the ISO3 code of the donor country
        donor_country_dict[country] = donor_country

    return donor_country_dict


M49_to_ISO3 = {
    4: "AFG",
    8: "ALB",
    10: "ATA",
    12: "DZA",
    16: "ASM",
    20: "AND",
    24: "AGO",
    28: "ATG",
    31: "AZE",
    32: "ARG",
    36: "AUS",
    40: "AUT",
    44: "BHS",
    48: "BHR",
    50: "BGD",
    51: "ARM",
    52: "BRB",
    56: "BEL",
    60: "BMU",
    64: "BTN",
    68: "BOL",
    70: "BIH",
    72: "BWA",
    74: "BVT",
    76: "BRA",
    84: "BLZ",
    86: "IOT",
    90: "SLB",
    92: "VGB",
    96: "BRN",
    100: "BGR",
    104: "MMR",
    108: "BDI",
    112: "BLR",
    116: "KHM",
    120: "CMR",
    124: "CAN",
    132: "CPV",
    136: "CYM",
    140: "CAF",
    144: "LKA",
    148: "TCD",
    152: "CHL",
    156: "CHN",
    162: "CXR",
    166: "CCK",
    170: "COL",
    174: "COM",
    175: "MYT",
    178: "COG",
    180: "COD",
    184: "COK",
    188: "CRI",
    191: "HRV",
    192: "CUB",
    196: "CYP",
    203: "CZE",
    204: "BEN",
    208: "DNK",
    212: "DMA",
    214: "DOM",
    218: "ECU",
    222: "SLV",
    226: "GNQ",
    231: "ETH",
    232: "ERI",
    233: "EST",
    234: "FRO",
    238: "FLK",
    239: "SGS",
    242: "FJI",
    246: "FIN",
    248: "ALA",
    250: "FRA",
    254: "GUF",
    258: "PYF",
    260: "ATF",
    262: "DJI",
    266: "GAB",
    268: "GEO",
    270: "GMB",
    275: "PSE",
    276: "DEU",
    288: "GHA",
    292: "GIB",
    296: "KIR",
    300: "GRC",
    304: "GRL",
    308: "GRD",
    312: "GLP",
    316: "GUM",
    320: "GTM",
    324: "GIN",
    328: "GUY",
    332: "HTI",
    334: "HMD",
    336: "VAT",
    340: "HND",
    344: "HKG",
    348: "HUN",
    352: "ISL",
    356: "IND",
    360: "IDN",
    364: "IRN",
    368: "IRQ",
    372: "IRL",
    376: "ISR",
    380: "ITA",
    384: "CIV",
    388: "JAM",
    392: "JPN",
    398: "KAZ",
    400: "JOR",
    404: "KEN",
    408: "PRK",
    410: "KOR",
    414: "KWT",
    417: "KGZ",
    418: "LAO",
    422: "LBN",
    426: "LSO",
    428: "LVA",
    430: "LBR",
    434: "LBY",
    438: "LIE",
    440: "LTU",
    442: "LUX",
    446: "MAC",
    450: "MDG",
    454: "MWI",
    458: "MYS",
    462: "MDV",
    466: "MLI",
    470: "MLT",
    474: "MTQ",
    478: "MRT",
    480: "MUS",
    484: "MEX",
    492: "MCO",
    496: "MNG",
    498: "MDA",
    499: "MNE",
    500: "MSR",
    504: "MAR",
    508: "MOZ",
    512: "OMN",
    516: "NAM",
    520: "NRU",
    524: "NPL",
    528: "NLD",
    531: "CUW",
    533: "ABW",
    534: "SXM",
    535: "BES",
    540: "NCL",
    548: "VUT",
    554: "NZL",
    558: "NIC",
    562: "NER",
    566: "NGA",
    570: "NIU",
    574: "NFK",
    578: "NOR",
    580: "MNP",
    581: "UMI",
    583: "FSM",
    584: "MHL",
    585: "PLW",
    586: "PAK",
    591: "PAN",
    598: "PNG",
    600: "PRY",
    604: "PER",
    608: "PHL",
    612: "PCN",
    616: "POL",
    620: "PRT",
    624: "GNB",
    626: "TLS",
    630: "PRI",
    634: "QAT",
    638: "REU",
    642: "ROU",
    643: "RUS",
    646: "RWA",
    652: "BLM",
    654: "SHN",
    659: "KNA",
    660: "AIA",
    662: "LCA",
    663: "MAF",
    666: "SPM",
    670: "VCT",
    674: "SMR",
    678: "STP",
    682: "SAU",
    686: "SEN",
    688: "SRB",
    690: "SYC",
    694: "SLE",
    702: "SGP",
    703: "SVK",
    704: "VNM",
    705: "SVN",
    706: "SOM",
    710: "ZAF",
    716: "ZWE",
    724: "ESP",
    728: "SSD",
    729: "SDN",
    732: "ESH",
    740: "SUR",
    744: "SJM",
    748: "SWZ",
    752: "SWE",
    756: "CHE",
    760: "SYR",
    762: "TJK",
    764: "THA",
    768: "TGO",
    772: "TKL",
    776: "TON",
    780: "TTO",
    784: "ARE",
    788: "TUN",
    792: "TUR",
    795: "TKM",
    796: "TCA",
    798: "TUV",
    800: "UGA",
    804: "UKR",
    807: "MKD",
    818: "EGY",
    826: "GBR",
    831: "GGY",
    832: "JEY",
    833: "IMN",
    834: "TZA",
    840: "USA",
    850: "VIR",
    854: "BFA",
    858: "URY",
    860: "UZB",
    862: "VEN",
    876: "WLF",
    882: "WSM",
    887: "YEM",
    894: "ZMB",
}


SUPERWELL_NAME_TO_ISO3 = {
    "Afghanistan": "AFG",
    "Albania": "ALB",
    "Algeria": "DZA",
    "Andorra": "AND",
    "Angola": "AGO",
    "Argentina": "ARG",
    "Armenia": "ARM",
    "Australia": "AUS",
    "Austria": "AUT",
    "Azerbaijan": "AZE",
    "Bahamas": "BHS",
    "Bahrain": "BHR",
    "Bangladesh": "BGD",
    "Belarus": "BLR",
    "Belgium": "BEL",
    "Belize": "BLZ",
    "Benin": "BEN",
    "Bhutan": "BTN",
    "Bolivia": "BOL",
    "Bosnia and Herzegovina": "BIH",
    "Botswana": "BWA",
    "Brazil": "BRA",
    "Brunei Darussalam": "BRN",
    "Bulgaria": "BGR",
    "Burkina Faso": "BFA",
    "Burundi": "BDI",
    "Cambodia": "KHM",
    "Cameroon": "CMR",
    "Canada": "CAN",
    "Central African Republic": "CAF",
    "Chad": "TCD",
    "Chile": "CHL",
    "China": "CHN",
    "Colombia": "COL",
    "Congo": "COG",
    "Costa Rica": "CRI",
    "Croatia": "HRV",
    "Cuba": "CUB",
    "Cyprus": "CYP",
    "Czech Republic": "CZE",  # Note: The official name is now Czechia
    "Democratic Republic of the Congo": "COD",
    "Denmark": "DNK",
    "Djibouti": "DJI",
    "Dominican Republic": "DOM",
    "Ecuador": "ECU",
    "Egypt": "EGY",
    "El Salvador": "SLV",
    "Equatorial Guinea": "GNQ",
    "Eritrea": "ERI",
    "Estonia": "EST",
    "Ethiopia": "ETH",
    "Finland": "FIN",
    "France": "FRA",
    "Gabon": "GAB",
    "Gambia": "GMB",
    "Georgia": "GEO",
    "Germany": "DEU",
    "Ghana": "GHA",
    "Gibraltar": "GIB",
    "Greece": "GRC",
    "Greenland": "GRL",
    "Guatemala": "GTM",
    "Guinea": "GIN",
    "Guinea-Bissau": "GNB",
    "Guyana": "GUY",
    "Haiti": "HTI",
    "Holy See": "VAT",
    "Honduras": "HND",
    "Hungary": "HUN",
    "Iceland": "ISL",
    "India": "IND",
    "Indonesia": "IDN",
    "Iran": "IRN",
    "Iraq": "IRQ",
    "Ireland": "IRL",
    "Israel": "ISR",
    "Italy": "ITA",
    "Jamaica": "JAM",
    "Japan": "JPN",
    "Jordan": "JOR",
    "Kazakhstan": "KAZ",
    "Kenya": "KEN",
    "Kosovo": "XKX",
    "Kuwait": "KWT",
    "Kyrgyzstan": "KGZ",
    "Laos": "LAO",
    "Latvia": "LVA",
    "Lebanon": "LBN",
    "Lesotho": "LSO",
    "Liberia": "LBR",
    "Libya": "LBY",
    "Liechtenstein": "LIE",
    "Lithuania": "LTU",
    "Luxembourg": "LUX",
    "Madagascar": "MDG",
    "Malawi": "MWI",
    "Malaysia": "MYS",
    "Mali": "MLI",
    "Marshall Islands": "MHL",
    "Mauritania": "MRT",
    "Mexico": "MEX",
    "Moldova": "MDA",
    "Monaco": "MCO",
    "Mongolia": "MNG",
    "Montenegro": "MNE",
    "Morocco": "MAR",
    "Mozambique": "MOZ",
    "Myanmar": "MMR",
    "Namibia": "NAM",
    "Nepal": "NPL",
    "Netherlands": "NLD",
    "New Zealand": "NZL",
    "Nicaragua": "NIC",
    "Niger": "NER",
    "Nigeria": "NGA",
    "North Korea": "PRK",
    "Norway": "NOR",
    "Oman": "OMN",
    "Pakistan": "PAK",
    "Palestinian Territory": "PSE",
    "Panama": "PAN",
    "Papua New Guinea": "PNG",
    "Paraguay": "PRY",
    "Peru": "PER",
    "Philippines": "PHL",
    "Poland": "POL",
    "Portugal": "PRT",
    "Qatar": "QAT",
    "Romania": "ROU",
    "Russian Federation": "RUS",
    "Rwanda": "RWA",
    "San Marino": "SMR",
    "Saudi Arabia": "SAU",
    "Senegal": "SEN",
    "Serbia": "SRB",
    "Seychelles": "SYC",
    "Sierra Leone": "SLE",
    "Singapore": "SGP",
    "Slovakia": "SVK",
    "Slovenia": "SVN",
    "Somalia": "SOM",
    "South Africa": "ZAF",
    "South Korea": "KOR",
    "Spain": "ESP",
    "Sri Lanka": "LKA",
    "Suriname": "SUR",
    "Swaziland": "SWZ",  # Note: Now officially known as Eswatini
    "Sweden": "SWE",
    "Switzerland": "CHE",
    "Syria": "SYR",
    "Tajikistan": "TJK",
    "Tanzania": "TZA",
    "Thailand": "THA",
    "The Former Yugoslav Republic of Macedonia": "MKD",  # Now officially known as North Macedonia
    "Timor-Leste": "TLS",
    "Togo": "TGO",
    "Tunisia": "TUN",
    "Turkey": "TUR",
    "Turkmenistan": "TKM",
    "Uganda": "UGA",
    "Ukraine": "UKR",
    "United Arab Emirates": "ARE",
    "United Kingdom": "GBR",
    "United States": "USA",
    "Uruguay": "URY",
    "Uzbekistan": "UZB",
    "Venezuela": "VEN",
    "Vietnam": "VNM",
    "Yemen": "YEM",
    "Zambia": "ZMB",
    "Zimbabwe": "ZWE",
    "Sudan": "SDN",
    "South Sudan": "SSD",
}

COUNTRY_NAME_TO_ISO3 = {
    "Albania": "ALB",
    "Algeria": "DZA",
    "American Samoa": "ASM",
    "Argentina": "ARG",
    "Austria": "AUT",
    "Bahamas": "BHS",
    "Barbados": "BRB",
    "Belgium": "BEL",
    "Brazil": "BRA",
    "Bulgaria": "BGR",
    "Burkina Faso": "BFA",
    "Chile": "CHL",
    "Colombia": "COL",
    "Côte d'Ivoire": "CIV",
    "Croatia": "HRV",
    "Cyprus": "CYP",
    "Czech Republic": "CZE",
    "Democratic Republic of the Congo": "COD",
    "Denmark": "DNK",
    "Dominica": "DMA",
    "Ecuador": "ECU",
    "Egypt": "EGY",
    "Estonia": "EST",
    "Ethiopia": "ETH",
    "Fiji": "FJI",
    "Finland": "FIN",
    "France": "FRA",
    "French Polynesia": "PYF",
    "Georgia": "GEO",
    "Germany": "DEU",
    "Greece": "GRC",
    "Grenada": "GRD",
    "Guam": "GUM",
    "Guatemala": "GTM",
    "Guinea": "GIN",
    "Honduras": "HND",
    "India": "IND",
    "Indonesia": "IDN",
    "Iran (Islamic Republic of)": "IRN",
    "Ireland": "IRL",
    "Italy": "ITA",
    "Japan": "JPN",
    "Jamaica": "JAM",
    "Jordan": "JOR",
    "Korea, Rep. of": "KOR",
    "Kyrgyzstan": "KGZ",
    "Kosovo": "XKX",
    "Lao People's Democratic Republic": "LAO",
    "Latvia": "LVA",
    "Lebanon": "LBN",
    "Lithuania": "LTU",
    "Luxembourg": "LUX",
    "Malta": "MLT",
    "Morocco": "MAR",
    "Myanmar": "MMR",
    "Namibia": "NAM",
    "Nepal": "NPL",
    "Netherlands": "NLD",
    "Nicaragua": "NIC",
    "Northern Mariana Islands": "MNP",
    "Norway": "NOR",
    "Pakistan": "PAK",
    "Panama": "PAN",
    "Paraguay": "PRY",
    "Peru": "PER",
    "Philippines": "PHL",
    "Poland": "POL",
    "Portugal": "PRT",
    "Puerto Rico": "PRI",
    "Qatar": "QAT",
    "Romania": "ROU",
    "Saint Lucia": "LCA",
    "Saint Vincent and the Grenadines": "VCT",
    "Samoa": "WSM",
    "Senegal": "SEN",
    "Serbia": "SRB",
    "Sweden": "SWE",
    "Switzerland": "CHE",
    "Thailand": "THA",
    "Trinidad and Tobago": "TTO",
    "Turkey": "TUR",
    "Uganda": "UGA",
    "United Kingdom": "GBR",
    "United States of America": "USA",
    "Uruguay": "URY",
    "Venezuela (Bolivarian Republic of)": "VEN",
    "Virgin Islands, United States": "VIR",
    "Yemen": "YEM",
    "Cook Islands": "COK",
    "French Guiana": "GUF",
    "Guadeloupe": "GLP",
    "Martinique": "MTQ",
    "Réunion": "REU",
    "Canada": "CAN",
    "China": "CHN",
    "Guinea Bissau": "GNB",
    "Hungary": "HUN",
    "Lesotho": "LSO",
    "Libya": "LBY",
    "Malawi": "MWI",
    "Mozambique": "MOZ",
    "New Zealand": "NZL",
    "Slovakia": "SVK",
    "Slovenia": "SVN",
    "Spain": "ESP",
    "St. Kitts & Nevis": "KNA",
    "Viet Nam": "VNM",
    "Australia": "AUS",
    "Djibouti": "DJI",
    "Mali": "MLI",
    "Togo": "TGO",
    "Zambia": "ZMB",
    "Afghanistan": "AFG",
    "Andorra": "AND",
    "Angola": "AGO",
    "Anguilla": "AIA",
    "Antarctica": "ATA",
    "Antigua and Barbuda": "ATG",
    "Armenia": "ARM",
    "Aruba": "ABW",
    "Azerbaijan": "AZE",
    "Bahrain": "BHR",
    "Bangladesh": "BGD",
    "Belarus": "BLR",
    "Belize": "BLZ",
    "Benin": "BEN",
    "Bermuda": "BMU",
    "Bhutan": "BTN",
    "Bolivia (Plurinational State of)": "BOL",
    "Bonaire, Sint Eustatius and Saba": "BES",
    "Bosnia and Herzegovina": "BIH",
    "Botswana": "BWA",
    "Bouvet Island": "BVT",
    "British Indian Ocean Territory": "IOT",
    "Brunei Darussalam": "BRN",
    "Burundi": "BDI",
    "Cabo Verde": "CPV",
    "Cambodia": "KHM",
    "Cameroon": "CMR",
    "Central African Republic": "CAF",
    "Chad": "TCD",
    "Christmas Island": "CXR",
    "Cocos (Keeling) Islands": "CCK",
    "Comoros": "COM",
    "Congo (the)": "COG",
    "Costa Rica": "CRI",
    "Cuba": "CUB",
    "Curaçao": "CUW",
    "Czechia": "CZE",
    "Dominican Republic": "DOM",
    "El Salvador": "SLV",
    "Equatorial Guinea": "GNQ",
    "Eritrea": "ERI",
    "Eswatini": "SWZ",
    "Falkland Islands": "FLK",
    "Faroe Islands": "FRO",
    "Gabon": "GAB",
    "Gambia": "GMB",
    "Ghana": "GHA",
    "Gibraltar": "GIB",
    "Greenland": "GRL",
    "Guernsey": "GGY",
    "Guyana": "GUY",
    "Haiti": "HTI",
    "Heard Island and McDonald Islands": "HMD",
    "Holy See": "VAT",
    "Hong Kong": "HKG",
    "Iceland": "ISL",
    "Iraq": "IRQ",
    "Isle of Man": "IMN",
    "Israel": "ISR",
    "Jersey": "JEY",
    "Kazakhstan": "KAZ",
    "Kenya": "KEN",
    "Kiribati": "KIR",
    "Korea (the Democratic People's Republic of)": "PRK",
    "Kuwait": "KWT",
    "Liechtenstein": "LIE",
    "Macao": "MAC",
    "Madagascar": "MDG",
    "Malaysia": "MYS",
    "Maldives": "MDV",
    "Marshall Islands": "MHL",
    "Mauritania": "MRT",
    "Mauritius": "MUS",
    "Mayotte": "MYT",
    "Mexico": "MEX",
    "Micronesia (Federated States of)": "FSM",
    "Moldova": "MDA",
    "Monaco": "MCO",
    "Mongolia": "MNG",
    "Montenegro": "MNE",
    "Montserrat": "MSR",
    "Nauru": "NRU",
    "Niger": "NER",
    "Nigeria": "NGA",
    "Niue": "NIU",
    "Norfolk Island": "NFK",
    "North Macedonia": "MKD",
    "Oman": "OMN",
    "Palau": "PLW",
    "Palestine, State of": "PSE",
    "Papua New Guinea": "PNG",
    "Pitcairn": "PCN",
    "Russian Federation": "RUS",
    "Rwanda": "RWA",
    "Saint Barthélemy": "BLM",
    "Saint Helena, Ascension and Tristan da Cunha": "SHN",
    "Saint Martin (French part)": "MAF",
    "Saint Pierre and Miquelon": "SPM",
    "San Marino": "SMR",
    "Sao Tome and Principe": "STP",
    "Saudi Arabia": "SAU",
    "Seychelles": "SYC",
    "Sierra Leone": "SLE",
    "Singapore": "SGP",
    "Sint Maarten (Dutch part)": "SXM",
    "Solomon Islands": "SLB",
    "Somalia": "SOM",
    "South Africa": "ZAF",
    "South Georgia and the South Sandwich Islands": "SGS",
    "South Sudan": "SSD",
    "Sri Lanka": "LKA",
    "Sudan": "SDN",
    "Suriname": "SUR",
    "Svalbard and Jan Mayen": "SJM",
    "Syrian Arab Republic": "SYR",
    "Taiwan": "TWN",
    "Tajikistan": "TJK",
    "Tanzania": "TZA",
    "Timor-Leste": "TLS",
    "Tokelau": "TKL",
    "Tonga": "TON",
    "Tunisia": "TUN",
    "Turkmenistan": "TKM",
    "Turks and Caicos Islands": "TCA",
    "Tuvalu": "TUV",
    "Ukraine": "UKR",
    "United Arab Emirates": "ARE",
    "United States Minor Outlying Islands": "UMI",
    "Uzbekistan": "UZB",
    "Vanuatu": "VUT",
    "Virgin Islands (British)": "VGB",
    "Wallis and Futuna": "WLF",
    "Western Sahara": "ESH",
    "Zimbabwe": "ZWE",
    "Åland Islands": "ALA",
}


GLOBIOM_NAME_TO_ISO3 = {
    "Argentina": "ARG",
    "Australia": "AUS",
    "Brazil": "BRA",
    "Canada": "CAN",
    "China": "CHN",
    "Cameroon": "CMR",
    "CentAfrRep": "CAF",
    "CongoDemR": "COD",
    "CongoRep": "COG",
    "EqGuinea": "GNQ",
    "Gabon": "GAB",
    "Estonia": "EST",
    "Latvia": "LVA",
    "Lithuania": "LTU",
    "Bulgaria": "BGR",
    "Croatia": "HRV",
    "CzechRep": "CZE",
    "Hungary": "HUN",
    "Poland": "POL",
    "Romania": "ROU",
    "Slovakia": "SVK",
    "Slovenia": "SVN",
    "Austria": "AUT",
    "Belgium": "BEL",
    "France": "FRA",
    "Germany": "DEU",
    "Luxembourg": "LUX",
    "Netherlands": "NLD",
    "Denmark": "DNK",
    "Finland": "FIN",
    "Ireland": "IRL",
    "Sweden": "SWE",
    "UK": "GBR",
    "Cyprus": "CYP",
    "Greece": "GRC",
    "Italy": "ITA",
    "Malta": "MLT",
    "Portugal": "PRT",
    "Spain": "ESP",
    "Armenia": "ARM",
    "Azerbaijan": "AZE",
    "Belarus": "BLR",
    "Georgia": "GEO",
    "Kazakhstan": "KAZ",
    "Kyrgyzstan": "KGZ",
    "MoldovaRep": "MDA",
    "Tajikistan": "TJK",
    "Turkmenistan": "TKM",
    "Uzbekistan": "UZB",
    "India": "IND",
    "Indonesia": "IDN",
    "Japan": "JPN",
    "Malaysia": "MYS",
    "Mexico": "MEX",
    "Bahrain": "BHR",
    "Iran": "IRN",
    "Iraq": "IRQ",
    "Israel": "ISR",
    "Jordan": "JOR",
    "Kuwait": "KWT",
    "Lebanon": "LBN",
    "Oman": "OMN",
    "Palestin": "PSE",
    "Qatar": "QAT",
    "SaudiArabia": "SAU",
    "Syria": "SYR",
    "UntdArabEm": "ARE",
    "Yemen": "YEM",
    "NewZealand": "NZL",
    "Algeria": "DZA",
    "Egypt": "EGY",
    "Libya": "LBY",
    "Morocco": "MAR",
    "Tunisia": "TUN",
    "WestSahara": "ESH",
    "FijiIslands": "FJI",
    "FrPolynesia": "PYF",
    "NewCaledonia": "NCL",
    "PapuaNGuin": "PNG",
    "Samoa": "WSM",
    "SolomonIs": "SLB",
    "Vanuatu": "VUT",
    "Bahamas": "BHS",
    "Belize": "BLZ",
    "CostaRica": "CRI",
    "Cuba": "CUB",
    "DominicanRp": "DOM",
    "ElSalvador": "SLV",
    "Guadeloupe": "GLP",
    "Guatemala": "GTM",
    "Haiti": "HTI",
    "Honduras": "HND",
    "Jamaica": "JAM",
    "Nicaragua": "NIC",
    "Panama": "PAN",
    "TrinidadTob": "TTO",
    "Albania": "ALB",
    "BosniaHerzg": "BIH",
    "Macedonia": "MKD",
    "Serbia-Monte": "SRB",
    "Greenland": "GRL",
    "Iceland": "ISL",
    "Norway": "NOR",
    "Switzerland": "CHE",
    "Bolivia": "BOL",
    "Chile": "CHL",
    "Colombia": "COL",
    "Ecuador": "ECU",
    "FalklandIs": "FLK",
    "FrGuiana": "GUF",
    "Guyana": "GUY",
    "Paraguay": "PRY",
    "Peru": "PER",
    "Suriname": "SUR",
    "Uruguay": "URY",
    "Venezuela": "VEN",
    "Afghanistan": "AFG",
    "Bangladesh": "BGD",
    "Bhutan": "BTN",
    "Nepal": "NPL",
    "Pakistan": "PAK",
    "SriLanka": "LKA",
    "BruneiDarsm": "BRN",
    "Myanmar": "MMR",
    "Philippines": "PHL",
    "Singapore": "SGP",
    "Thailand": "THA",
    "TimorLeste": "TLS",
    "Cambodia": "KHM",
    "KoreaDPRp": "PRK",
    "Laos": "LAO",
    "Mongolia": "MNG",
    "VietNam": "VNM",
    "RussianFed": "RUS",
    "SouthAfrica": "ZAF",
    "KoreaRep": "KOR",
    "Burundi": "BDI",
    "Ethiopia": "ETH",
    "Kenya": "KEN",
    "Rwanda": "RWA",
    "Tanzania": "TZA",
    "Uganda": "UGA",
    "Angola": "AGO",
    "Botswana": "BWA",
    "Comoros": "COM",
    "Lesotho": "LSO",
    "Madagascar": "MDG",
    "Malawi": "MWI",
    "Mauritius": "MUS",
    "Mozambique": "MOZ",
    "Namibia": "NAM",
    "Reunion": "REU",
    "Swaziland": "SWZ",
    "Zambia": "ZMB",
    "Zimbabwe": "ZWE",
    "Benin": "BEN",
    "BurkinaFaso": "BFA",
    "CapeVerde": "CPV",
    "Chad": "TCD",
    "CotedIvoire": "CIV",
    "Djibouti": "DJI",
    "Eritrea": "ERI",
    "Gambia": "GMB",
    "Ghana": "GHA",
    "Guinea": "GIN",
    "GuineaBissau": "GNB",
    "Liberia": "LBR",
    "Mali": "MLI",
    "Mauritania": "MRT",
    "Niger": "NER",
    "Nigeria": "NGA",
    "Senegal": "SEN",
    "SierraLeone": "SLE",
    "Somalia": "SOM",
    "Sudan": "SDN",
    "Togo": "TGO",
    "Turkey": "TUR",
    "Ukraine": "UKR",
    "PuertoRico": "PRI",
    "USA": "USA",
}


AQUASTAT_NAME_TO_ISO3 = {
    "Afghanistan": "AFG",
    "Albania": "ALB",
    "Algeria": "DZA",
    "Andorra": "AND",
    "Angola": "AGO",
    "Antigua and Barbuda": "ATG",
    "Argentina": "ARG",
    "Armenia": "ARM",
    "Australia": "AUS",
    "Austria": "AUT",
    "Azerbaijan": "AZE",
    "Bahamas": "BHS",
    "Bahrain": "BHR",
    "Bangladesh": "BGD",
    "Barbados": "BRB",
    "Belarus": "BLR",
    "Belgium": "BEL",
    "Belize": "BLZ",
    "Benin": "BEN",
    "Bhutan": "BTN",
    "Bolivia (Plurinational State of)": "BOL",
    "Bosnia and Herzegovina": "BIH",
    "Botswana": "BWA",
    "Brazil": "BRA",
    "Brunei Darussalam": "BRN",
    "Bulgaria": "BGR",
    "Burkina Faso": "BFA",
    "Burundi": "BDI",
    "Cabo Verde": "CPV",
    "Cambodia": "KHM",
    "Cameroon": "CMR",
    "Canada": "CAN",
    "Central African Republic": "CAF",
    "Chad": "TCD",
    "Chile": "CHL",
    "China": "CHN",
    "Colombia": "COL",
    "Comoros": "COM",
    "Congo": "COG",
    "Cook Islands": "COK",
    "Costa Rica": "CRI",
    "Croatia": "HRV",
    "Cuba": "CUB",
    "Cyprus": "CYP",
    "Czechia": "CZE",
    "Côte d'Ivoire": "CIV",
    "Democratic People's Republic of Korea": "PRK",
    "Democratic Republic of the Congo": "COD",
    "Denmark": "DNK",
    "Djibouti": "DJI",
    "Dominica": "DMA",
    "Dominican Republic": "DOM",
    "Ecuador": "ECU",
    "Egypt": "EGY",
    "El Salvador": "SLV",
    "Equatorial Guinea": "GNQ",
    "Eritrea": "ERI",
    "Estonia": "EST",
    "Eswatini": "SWZ",
    "Ethiopia": "ETH",
    "Faroe Islands": "FRO",
    "Fiji": "FJI",
    "Finland": "FIN",
    "France": "FRA",
    "Gabon": "GAB",
    "Gambia": "GMB",
    "Georgia": "GEO",
    "Germany": "DEU",
    "Ghana": "GHA",
    "Greece": "GRC",
    "Grenada": "GRD",
    "Guatemala": "GTM",
    "Guinea": "GIN",
    "Guinea-Bissau": "GNB",
    "Guyana": "GUY",
    "Haiti": "HTI",
    "Holy See": "VAT",
    "Honduras": "HND",
    "Hungary": "HUN",
    "Iceland": "ISL",
    "India": "IND",
    "Indonesia": "IDN",
    "Iran (Islamic Republic of)": "IRN",
    "Iraq": "IRQ",
    "Ireland": "IRL",
    "Israel": "ISR",
    "Italy": "ITA",
    "Jamaica": "JAM",
    "Japan": "JPN",
    "Jordan": "JOR",
    "Kazakhstan": "KAZ",
    "Kenya": "KEN",
    "Kiribati": "KIR",
    "Kosovo": "XKX",
    "Kuwait": "KWT",
    "Kyrgyzstan": "KGZ",
    "Lao People's Democratic Republic": "LAO",
    "Latvia": "LVA",
    "Lebanon": "LBN",
    "Lesotho": "LSO",
    "Liberia": "LBR",
    "Libya": "LBY",
    "Liechtenstein": "LIE",
    "Lithuania": "LTU",
    "Luxembourg": "LUX",
    "Madagascar": "MDG",
    "Malawi": "MWI",
    "Malaysia": "MYS",
    "Maldives": "MDV",
    "Mali": "MLI",
    "Malta": "MLT",
    "Marshall Islands": "MHL",
    "Mauritania": "MRT",
    "Mauritius": "MUS",
    "Mexico": "MEX",
    "Micronesia (Federated States of)": "FSM",
    "Monaco": "MCO",
    "Mongolia": "MNG",
    "Montenegro": "MNE",
    "Morocco": "MAR",
    "Mozambique": "MOZ",
    "Myanmar": "MMR",
    "Namibia": "NAM",
    "Nauru": "NRU",
    "Nepal": "NPL",
    "Netherlands (Kingdom of the)": "NLD",
    "New Zealand": "NZL",
    "Nicaragua": "NIC",
    "Niger": "NER",
    "Nigeria": "NGA",
    "Niue": "NIU",
    "North Macedonia": "MKD",
    "Norway": "NOR",
    "Oman": "OMN",
    "Pakistan": "PAK",
    "Palau": "PLW",
    "Palestine": "PSE",
    "Panama": "PAN",
    "Papua New Guinea": "PNG",
    "Paraguay": "PRY",
    "Peru": "PER",
    "Philippines": "PHL",
    "Poland": "POL",
    "Portugal": "PRT",
    "Puerto Rico": "PRI",
    "Qatar": "QAT",
    "Republic of Korea": "KOR",
    "Republic of Moldova": "MDA",
    "Romania": "ROU",
    "Russian Federation": "RUS",
    "Rwanda": "RWA",
    "Saint Kitts and Nevis": "KNA",
    "Saint Lucia": "LCA",
    "Saint Vincent and the Grenadines": "VCT",
    "Samoa": "WSM",
    "San Marino": "SMR",
    "Sao Tome and Principe": "STP",
    "Saudi Arabia": "SAU",
    "Senegal": "SEN",
    "Serbia": "SRB",
    "Seychelles": "SYC",
    "Sierra Leone": "SLE",
    "Singapore": "SGP",
    "Slovakia": "SVK",
    "Slovenia": "SVN",
    "Solomon Islands": "SLB",
    "Somalia": "SOM",
    "South Africa": "ZAF",
    "South Sudan": "SSD",
    "Spain": "ESP",
    "Sri Lanka": "LKA",
    "Sudan": "SDN",
    "Suriname": "SUR",
    "Sweden": "SWE",
    "Switzerland": "CHE",
    "Syrian Arab Republic": "SYR",
    "Tajikistan": "TJK",
    "Thailand": "THA",
    "Timor-Leste": "TLS",
    "Togo": "TGO",
    "Tokelau": "TKL",
    "Tonga": "TON",
    "Trinidad and Tobago": "TTO",
    "Tunisia": "TUN",
    "Turkmenistan": "TKM",
    "Tuvalu": "TUV",
    "Türkiye": "TUR",
    "Uganda": "UGA",
    "Ukraine": "UKR",
    "United Arab Emirates": "ARE",
    "United Kingdom of Great Britain and Northern Ireland": "GBR",
    "United Republic of Tanzania": "TZA",
    "United States of America": "USA",
    "Uruguay": "URY",
    "Uzbekistan": "UZB",
    "Vanuatu": "VUT",
    "Venezuela (Bolivarian Republic of)": "VEN",
    "Viet Nam": "VNM",
    "Yemen": "YEM",
    "Zambia": "ZMB",
    "Zimbabwe": "ZWE",
}
