/*
 * (C) Copyright 2023, met.no
 *
 * This file is part of the Norbufr BUFR en/decoder
 *
 * Author: istvans@met.no
 *
 */

#ifndef _COUNTRYCODES_H_
#define _COUNTRYCODES_H_

#include <list>
#include <string>

// Conversion between BUFR Country codes and ISO3166 Country codes

struct cc_conv {
  std::string cname; // Iso Country Name
  std::string cc2;
  std::string cc3;
  int iso_code;
  int bufr_code;
  std::string bname; // BUFR Country Name
};

static const std::list<cc_conv> country_codes = {

    {"Afghanistan", "AF", "AFG", 4, 200, "AFGHANISTAN"},
    {"Åland Islands", "AX", "ALA", 248, 0, ""},
    {"Albania", "AL", "ALB", 8, 600, "ALBANIA"},
    {"Algeria", "DZ", "DZA", 12, 100, "ALGERIA"},
    {"American Samoa", "AS", "ASM", 16, 0, ""},
    {"Andorra", "AD", "AND", 20, 0, ""},
    {"Angola", "AO", "AGO", 24, 101, "ANGOLA"},
    {"Anguilla", "AI", "AIA", 660, 0, ""},
    {"Antarctica", "AQ", "ATA", 10, 0, ""},
    {"Antigua and Barbuda", "AG", "ATG", 28, 400, "ANTIGUA AND BARBUDA"},
    {"Argentina", "AR", "ARG", 32, 300, "ARGENTINA"},
    {"Armenia", "AM", "ARM", 51, 601, "ARMENIA"},
    {"Aruba", "AW", "ABW", 533, 0, ""},
    {"Australia", "AU", "AUS", 36, 500, "AUSTRALIA"},
    {"Austria", "AT", "AUT", 40, 602, "AUSTRIA"},
    {"Azerbaijan", "AZ", "AZE", 31, 603, "AZERBAIJAN"},
    {"Bahamas", "BS", "BHS", 44, 401, "BAHAMAS"},
    {"Bahrain", "BH", "BHR", 48, 201, "BAHRAIN"},
    {"Bangladesh", "BD", "BGD", 50, 202, "BANGLADESH"},
    {"Barbados", "BB", "BRB", 52, 402, "BARBADOS"},
    {"Belarus", "BY", "BLR", 112, 604, "BELARUS"},
    {"Belgium", "BE", "BEL", 56, 605, "BELGIUM"},
    {"Belize", "BZ", "BLZ", 84, 403, "BELIZE"},
    {"Benin", "BJ", "BEN", 204, 102, "BENIN"},
    {"Bermuda", "BM", "BMU", 60, 0, ""},
    {"Bhutan", "BT", "BTN", 64, 203, "BHUTAN"},
    {"Bolivia (Plurinational State of)", "BO", "BOL", 68, 0, ""},
    {"Bonaire, Sint Eustatius and Saba", "BQ", "BES", 535, 0, ""},
    {"Bosnia and Herzegovina", "BA", "BIH", 70, 606, "BOSNIA AND HERZEGOVINA"},
    {"Botswana", "BW", "BWA", 72, 103, "BOTSWANA"},
    {"Bouvet Island", "BV", "BVT", 74, 0, ""},
    {"Brazil", "BR", "BRA", 76, 302, "BRAZIL"},
    {"British Indian Ocean Territory", "IO", "IOT", 86, 0, ""},
    {"Brunei Darussalam", "BN", "BRN", 96, 501, "BRUNEI DARUSSALAM"},
    {"Bulgaria", "BG", "BGR", 100, 607, "BULGARIA"},
    {"Burkina Faso", "BF", "BFA", 854, 104, "BURKINA FASO"},
    {"Burundi", "BI", "BDI", 108, 105, "BURUNDI"},
    {"Cabo Verde", "CV", "CPV", 132, 107, "CABO VERDE"},
    {"Cambodia", "KH", "KHM", 116, 204, "CAMBODIA"},
    {"Cameroon", "CM", "CMR", 120, 106, "CAMEROON"},
    {"Canada", "CA", "CAN", 124, 405, "CANADA"},
    {"Cayman Islands", "KY", "CYM", 136, 0, ""},
    {"Central African Republic", "CF", "CAF", 140, 108,
     "CENTRAL AFRICAN REPUBLIC"},
    {"Chad", "TD", "TCD", 148, 109, "CHAD"},
    {"Chile", "CL", "CHL", 152, 303, "CHILE"},
    {"China", "CN", "CHN", 156, 205, "CHINA"},
    {"China", "CN", "CHN", 156, 207, "HONG KONG, CHINA"},
    {"China", "CN", "CHN", 156, 216, "MACAO, CHINA"},
    {"Christmas Island", "CX", "CXR", 162, 0, ""},
    {"Cocos (Keeling) Islands", "CC", "CCK", 166, 0, ""},
    {"Colombia", "CO", "COL", 170, 304, "COLOMBIA"},
    {"Colombia", "CO", "COL", 170, 406, "COLOMBIA"},
    {"Comoros", "KM", "COM", 174, 110, "COMOROS"},
    {"Congo", "CG", "COG", 178, 111, "CONGO"},
    {"Congo", "CG", "COG", 178, 113, "DEMOCRATIC REPUBLIC OF THE CONGO"},
    {"Congo, Democratic Republic of the", "CD", "COD", 180, 0, ""},
    {"Cook Islands", "CK", "COK", 184, 502, "COOK ISLANDS"},
    {"Costa Rica", "CR", "CRI", 188, 407, "COSTA RICA"},
    {"Côte d'Ivoire", "CI", "CIV", 384, 112, "CôTE D'IVOIRE"},
    {"Croatia", "HR", "HRV", 191, 608, "CROATIA"},
    {"Cuba", "CU", "CUB", 192, 408, "CUBA"},
    {"Curaçao", "CW", "CUW", 531, 418, "CURAçAO AND SINT MAARTEN"},
    {"Cyprus", "CY", "CYP", 196, 609, "CYPRUS"},
    {"Czechia", "CZ", "CZE", 203, 610, "CZECHIA"},
    {"Denmark", "DK", "DNK", 208, 611, "DENMARK"},
    {"Djibouti", "DJ", "DJI", 262, 114, "DJIBOUTI"},
    {"Dominica", "DM", "DMA", 212, 409, "DOMINICA"},
    {"Dominica", "DM", "DMA", 212, 410, "DOMINICAN REPUBLIC"},
    {"Dominican Republic", "DO", "DOM", 214, 410, "DOMINICAN REPUBLIC"},
    {"Ecuador", "EC", "ECU", 218, 305, "ECUADOR"},
    {"Egypt", "EG", "EGY", 818, 115, "EGYPT"},
    {"El Salvador", "SV", "SLV", 222, 411, "EL SALVADOR"},
    {"Equatorial Guinea", "GQ", "GNQ", 226, 0, ""},
    {"Eritrea", "ER", "ERI", 232, 116, "ERITREA"},
    {"Estonia", "EE", "EST", 233, 612, "ESTONIA"},
    {"Eswatini", "SZ", "SWZ", 748, 148, "ESWATINI"},
    {"Ethiopia", "ET", "ETH", 231, 117, "ETHIOPIA"},
    {"Falkland Islands (Malvinas)", "FK", "FLK", 238, 0, ""},
    {"Faroe Islands", "FO", "FRO", 234, 0, ""},
    {"Fiji", "FJ", "FJI", 242, 503, "FIJI"},
    {"Finland", "FI", "FIN", 246, 613, "FINLAND"},
    {"France", "FR", "FRA", 250, 118, "FRANCE (RA I)"},
    {"France", "FR", "FRA", 250, 306, "FRANCE (RA III)"},
    {"France", "FR", "FRA", 250, 412, "FRANCE (RA IV)"},
    {"France", "FR", "FRA", 250, 614, "FRANCE (RA VI)"},
    {"French Guiana", "GF", "GUF", 254, 0, ""},
    {"French Polynesia", "PF", "PYF", 258, 504, "FRENCH POLYNESIA"},
    {"French Southern Territories", "TF", "ATF", 260, 0, ""},
    {"Gabon", "GA", "GAB", 266, 119, "GABON"},
    {"Gambia", "GM", "GMB", 270, 120, "GAMBIA"},
    {"Georgia", "GE", "GEO", 268, 615, "GEORGIA"},
    {"Germany", "DE", "DEU", 276, 616, "GERMANY"},
    {"Ghana", "GH", "GHA", 288, 121, "GHANA"},
    {"Gibraltar", "GI", "GIB", 292, 0, ""},
    {"Greece", "GR", "GRC", 300, 617, "GREECE"},
    {"Greenland", "GL", "GRL", 304, 0, ""},
    {"Grenada", "GD", "GRD", 308, 0, ""},
    {"Guadeloupe", "GP", "GLP", 312, 0, ""},
    {"Guam", "GU", "GUM", 316, 0, ""},
    {"Guatemala", "GT", "GTM", 320, 413, "GUATEMALA"},
    {"Guernsey", "GG", "GGY", 831, 0, ""},
    {"Guinea", "GN", "GIN", 324, 122, "GUINEA"},
    {"Guinea", "GN", "GIN", 324, 123, "GUINEA-BISSAU"},
    {"Guinea", "GN", "GIN", 324, 512, "PAPUA NEW GUINEA"},
    {"Guinea-Bissau", "GW", "GNB", 624, 123, "GUINEA-BISSAU"},
    {"Guyana", "GY", "GUY", 328, 307, "GUYANA"},
    {"Haiti", "HT", "HTI", 332, 414, "HAITI"},
    {"Heard Island and McDonald Islands", "HM", "HMD", 334, 0, ""},
    {"Holy See", "VA", "VAT", 336, 0, ""},
    {"Honduras", "HN", "HND", 340, 415, "HONDURAS"},
    {"Hong Kong", "HK", "HKG", 344, 207, "HONG KONG, CHINA"},
    {"Hungary", "HU", "HUN", 348, 618, "HUNGARY"},
    {"Iceland", "IS", "ISL", 352, 619, "ICELAND"},
    {"India", "IN", "IND", 356, 208, "INDIA"},
    {"Indonesia", "ID", "IDN", 360, 505, "INDONESIA"},
    {"Iran (Islamic Republic of)", "IR", "IRN", 364, 0, ""},
    {"Iraq", "IQ", "IRQ", 368, 210, "IRAQ"},
    {"Ireland", "IE", "IRL", 372, 620, "IRELAND"},
    {"Isle of Man", "IM", "IMN", 833, 0, ""},
    {"Israel", "IL", "ISR", 376, 621, "ISRAEL"},
    {"Italy", "IT", "ITA", 380, 622, "ITALY"},
    {"Jamaica", "JM", "JAM", 388, 416, "JAMAICA"},
    {"Japan", "JP", "JPN", 392, 211, "JAPAN"},
    {"Jersey", "JE", "JEY", 832, 0, ""},
    {"Jordan", "JO", "JOR", 400, 623, "JORDAN"},
    {"Kazakhstan", "KZ", "KAZ", 398, 212, "KAZAKHSTAN"},
    {"Kazakhstan", "KZ", "KAZ", 398, 624, "KAZAKHSTAN"},
    {"Kenya", "KE", "KEN", 404, 124, "KENYA"},
    {"Kiribati", "KI", "KIR", 296, 506, "KIRIBATI"},
    {"Korea (Democratic People's Republic of)", "KP", "PRK", 408, 206,
     "DEMOCRATIC PEOPLE'S REPUBLIC OF KOREA"},
    {"Korea, Republic of", "KR", "KOR", 410, 224, "REPUBLIC OF KOREA"},
    {"Kuwait", "KW", "KWT", 414, 213, "KUWAIT"},
    {"Kyrgyzstan", "KG", "KGZ", 417, 214, "KYRGYZSTAN"},
    {"Lao People's Democratic Republic", "LA", "LAO", 418, 0, ""},
    {"Latvia", "LV", "LVA", 428, 625, "LATVIA"},
    {"Lebanon", "LB", "LBN", 422, 626, "LEBANON"},
    {"Lesotho", "LS", "LSO", 426, 125, "LESOTHO"},
    {"Liberia", "LR", "LBR", 430, 126, "LIBERIA"},
    {"Libya", "LY", "LBY", 434, 127, "LIBYA"},
    {"Liechtenstein", "LI", "LIE", 438, 0, ""},
    {"Lithuania", "LT", "LTU", 440, 627, "LITHUANIA"},
    {"Luxembourg", "LU", "LUX", 442, 628, "LUXEMBOURG"},
    {"Macao", "MO", "MAC", 446, 216, "MACAO, CHINA"},
    {"Madagascar", "MG", "MDG", 450, 128, "MADAGASCAR"},
    {"Malawi", "MW", "MWI", 454, 129, "MALAWI"},
    {"Malaysia", "MY", "MYS", 458, 507, "MALAYSIA"},
    {"Maldives", "MV", "MDV", 462, 217, "MALDIVES"},
    {"Mali", "ML", "MLI", 466, 130, "MALI"},
    {"Mali", "ML", "MLI", 466, 144, "SOMALIA"},
    {"Malta", "MT", "MLT", 470, 629, "MALTA"},
    {"Marshall Islands", "MH", "MHL", 584, 0, ""},
    {"Martinique", "MQ", "MTQ", 474, 0, ""},
    {"Mauritania", "MR", "MRT", 478, 131, "MAURITANIA"},
    {"Mauritius", "MU", "MUS", 480, 132, "MAURITIUS"},
    {"Mayotte", "YT", "MYT", 175, 0, ""},
    {"Mexico", "MX", "MEX", 484, 417, "MEXICO"},
    {"Micronesia (Federated States of)", "FM", "FSM", 583, 0, ""},
    {"Moldova, Republic of", "MD", "MDA", 498, 0, ""},
    {"Monaco", "MC", "MCO", 492, 630, "MONACO"},
    {"Mongolia", "MN", "MNG", 496, 218, "MONGOLIA"},
    {"Montenegro", "ME", "MNE", 499, 631, "MONTENEGRO"},
    {"Montserrat", "MS", "MSR", 500, 0, ""},
    {"Morocco", "MA", "MAR", 504, 133, "MOROCCO"},
    {"Mozambique", "MZ", "MOZ", 508, 134, "MOZAMBIQUE"},
    {"Myanmar", "MM", "MMR", 104, 219, "MYANMAR"},
    {"Namibia", "NA", "NAM", 516, 135, "NAMIBIA"},
    {"Nauru", "NR", "NRU", 520, 0, ""},
    {"Nepal", "NP", "NPL", 524, 220, "NEPAL"},
    {"Netherlands", "NL", "NLD", 528, 632, "NETHERLANDS"},
    {"New Caledonia", "NC", "NCL", 540, 509, "NEW CALEDONIA"},
    {"New Zealand", "NZ", "NZL", 554, 510, "NEW ZEALAND"},
    {"Nicaragua", "NI", "NIC", 558, 419, "NICARAGUA"},
    {"Niger", "NE", "NER", 562, 136, "NIGER"},
    {"Niger", "NE", "NER", 562, 137, "NIGERIA"},
    {"Nigeria", "NG", "NGA", 566, 137, "NIGERIA"},
    {"Niue", "NU", "NIU", 570, 511, "NIUE"},
    {"Norfolk Island", "NF", "NFK", 574, 0, ""},
    {"North Macedonia", "MK", "MKD", 807, 0, ""},
    {"Northern Mariana Islands", "MP", "MNP", 580, 0, ""},
    {"Norway", "NO", "NOR", 578, 633, "NORWAY"},
    {"Oman", "OM", "OMN", 512, 221, "OMAN"},
    {"Oman", "OM", "OMN", 512, 637, "ROMANIA"},
    {"Pakistan", "PK", "PAK", 586, 222, "PAKISTAN"},
    {"Palau", "PW", "PLW", 585, 0, ""},
    {"Palestine, State of", "PS", "PSE", 275, 0, ""},
    {"Panama", "PA", "PAN", 591, 420, "PANAMA"},
    {"Papua New Guinea", "PG", "PNG", 598, 512, "PAPUA NEW GUINEA"},
    {"Paraguay", "PY", "PRY", 600, 308, "PARAGUAY"},
    {"Peru", "PE", "PER", 604, 309, "PERU"},
    {"Philippines", "PH", "PHL", 608, 513, "PHILIPPINES"},
    {"Pitcairn", "PN", "PCN", 612, 0, ""},
    {"Poland", "PL", "POL", 616, 634, "POLAND"},
    {"Portugal", "PT", "PRT", 620, 138, "PORTUGAL (RA I)"},
    {"Portugal", "PT", "PRT", 620, 635, "PORTUGAL (RA VI)"},
    {"Puerto Rico", "PR", "PRI", 630, 0, ""},
    {"Qatar", "QA", "QAT", 634, 223, "QATAR"},
    {"Réunion", "RE", "REU", 638, 0, ""},
    {"Romania", "RO", "ROU", 642, 637, "ROMANIA"},
    {"Russian Federation", "RU", "RUS", 643, 226, "RUSSIAN FEDERATION (RA II)"},
    {"Russian Federation", "RU", "RUS", 643, 638, "RUSSIAN FEDERATION (RA VI)"},
    {"Rwanda", "RW", "RWA", 646, 139, "RWANDA"},
    {"Saint Barthélemy", "BL", "BLM", 652, 0, ""},
    {"Saint Helena, Ascension and Tristan da Cunha", "SH", "SHN", 654, 0, ""},
    {"Saint Kitts and Nevis", "KN", "KNA", 659, 0, ""},
    {"Saint Lucia", "LC", "LCA", 662, 421, "SAINT LUCIA"},
    {"Saint Martin (French part)", "MF", "MAF", 663, 0, ""},
    {"Saint Pierre and Miquelon", "PM", "SPM", 666, 0, ""},
    {"Saint Vincent and the Grenadines", "VC", "VCT", 670, 0, ""},
    {"Samoa", "WS", "WSM", 882, 514, "SAMOA"},
    {"San Marino", "SM", "SMR", 674, 0, ""},
    {"Sao Tome and Principe", "ST", "STP", 678, 140, "SAO TOME AND PRINCIPE"},
    {"Saudi Arabia", "SA", "SAU", 682, 227, "SAUDI ARABIA"},
    {"Senegal", "SN", "SEN", 686, 141, "SENEGAL"},
    {"Serbia", "RS", "SRB", 688, 639, "SERBIA"},
    {"Seychelles", "SC", "SYC", 690, 142, "SEYCHELLES"},
    {"Sierra Leone", "SL", "SLE", 694, 143, "SIERRA LEONE"},
    {"Singapore", "SG", "SGP", 702, 515, "SINGAPORE"},
    {"Sint Maarten (Dutch part)", "SX", "SXM", 534, 0, ""},
    {"Slovakia", "SK", "SVK", 703, 640, "SLOVAKIA"},
    {"Slovenia", "SI", "SVN", 705, 641, "SLOVENIA"},
    {"Solomon Islands", "SB", "SLB", 90, 516, "SOLOMON ISLANDS"},
    {"Somalia", "SO", "SOM", 706, 144, "SOMALIA"},
    {"South Africa", "ZA", "ZAF", 710, 145, "SOUTH AFRICA"},
    {"South Georgia and the South Sandwich Islands", "GS", "SGS", 239, 0, ""},
    {"South Sudan", "SS", "SSD", 728, 0, ""},
    {"Spain", "ES", "ESP", 724, 146, "SPAIN (RA I)"},
    {"Spain", "ES", "ESP", 724, 642, "SPAIN (RA VI)"},
    {"Sri Lanka", "LK", "LKA", 144, 228, "SRI LANKA"},
    {"Sudan", "SD", "SDN", 729, 147, "SUDAN"},
    {"Suriname", "SR", "SUR", 740, 310, "SURINAME"},
    {"Svalbard and Jan Mayen", "SJ", "SJM", 744, 0, ""},
    {"Sweden", "SE", "SWE", 752, 643, "SWEDEN"},
    {"Switzerland", "CH", "CHE", 756, 644, "SWITZERLAND"},
    {"Syrian Arab Republic", "SY", "SYR", 760, 645, "SYRIAN ARAB REPUBLIC"},
    {"Taiwan, Province of China", "TW", "TWN", 158, 0, ""},
    {"Tajikistan", "TJ", "TJK", 762, 229, "TAJIKISTAN"},
    {"Tanzania, United Republic of", "TZ", "TZA", 834, 0, ""},
    {"Thailand", "TH", "THA", 764, 230, "THAILAND"},
    {"Timor-Leste", "TL", "TLS", 626, 0, ""},
    {"Togo", "TG", "TGO", 768, 149, "TOGO"},
    {"Tokelau", "TK", "TKL", 772, 0, ""},
    {"Tonga", "TO", "TON", 776, 517, "TONGA"},
    {"Trinidad and Tobago", "TT", "TTO", 780, 422, "TRINIDAD AND TOBAGO"},
    {"Tunisia", "TN", "TUN", 788, 150, "TUNISIA"},
    {"Turkey", "TR", "TUR", 792, 647, "TURKEY"},
    {"Turkmenistan", "TM", "TKM", 795, 231, "TURKMENISTAN"},
    {"Turks and Caicos Islands", "TC", "TCA", 796, 0, ""},
    {"Tuvalu", "TV", "TUV", 798, 0, ""},
    {"Uganda", "UG", "UGA", 800, 151, "UGANDA"},
    {"Ukraine", "UA", "UKR", 804, 648, "UKRAINE"},
    {"United Arab Emirates", "AE", "ARE", 784, 232, "UNITED ARAB EMIRATES"},
    {"United Kingdom of Great Britain and Northern Ireland", "GB", "GBR", 826,
     152, "UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (RA I)"},
    {"United Kingdom of Great Britain and Northern Ireland", "GB", "GBR", 826,
     423, "UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (RA IV)"},
    {"United Kingdom of Great Britain and Northern Ireland", "GB", "GBR", 826,
     518, "UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (RA V)"},
    {"United Kingdom of Great Britain and Northern Ireland", "GB", "GBR", 826,
     649, "UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (RA VI)"},
    {"United States of America", "US", "USA", 840, 424,
     "UNITED STATES OF AMERICA (RA IV)"},
    {"United States of America", "US", "USA", 840, 519,
     "UNITED STATES OF AMERICA (RA V)"},
    {"United States Minor Outlying Islands", "UM", "UMI", 581, 0, ""},
    {"Uruguay", "UY", "URY", 858, 311, "URUGUAY"},
    {"Uzbekistan", "UZ", "UZB", 860, 233, "UZBEKISTAN"},
    {"Vanuatu", "VU", "VUT", 548, 520, "VANUATU"},
    {"Venezuela (Bolivarian Republic of)", "VE", "VEN", 862, 312,
     "VENEZUELA (BOLIVARIAN REPUBLIC OF)"},
    {"Viet Nam", "VN", "VNM", 704, 234, "VIET NAM"},
    {"Virgin Islands (British)", "VG", "VGB", 92, 0, ""},
    {"Virgin Islands (U.S.)", "VI", "VIR", 850, 0, ""},
    {"Wallis and Futuna", "WF", "WLF", 876, 0, ""},
    {"Western Sahara", "EH", "ESH", 732, 0, ""},
    {"Yemen", "YE", "YEM", 887, 225, "YEMEN"},
    {"Zambia", "ZM", "ZMB", 894, 154, "ZAMBIA"},
    {"Zimbabwe", "ZW", "ZWE", 716, 155, "ZIMBABWE"}

};

inline int bufrToIsocc(const int bufr_state_id) {
  int ret = 0;
  for (auto cc : country_codes) {
    if (cc.bufr_code == bufr_state_id) {
      ret = cc.iso_code;
      break;
    }
  }
  return ret;
}

#endif
