package e2e

import (
	"context"
	"fmt"
	"time"

	"github.com/tidbcloud/account/central/utils"
	"github.com/tidbcloud/account/internel/controllers/central/location"
)

func ExecIPInfoCase() {
	CheckIpInfoCorrectness()
	//CheckIPList()
}

func CheckIPList() {
	ctx := context.Background()
	ips := []string{
		"150.195.208.67",
		"13.200.0.172",
		"138.113.121.131",
	}
	for _, ip := range ips {
		ipinfoCode, _ := utils.IP2CountryCode(ctx, ip)
		fmt.Printf("ipinfo result, ip: %s, country: %s", ip, ipinfoCode)

		geoCode, _ := location.QueryCountryCodeByIP(ctx, ip)
		fmt.Printf("geoip result, ip: %s, country: %s", ip, geoCode)
	}
}

func CheckIpInfoCorrectness() {
	//var wIpInfo = map[string]string{}
	var wGeoIp = map[string]string{}
	//var wDiff = map[string]string{}
	ctx := context.Background()

	for c, ip := range CountryIP {
		//code, _ := utils.IP2CountryCode(ctx, ip)
		//if code != c {
		//	wIpInfo[ip] = fmt.Sprintf("origin: %s; ipinfo: %s", c, code)
		//}

		r, _ := location.QueryCountryCodeByIP(ctx, ip)
		if r != c {
			wGeoIp[ip] = fmt.Sprintf("origin: %s; geoip: %s", c, r)
		}

		//if code != r {
		//	wDiff[ip] = fmt.Sprintf("origin: %s, ipinfo: %s, geoip: %s", c, code, r)
		//}
		time.Sleep(time.Millisecond * 50)
	}

	pass := true
	if len(wGeoIp) > 0 {
		pass = false
	}
	SaveCase("Central_IPInfo", "CheckIpInfoCorrectness", CaseDetail{
		Url:       "",
		Method:    "GET",
		ReqHeader: nil,
		ReqBody:   "",
		Pass:      pass,
		RspCode:   0,
		RspData:   "",
		Extra: map[string]interface{}{
			"geoip": wGeoIp,
		},
	})
}

