use crate::error::Result;
use crate::utils::constants::*;
use owo_colors::OwoColorize;

pub const REGIONS: &[(u16, &str, &str, &str)] = &[
    (1, "Afghanistan", "AF", "AFG"),
    (2, "Albania", "AL", "ALB"),
    (3, "Algeria", "DZ", "DZA"),
    (4, "American Samoa", "AS", "ASM"),
    (5, "Andorra", "AD", "AND"),
    (6, "Angola", "AO", "AGO"),
    (7, "Anguilla", "AI", "AIA"),
    (8, "Antarctica", "AQ", "ATA"),
    (9, "Antigua and Barbuda", "AG", "ATG"),
    (10, "Argentina", "AR", "ARG"),
    (11, "Armenia", "AM", "ARM"),
    (12, "Aruba", "AW", "ABW"),
    (13, "Australia", "AU", "AUS"),
    (14, "Austria", "AT", "AUT"),
    (15, "Azerbaijan", "AZ", "AZE"),
    (16, "Bahamas", "BS", "BHS"),
    (17, "Bahrain", "BH", "BHR"),
    (18, "Bangladesh", "BD", "BGD"),
    (19, "Barbados", "BB", "BRB"),
    (20, "Belarus", "BY", "BLR"),
    (21, "Belgium", "BE", "BEL"),
    (22, "Belize", "BZ", "BLZ"),
    (23, "Benin", "BJ", "BEN"),
    (24, "Bermuda", "BM", "BMU"),
    (25, "Bhutan", "BT", "BTN"),
    (26, "Bolivia", "BO", "BOL"),
    (27, "Bosnia-Herzegovina", "BA", "BIH"),
    (28, "Botswana", "BW", "BWA"),
    (29, "Bouvet Island", "BV", "BVT"),
    (30, "Brazil", "BR", "BRA"),
    (31, "British Indian Ocean Territory", "IO", "IOT"),
    (32, "British Virgin Islands", "VG", "VGB"),
    (33, "Brunei Darussalam", "BN", "BRN"),
    (34, "Bulgaria", "BG", "BGR"),
    (35, "Burkina Faso", "BF", "BFA"),
    (36, "Burundi", "BI", "BDI"),
    (37, "Cambodia", "KH", "KHM"),
    (38, "Cameroon", "CM", "CMR"),
    (39, "Canada", "CA", "CAN"),
    (40, "Cape Verde", "CV", "CPV"),
    (41, "Cayman Islands", "KY", "CYM"),
    (42, "Central African Republic", "CF", "CAF"),
    (43, "Chad", "TD", "TCD"),
    (44, "Chile", "CL", "CHL"),
    (45, "China", "CN", "CHN"),
    (46, "Christmas Island", "CX", "CXR"),
    (47, "Cocos Islands", "CC", "CCK"),
    (48, "Colombia", "CO", "COL"),
    (49, "Comoros", "KM", "COM"),
    (50, "Congo, Democratic Republic of", "CD", "COD"),
    (51, "Congo", "CG", "COG"),
    (52, "Cook Islands", "CK", "COK"),
    (53, "Costa Rica", "CR", "CRI"),
    (54, "Ivory Coast", "CI", "CIV"),
    (55, "Cuba", "CU", "CUB"),
    (56, "Cyprus", "CY", "CYP"),
    (57, "Czech Republic", "CZ", "CZE"),
    (58, "Denmark", "DK", "DNK"),
    (59, "Djibouti", "DJ", "DJI"),
    (60, "Dominica", "DM", "DMA"),
    (61, "Dominican Republic", "DO", "DOM"),
    (62, "Ecuador", "EC", "ECU"),
    (63, "Egypt", "EG", "EGY"),
    (64, "El Salvador", "SV", "SLV"),
    (65, "Equatorial Guinea", "GQ", "GNQ"),
    (66, "Eritrea", "ER", "ERI"),
    (67, "Estonia", "EE", "EST"),
    (68, "Ethiopia", "ET", "ETH"),
    (69, "Faeroe Islands", "FO", "FRO"),
    (70, "Falkland Islands", "FK", "FLK"),
    (71, "Fiji", "FJ", "FJI"),
    (72, "Finland", "FI", "FIN"),
    (73, "France", "FR", "FRA"),
    (74, "French Guiana", "GF", "GUF"),
    (75, "French Polynesia", "PF", "PYF"),
    (76, "French Southern Territories", "TF", "ATF"),
    (77, "Gabon", "GA", "GAB"),
    (78, "Gambia", "GM", "GMB"),
    (79, "Georgia", "GE", "GEO"),
    (80, "Germany", "DE", "DEU"),
    (81, "Ghana", "GH", "GHA"),
    (82, "Gibraltar", "GI", "GIB"),
    (83, "Greece", "GR", "GRC"),
    (84, "Greenland", "GL", "GRL"),
    (85, "Grenada", "GD", "GRD"),
    (86, "Guadaloupe", "GP", "GLP"),
    (87, "Guam", "GU", "GUM"),
    (88, "Guatemala", "GT", "GTM"),
    (89, "Guinea", "GN", "GIN"),
    (90, "Guinea-Bissau", "GW", "GNB"),
    (91, "Guyana", "GY", "GUY"),
    (92, "Haiti", "HT", "HTI"),
    (93, "Heard and McDonald Islands", "HM", "HMD"),
    (94, "Holy See", "VA", "VAT"),
    (95, "Honduras", "HN", "HND"),
    (96, "Hong Kong, China", "HK", "HKG"),
    (97, "Croatia", "HR", "HRV"),
    (98, "Hungary", "HU", "HUN"),
    (99, "Iceland", "IS", "ISL"),
    (100, "India", "IN", "IND"),
    (101, "Indonesia", "ID", "IDN"),
    (102, "Iran", "IR", "IRN"),
    (103, "Iraq", "IQ", "IRQ"),
    (104, "Ireland", "IE", "IRL"),
    (105, "Israel", "IL", "ISR"),
    (106, "Italy", "IT", "ITA"),
    (107, "Jamaica", "JM", "JAM"),
    (108, "Japan", "JP", "JPN"),
    (109, "Jordan", "JO", "JOR"),
    (110, "Kazakhstan", "KZ", "KAZ"),
    (111, "Kenya", "KE", "KEN"),
    (112, "Kiribati", "KI", "KIR"),
    (113, "North Korea", "KP", "PRK"),
    (114, "South Korea", "KR", "KOR"),
    (115, "Kuwait", "KW", "KWT"),
    (116, "Kyrgyzstan", "KG", "KGZ"),
    (117, "Laos", "LA", "LAO"),
    (118, "Latvia", "LV", "LVA"),
    (119, "Lebanon", "LB", "LBN"),
    (120, "Lesotho", "LS", "LSO"),
    (121, "Liberia", "LR", "LBR"),
    (122, "Libya", "LY", "LBY"),
    (123, "Liechtenstein", "LI", "LIE"),
    (124, "Lithuania", "LT", "LTU"),
    (125, "Luxembourg", "LU", "LUX"),
    (126, "Macao, China", "MO", "MAC"),
    (127, "Macedonia", "MK", "MKD"),
    (128, "Madagascar", "MG", "MDG"),
    (129, "Malawi", "MW", "MWI"),
    (130, "Malaysia", "MY", "MYS"),
    (131, "Maldives", "MV", "MDV"),
    (132, "Mali", "ML", "MLI"),
    (133, "Malta", "MT", "MLT"),
    (134, "Marshall Islands", "MH", "MHL"),
    (135, "Martinique", "MQ", "MTQ"),
    (136, "Mauritania", "MR", "MRT"),
    (137, "Mauritius", "MU", "MUS"),
    (138, "Mayotte", "YT", "MYT"),
    (139, "Mexico", "MX", "MEX"),
    (140, "Micronesia", "FM", "FSM"),
    (141, "Moldova", "MD", "MDA"),
    (142, "Monaco", "MC", "MCO"),
    (143, "Mongolia", "MN", "MNG"),
    (144, "Montserrat", "MS", "MSR"),
    (145, "Morocco", "MA", "MAR"),
    (146, "Mozambique", "MZ", "MOZ"),
    (147, "Myanmar", "MM", "MMR"),
    (148, "Namibia", "NA", "NAM"),
    (149, "Nauru", "NR", "NRU"),
    (150, "Nepal", "NP", "NPL"),
    (151, "Netherlands Antilles", "AN", "ANT"),
    (152, "Netherlands", "NL", "NLD"),
    (153, "New Caledonia", "NC", "NCL"),
    (154, "New Zealand", "NZ", "NZL"),
    (155, "Nicaragua", "NI", "NIC"),
    (156, "Niger", "NE", "NER"),
    (157, "Nigeria", "NG", "NGA"),
    (158, "Niue", "NU", "NIU"),
    (159, "Norfolk Island", "NF", "NFK"),
    (160, "Northern Mariana Islands", "MP", "MNP"),
    (161, "Norway", "NO", "NOR"),
    (162, "Oman", "OM", "OMN"),
    (163, "Pakistan", "PK", "PAK"),
    (164, "Palau", "PW", "PLW"),
    (165, "Palestine", "PS", "PSE"),
    (166, "Panama", "PA", "PAN"),
    (167, "Papua New Guinea", "PG", "PNG"),
    (168, "Paraguay", "PY", "PRY"),
    (169, "Peru", "PE", "PER"),
    (170, "Philippines", "PH", "PHL"),
    (171, "Pitcairn Island", "PN", "PCN"),
    (172, "Poland", "PL", "POL"),
    (173, "Portugal", "PT", "PRT"),
    (174, "Puerto Rico", "PR", "PRI"),
    (175, "Qatar", "QA", "QAT"),
    (176, "Reunion", "RE", "REU"),
    (177, "Romania", "RO", "ROU"),
    (178, "Russia", "RU", "RUS"),
    (179, "Rwanda", "RW", "RWA"),
    (180, "St. Helena", "SH", "SHN"),
    (181, "St. Kitts and Nevis", "KN", "KNA"),
    (182, "St. Lucia", "LC", "LCA"),
    (183, "St. Pierre and Miquelon", "PM", "SPM"),
    (184, "St. Vincent and the Grenadines", "VC", "VCT"),
    (185, "Samoa", "WS", "WSM"),
    (186, "San Marino", "SM", "SMR"),
    (187, "Sao Tome and Principe", "ST", "STP"),
    (188, "Saudi Arabia", "SA", "SAU"),
    (189, "Senegal", "SN", "SEN"),
    (190, "Serbia", "RS", "SRB"),
    (191, "Seychelles", "SC", "SYC"),
    (192, "Sierra Leone", "SL", "SLE"),
    (193, "Singapore", "SG", "SGP"),
    (194, "Slovakia", "SK", "SVK"),
    (195, "Slovenia", "SI", "SVN"),
    (196, "Solomon Islands", "SB", "SLB"),
    (197, "Somalia", "SO", "SOM"),
    (198, "South Africa", "ZA", "ZAF"),
    (
        199,
        "South Georgia and the South Sandwich Islands",
        "GS",
        "SGS",
    ),
    (200, "Spain", "ES", "ESP"),
    (201, "Sri Lanka", "LK", "LKA"),
    (202, "Sudan", "SD", "SDN"),
    (203, "Suriname", "SR", "SUR"),
    (204, "Svalbard & Jan Mayen Islands", "SJ", "SJM"),
    (205, "Eswatini", "SZ", "SWZ"),
    (206, "Sweden", "SE", "SWE"),
    (207, "Switzerland", "CH", "CHE"),
    (208, "Syria", "SY", "SYR"),
    (209, "Chinese Taipei", "TW", "TWN"),
    (210, "Tajikistan", "TJ", "TJK"),
    (211, "Tanzania", "TZ", "TZA"),
    (212, "Thailand", "TH", "THA"),
    (213, "Timor-Leste", "TL", "TLS"),
    (214, "Togo", "TG", "TGO"),
    (215, "Tokelau", "TK", "TKL"),
    (216, "Tonga", "TO", "TON"),
    (217, "Trinidad and Tobago", "TT", "TTO"),
    (218, "Tunisia", "TN", "TUN"),
    (219, "Turkey", "TR", "TUR"),
    (220, "Turkmenistan", "TM", "TKM"),
    (221, "Turks and Caicos Islands", "TC", "TCA"),
    (222, "Tuvalu", "TV", "TUV"),
    (223, "US Virgin Islands", "VI", "VIR"),
    (224, "Uganda", "UG", "UGA"),
    (225, "Ukraine", "UA", "UKR"),
    (226, "United Arab Emirates", "AE", "ARE"),
    (227, "United Kingdom", "GB", "GBR"),
    (228, "United States Minor Outlying Islands", "UM", "UMI"),
    (229, "USA", "US", "USA"),
    (230, "Uruguay", "UY", "URY"),
    (231, "Uzbekistan", "UZ", "UZB"),
    (232, "Vanuatu", "VU", "VUT"),
    (233, "Venezuela", "VE", "VEN"),
    (234, "Vietnam", "VN", "VNM"),
    (235, "Wallis and Futuna Islands", "WF", "WLF"),
    (236, "Western Sahara", "EH", "ESH"),
    (237, "Yemen", "YE", "YEM"),
    (238, "Zambia", "ZM", "ZMB"),
    (239, "Zimbabwe", "ZW", "ZWE"),
    (240, "Montenegro", "ME", "MNE"),
    (241, "England", "EN", "ENG"),
    (242, "Northern Ireland", "NN", "NIR"),
    (243, "Scotland", "S1", "SCO"),
    (244, "Wales", "WA", "WAL"),
    (267, "Aland Islands", "AX", "ALA"),
    (269, "Saint Barthelemy", "BL", "BLM"),
    (271, "Guernsey", "GG", "GGY"),
    (273, "Isle of Man", "IM", "IMN"),
    (275, "Jersey", "JE", "JEY"),
    (277, "Saint Martin", "MF", "MAF"),
    (279, "Bonaire, Saint Eustatius and Saba", "BQ", "BES"),
    (281, "Curacao", "CW", "CUW"),
    (283, "Sint Maarten", "SX", "SXM"),
    (285, "South Sudan", "SS", "SSD"),
    (286, "Kosovo", "XK", "RKS"),
];

pub async fn handle_region() -> Result<()> {
    let name_width = REGIONS
        .iter()
        .map(|(_, name, _, _)| name.len())
        .max()
        .unwrap_or(0)
        .max(11); // "Region Name".len() is 11

    println!(
        "{:<id_w$}  {:<name_w$}  {:<iso2_w$}  {:<iso3_w$}",
        "ID".bold(),
        "Region Name".bold(),
        "ISO2".bold(),
        "ISO3".bold(),
        id_w = WIDTH_ID,
        name_w = name_width,
        iso2_w = 4,
        iso3_w = 4,
    );

    for (id, name, iso2, iso3) in REGIONS {
        println!(
            "{:<id_w$}  {:<name_w$}  {:<iso2_w$}  {:<iso3_w$}",
            id,
            name,
            iso2,
            iso3,
            id_w = WIDTH_ID,
            name_w = name_width,
            iso2_w = 4,
            iso3_w = 4,
        );
    }

    Ok(())
}