var CountryIP = map[string]string{
	"AD": "23.230.104.186",  // Andorra
	"AE": "2.16.44.81",      // United Arab Emirates
	"AF": "5.62.63.234",     // Afghanistan
	"AG": "23.132.144.105",  // Antigua and Barbuda
	"AI": "45.138.10.45",    // Anguilla
	"AL": "17.118.196.107",  // Albania
	"AM": "2.17.249.145",    // Armenia
	"AO": "41.63.167.248",   // Angola
	"AQ": "5.175.190.178",   // Antarctica
	"AR": "1.178.49.185",    // Argentina
	"AS": "38.101.165.117",  // American Samoa
	"AT": "2.16.16.69",      // Austria
	"AU": "1.0.4.95",        // Australia
	"AW": "45.39.106.14",    // Aruba
	"AX": "45.138.10.61",    // Åland Islands
	"AZ": "5.10.255.212",    // Azerbaijan
	"BA": "5.43.95.37",      // Bosnia and Herzegovina
	"BB": "23.236.3.98",     // Barbados
	"BD": "14.1.102.199",    // Bangladesh
	"BE": "2.17.107.43",     // Belgium
	"BF": "41.78.51.206",    // Burkina Faso
	"BG": "2.20.45.196",     // Bulgaria
	"BH": "3.2.32.179",      // Bahrain
	"BI": "2.18.11.99",      // Burundi
	"BJ": "41.74.3.121",     // Benin
	"BL": "23.135.232.146",  // Saint Barthélemy
	"BM": "31.47.95.236",    // Bermuda
	"BN": "43.225.42.90",    // Brunei
	"BO": "34.100.5.121",    // Bolivia
	"BQ": "104.28.172.110",  // Bonaire, Sint Eustatius, and Saba
	"BR": "1.178.45.249",    // Brazil
	"BS": "23.128.212.87",   // Bahamas
	"BT": "5.182.196.171",   // Bhutan
	"BV": "185.193.124.134", // Bouvet Island
	"BW": "38.51.241.139",   // Botswana
	"BY": "5.44.44.132",     // Belarus
	"BZ": "44.61.3.14",      // Belize
	"CA": "1.178.92.184",    // Canada
	"CC": "45.138.10.5",     // Cocos (Keeling) Islands
	"CD": "5.175.77.141",    // DR Congo
	"CF": "41.78.120.151",   // Central African Republic
	"CG": "41.75.68.249",    // Congo Republic
	"CH": "2.16.12.229",     // Switzerland
	"CI": "38.26.249.3",     // Ivory Coast
	"CK": "14.137.40.48",    // Cook Islands
	"CL": "2.18.21.72",      // Chile
	"CM": "2.16.134.49",     // Cameroon
	"CN": "1.0.1.134",       // China
	"CO": "2.19.35.160",     // Colombia
	"CR": "2.59.173.135",    // Costa Rica
	"CU": "45.39.106.226",   // Cuba
	"CV": "23.230.104.120",  // Cabo Verde
	"CW": "38.65.58.33",     // Curaçao
	"CX": "45.138.10.26",    // Christmas Island
	"CY": "2.56.120.127",    // Cyprus
	"CZ": "2.16.2.105",      // Czechia
	"DE": "1.178.10.132",    // Germany
	"DJ": "41.189.248.142",  // Djibouti
	"DK": "2.16.63.58",      // Denmark
	"DM": "23.186.240.208",  // Dominica
	"DO": "5.183.81.217",    // Dominican Republic
	"DZ": "5.180.66.11",     // Algeria
	"EC": "8.242.88.84",     // Ecuador
	"EE": "2.57.220.157",    // Estonia
	"EG": "2.21.128.51",     // Egypt
	"EH": "46.243.148.248",  // Western Sahara
	"ER": "57.82.120.117",   // Eritrea
	"ES": "1.178.245.1",     // Spain
	"ET": "45.39.106.140",   // Ethiopia
	"FI": "2.20.1.241",      // Finland
	"FJ": "14.137.38.84",    // Fiji
	"FK": "45.138.10.30",    // Falkland Islands
	"FM": "43.248.159.185",  // Federated States of Micronesia
	"FO": "46.227.117.241",  // Faroe Islands
	"FR": "1.178.90.81",     // France
	"GA": "41.78.243.235",   // Gabon
	"GB": "1.178.94.246",    // United Kingdom
	"GD": "23.128.4.195",    // Grenada
	"GE": "2.57.60.17",      // Georgia
	"GF": "5.102.79.95",     // French Guiana
	"GG": "5.62.84.230",     // Guernsey
	"GH": "2.16.77.35",      // Ghana
	"GI": "2.58.11.112",     // Gibraltar
	"GL": "37.18.47.57",     // Greenland
	"GM": "41.76.9.12",      // Gambia
	"GN": "41.77.188.28",    // Guinea
	"GP": "5.102.76.161",    // Guadeloupe
	"GQ": "23.230.104.103",  // Equatorial Guinea
	"GR": "2.16.19.144",     // Greece
	"GT": "2.57.255.33",     // Guatemala
	"GU": "8.3.114.106",     // Guam
	"GW": "57.82.162.218",   // Guinea-Bissau
	"GY": "38.246.40.46",    // Guyana
	"HK": "1.32.205.241",    // Hong Kong
	"HM": "147.66.5.196",    // Heard and McDonald Islands
	"HN": "23.216.144.215",  // Honduras
	"HR": "2.56.9.253",      // Croatia
	"HT": "38.92.116.120",   // Haiti
	"HU": "2.58.171.224",    // Hungary
	"ID": "2.16.128.95",     // Indonesia
	"IE": "1.178.7.131",     // Ireland
	"IL": "2.22.233.87",     // Israel
	"IM": "5.62.83.181",     // Isle of Man
	"IN": "1.7.120.150",     // India
	"IO": "67.199.159.19",   // British Indian Ocean Territory
	"IQ": "5.1.105.204",     // Iraq
	"IR": "2.57.3.51",       // Iran
	"IS": "2.56.243.134",    // Iceland
	"IT": "2.16.17.75",      // Italy
	"JE": "5.35.166.21",     // Jersey
	"JM": "17.118.110.23",   // Jamaica
	"JO": "2.17.26.79",      // Jordan
	"JP": "1.0.27.108",      // Japan
	"KE": "2.17.161.106",    // Kenya
	"KG": "5.57.9.143",      // Kyrgyzstan
	"KH": "5.28.36.197",     // Cambodia
	"KI": "45.138.10.158",   // Kiribati
	"KM": "41.194.33.205",   // Comoros
	"KN": "23.131.208.143",  // St Kitts and Nevis
	"KP": "5.62.56.161",     // North Korea
	"KR": "1.11.164.81",     // South Korea
	"KW": "5.182.132.112",   // Kuwait
	"KY": "23.188.0.132",    // Cayman Islands
	"KZ": "2.57.99.190",     // Kazakhstan
	"LA": "38.135.66.79",    // Laos
	"LB": "5.8.151.243",     // Lebanon
	"LC": "24.92.147.189",   // Saint Lucia
	"LI": "5.34.251.72",     // Liechtenstein
	"LK": "14.1.79.42",      // Sri Lanka
	"LR": "41.57.84.177",    // Liberia
	"LS": "41.76.22.197",    // Lesotho
	"LT": "2.56.3.3",        // Lithuania
	"LU": "2.18.250.83",     // Luxembourg
	"LV": "2.58.18.145",     // Latvia
	"LY": "5.63.4.230",      // Libya
	"MA": "23.27.214.104",   // Morocco
	"MC": "34.99.173.112",   // Monaco
	"MD": "5.10.217.116",    // Moldova
	"ME": "31.204.198.244",  // Montenegro
	"MF": "5.102.77.18",     // Saint Martin
	"MG": "41.63.135.38",    // Madagascar
	"MH": "45.138.10.102",   // Marshall Islands
	"MK": "5.32.177.80",     // North Macedonia
	"ML": "41.73.107.24",    // Mali
	"MM": "23.199.72.199",   // Myanmar
	"MN": "14.0.59.213",     // Mongolia
	"MO": "17.91.140.47",    // Macao
	"MP": "8.3.117.131",     // Northern Mariana Islands
	"MQ": "5.102.73.50",     // Martinique
	"MR": "41.138.148.108",  // Mauritania
	"MS": "45.62.191.182",   // Montserrat
	"MT": "5.39.28.194",     // Malta
	"MU": "23.230.104.135",  // Mauritius
	"MV": "23.230.104.153",  // Maldives
	"MW": "41.70.99.252",    // Malawi
	"MX": "2.21.11.18",      // Mexico
	"MY": "1.9.143.206",     // Malaysia
	"MZ": "5.252.185.64",    // Mozambique
	"NA": "41.63.204.38",    // Namibia
	"NC": "27.122.3.28",     // New Caledonia
	"NE": "41.78.117.111",   // Niger
	"NF": "45.138.10.18",    // Norfolk Island
	"NG": "8.35.57.239",     // Nigeria
	"NI": "5.62.56.149",     // Nicaragua
	"NL": "1.118.32.161",    // The Netherlands
	"NO": "2.18.174.245",    // Norway
	"NP": "27.34.4.85",      // Nepal
	"NR": "43.230.6.36",     // Nauru
	"NU": "14.1.76.173",     // Niue
	"NZ": "3.2.32.34",       // New Zealand
	"OM": "2.56.253.1",      // Oman
	"PA": "5.145.169.90",    // Panama
	"PE": "2.23.233.157",    // Peru
	"PF": "23.230.104.244",  // French Polynesia
	"PG": "14.137.32.32",    // Papua New Guinea
	"PH": "1.37.92.32",      // Philippines
	"PK": "14.1.104.236",    // Pakistan
	"PL": "2.16.172.231",    // Poland
	"PM": "5.252.113.138",   // Saint Pierre and Miquelon
	"PN": "206.83.126.95",   // Pitcairn Islands
	"PR": "5.62.56.194",     // Puerto Rico
	"PS": "1.178.113.192",   // Palestine
	"PT": "2.16.65.236",     // Portugal
	"PW": "38.150.39.65",    // Palau
	"PY": "24.152.42.183",   // Paraguay
	"QA": "2.23.170.32",     // Qatar
	"RE": "5.57.113.78",     // Réunion
	"RO": "2.17.116.22",     // Romania
	"RS": "5.22.175.200",    // Serbia
	"RU": "2.16.20.81",      // Russia
	"RW": "41.60.150.202",   // Rwanda
	"SA": "2.59.55.77",      // Saudi Arabia
	"SB": "14.137.34.209",   // Solomon Islands
	"SC": "5.61.209.59",     // Seychelles
	"SD": "41.67.5.65",      // Sudan
	"SE": "1.178.93.1",      // Sweden
	"SG": "1.21.233.186",    // Singapore
	"SH": "150.48.186.44",   // Saint Helena
	"SI": "2.58.48.121",     // Slovenia
	"SJ": "62.16.187.88",    // Svalbard and Jan Mayen
	"SK": "2.57.64.215",     // Slovakia
	"SL": "41.78.85.186",    // Sierra Leone
	"SM": "5.62.63.97",      // San Marino
	"SN": "41.83.27.177",    // Senegal
	"SO": "41.78.72.140",    // Somalia
	"SR": "45.11.243.52",    // Suriname
	"SS": "38.43.72.158",    // South Sudan
	"ST": "57.82.65.181",    // São Tomé and Príncipe
	"SV": "38.7.12.214",     // El Salvador
	"SX": "45.138.10.81",    // Sint Maarten
	"SY": "5.0.165.226",     // Syria
	"SZ": "41.77.236.114",   // Eswatini
	"TC": "45.39.106.21",    // Turks and Caicos Islands
	"TD": "41.242.152.120",  // Chad
	"TF": "154.120.133.185", // French Southern Territories
	"TG": "41.78.137.113",   // Togo
	"TH": "1.0.181.5",       // Thailand
	"TJ": "37.98.152.16",    // Tajikistan
	"TK": "27.96.29.241",    // Tokelau
	"TL": "43.243.122.166",  // Timor-Leste
	"TM": "57.90.151.72",    // Turkmenistan
	"TN": "23.230.104.6",    // Tunisia
	"TO": "14.137.33.240",   // Tonga
	"TR": "2.16.151.180",    // Türkiye
	"TT": "23.3.72.132",     // Trinidad and Tobago
	"TV": "14.137.42.252",   // Tuvalu
	"TW": "1.32.194.136",    // Taiwan
	"TZ": "2.17.250.237",    // Tanzania
	"UA": "2.21.89.149",     // Ukraine
	"UG": "2.17.248.76",     // Uganda
	"UM": "64.9.239.118",    // U.S. Outlying Islands
	"US": "1.178.1.68",      // United States
	"UY": "5.145.169.98",    // Uruguay
	"UZ": "2.57.149.63",     // Uzbekistan
	"VA": "2.56.112.19",     // Vatican City
	"VC": "23.170.80.47",    // St Vincent and Grenadines
	"VE": "8.242.232.221",   // Venezuela
	"VG": "2.56.145.254",    // British Virgin Islands
	"VI": "8.26.17.152",     // U.S. Virgin Islands
	"VN": "1.52.91.205",     // Vietnam
	"VU": "14.137.37.106",   // Vanuatu
	"WF": "27.125.192.192",  // Wallis and Futuna
	"WS": "43.241.165.103",  // Samoa
	"XK": "5.206.233.87",    // Kosovo
	"YE": "5.100.166.241",   // Yemen
	"YT": "5.102.72.19",     // Mayotte
	"ZA": "2.16.34.53",      // South Africa
	"ZM": "41.60.1.118",     // Zambia
	"ZW": "5.252.184.48",    // Zimbabwe
}
