import { Translate } from '../translate/translate.service';

export interface Country {
	code: string;
	name: string;
}

export interface Region {
	code: string;
	name: string;
}

export class Geo {
	private static countries: Country[] = [];
	private static regions: { [k: string]: Region[] } = {};

	static getCountryName(code: string) {
		const country = this.getCountries().find(c => c.code === (code || '').toLowerCase());
		return country ? country.name : undefined;
	}

	static getRegionName(country: string, code: string) {
		const regions = this.getRegions(country);
		if (!regions) {
			console.log('no region');
			return undefined;
		}

		const region = regions.find(c => c.code === (code || '').toLowerCase());
		return region ? region.name : undefined;
	}

	static getCountries() {
		if (!this.countries.length) {
			this.countries = [
				{ code: 'af', name: Translate.$gettext('Afghanistan') },
				{ code: 'ax', name: Translate.$gettext('Aland Islands') },
				{ code: 'al', name: Translate.$gettext('Albania') },
				{ code: 'dz', name: Translate.$gettext('Algeria') },
				{ code: 'as', name: Translate.$gettext('American Samoa') },
				{ code: 'ad', name: Translate.$gettext('Andorra') },
				{ code: 'ao', name: Translate.$gettext('Angola') },
				{ code: 'ai', name: Translate.$gettext('Anguilla') },
				{ code: 'aq', name: Translate.$gettext('Antarctica') },
				{ code: 'ag', name: Translate.$gettext('Antigua and Barbuda') },
				{ code: 'ar', name: Translate.$gettext('Argentina') },
				{ code: 'am', name: Translate.$gettext('Armenia') },
				{ code: 'aw', name: Translate.$gettext('Aruba') },
				{ code: 'au', name: Translate.$gettext('Australia') },
				{ code: 'at', name: Translate.$gettext('Austria') },
				{ code: 'az', name: Translate.$gettext('Azerbaijan') },
				{ code: 'bs', name: Translate.$gettext('Bahamas') },
				{ code: 'bh', name: Translate.$gettext('Bahrain') },
				{ code: 'bd', name: Translate.$gettext('Bangladesh') },
				{ code: 'bb', name: Translate.$gettext('Barbados') },
				{ code: 'by', name: Translate.$gettext('Belarus') },
				{ code: 'be', name: Translate.$gettext('Belgium') },
				{ code: 'bz', name: Translate.$gettext('Belize') },
				{ code: 'bj', name: Translate.$gettext('Benin') },
				{ code: 'bm', name: Translate.$gettext('Bermuda') },
				{ code: 'bt', name: Translate.$gettext('Bhutan') },
				{ code: 'bo', name: Translate.$gettext('Bolivia') },
				{
					code: 'bq',
					name: Translate.$gettext('Bonaire, Saint Eustatius and Saba'),
				},
				{ code: 'ba', name: Translate.$gettext('Bosnia and Herzegovina') },
				{ code: 'bw', name: Translate.$gettext('Botswana') },
				{ code: 'bv', name: Translate.$gettext('Bouvet Island') },
				{ code: 'br', name: Translate.$gettext('Brazil') },
				{
					code: 'io',
					name: Translate.$gettext('British Indian Ocean Territory'),
				},
				{ code: 'vg', name: Translate.$gettext('British Virgin Islands') },
				{ code: 'bn', name: Translate.$gettext('Brunei') },
				{ code: 'bg', name: Translate.$gettext('Bulgaria') },
				{ code: 'bf', name: Translate.$gettext('Burkina Faso') },
				{ code: 'bi', name: Translate.$gettext('Burundi') },
				{ code: 'kh', name: Translate.$gettext('Cambodia') },
				{ code: 'cm', name: Translate.$gettext('Cameroon') },
				{ code: 'ca', name: Translate.$gettext('Canada') },
				{ code: 'cv', name: Translate.$gettext('Cape Verde') },
				{ code: 'ky', name: Translate.$gettext('Cayman Islands') },
				{ code: 'cf', name: Translate.$gettext('Central African Republic') },
				{ code: 'td', name: Translate.$gettext('Chad') },
				{ code: 'cl', name: Translate.$gettext('Chile') },
				{ code: 'cn', name: Translate.$gettext('China') },
				{ code: 'cx', name: Translate.$gettext('Christmas Island') },
				{ code: 'cc', name: Translate.$gettext('Cocos Islands') },
				{ code: 'co', name: Translate.$gettext('Colombia') },
				{ code: 'km', name: Translate.$gettext('Comoros') },
				{ code: 'ck', name: Translate.$gettext('Cook Islands') },
				{ code: 'cr', name: Translate.$gettext('Costa Rica') },
				{ code: 'hr', name: Translate.$gettext('Croatia') },
				{ code: 'cu', name: Translate.$gettext('Cuba') },
				{ code: 'cw', name: Translate.$gettext('Curacao') },
				{ code: 'cy', name: Translate.$gettext('Cyprus') },
				{ code: 'cz', name: Translate.$gettext('Czech Republic') },
				{
					code: 'cd',
					name: Translate.$gettext('Democratic Republic of the Congo'),
				},
				{ code: 'dk', name: Translate.$gettext('Denmark') },
				{ code: 'dj', name: Translate.$gettext('Djibouti') },
				{ code: 'dm', name: Translate.$gettext('Dominica') },
				{ code: 'do', name: Translate.$gettext('Dominican Republic') },
				{ code: 'tl', name: Translate.$gettext('East Timor') },
				{ code: 'ec', name: Translate.$gettext('Ecuador') },
				{ code: 'eg', name: Translate.$gettext('Egypt') },
				{ code: 'sv', name: Translate.$gettext('El Salvador') },
				{ code: 'gq', name: Translate.$gettext('Equatorial Guinea') },
				{ code: 'er', name: Translate.$gettext('Eritrea') },
				{ code: 'ee', name: Translate.$gettext('Estonia') },
				{ code: 'et', name: Translate.$gettext('Ethiopia') },
				{ code: 'fk', name: Translate.$gettext('Falkland Islands') },
				{ code: 'fo', name: Translate.$gettext('Faroe Islands') },
				{ code: 'fj', name: Translate.$gettext('Fiji') },
				{ code: 'fi', name: Translate.$gettext('Finland') },
				{ code: 'fr', name: Translate.$gettext('France') },
				{ code: 'gf', name: Translate.$gettext('French Guiana') },
				{ code: 'pf', name: Translate.$gettext('French Polynesia') },
				{ code: 'tf', name: Translate.$gettext('French Southern Territories') },
				{ code: 'ga', name: Translate.$gettext('Gabon') },
				{ code: 'gm', name: Translate.$gettext('Gambia') },
				{ code: 'ge', name: Translate.$gettext('Georgia') },
				{ code: 'de', name: Translate.$gettext('Germany') },
				{ code: 'gh', name: Translate.$gettext('Ghana') },
				{ code: 'gi', name: Translate.$gettext('Gibraltar') },
				{ code: 'gr', name: Translate.$gettext('Greece') },
				{ code: 'gl', name: Translate.$gettext('Greenland') },
				{ code: 'gd', name: Translate.$gettext('Grenada') },
				{ code: 'gp', name: Translate.$gettext('Guadeloupe') },
				{ code: 'gu', name: Translate.$gettext('Guam') },
				{ code: 'gt', name: Translate.$gettext('Guatemala') },
				{ code: 'gg', name: Translate.$gettext('Guernsey') },
				{ code: 'gn', name: Translate.$gettext('Guinea') },
				{ code: 'gw', name: Translate.$gettext('Guinea-Bissau') },
				{ code: 'gy', name: Translate.$gettext('Guyana') },
				{ code: 'ht', name: Translate.$gettext('Haiti') },
				{
					code: 'hm',
					name: Translate.$gettext('Heard Island and McDonald Islands'),
				},
				{ code: 'hn', name: Translate.$gettext('Honduras') },
				{ code: 'hk', name: Translate.$gettext('Hong Kong') },
				{ code: 'hu', name: Translate.$gettext('Hungary') },
				{ code: 'is', name: Translate.$gettext('Iceland') },
				{ code: 'in', name: Translate.$gettext('India') },
				{ code: 'id', name: Translate.$gettext('Indonesia') },
				{ code: 'ir', name: Translate.$gettext('Iran') },
				{ code: 'iq', name: Translate.$gettext('Iraq') },
				{ code: 'ie', name: Translate.$gettext('Ireland') },
				{ code: 'im', name: Translate.$gettext('Isle of Man') },
				{ code: 'il', name: Translate.$gettext('Israel') },
				{ code: 'it', name: Translate.$gettext('Italy') },
				{ code: 'ci', name: Translate.$gettext('Ivory Coast') },
				{ code: 'jm', name: Translate.$gettext('Jamaica') },
				{ code: 'jp', name: Translate.$gettext('Japan') },
				{ code: 'je', name: Translate.$gettext('Jersey') },
				{ code: 'jo', name: Translate.$gettext('Jordan') },
				{ code: 'kz', name: Translate.$gettext('Kazakhstan') },
				{ code: 'ke', name: Translate.$gettext('Kenya') },
				{ code: 'ki', name: Translate.$gettext('Kiribati') },
				{ code: 'xk', name: Translate.$gettext('Kosovo') },
				{ code: 'kw', name: Translate.$gettext('Kuwait') },
				{ code: 'kg', name: Translate.$gettext('Kyrgyzstan') },
				{ code: 'la', name: Translate.$gettext('Laos') },
				{ code: 'lv', name: Translate.$gettext('Latvia') },
				{ code: 'lb', name: Translate.$gettext('Lebanon') },
				{ code: 'ls', name: Translate.$gettext('Lesotho') },
				{ code: 'lr', name: Translate.$gettext('Liberia') },
				{ code: 'ly', name: Translate.$gettext('Libya') },
				{ code: 'li', name: Translate.$gettext('Liechtenstein') },
				{ code: 'lt', name: Translate.$gettext('Lithuania') },
				{ code: 'lu', name: Translate.$gettext('Luxembourg') },
				{ code: 'mo', name: Translate.$gettext('Macao') },
				{ code: 'mk', name: Translate.$gettext('Macedonia') },
				{ code: 'mg', name: Translate.$gettext('Madagascar') },
				{ code: 'mw', name: Translate.$gettext('Malawi') },
				{ code: 'my', name: Translate.$gettext('Malaysia') },
				{ code: 'mv', name: Translate.$gettext('Maldives') },
				{ code: 'ml', name: Translate.$gettext('Mali') },
				{ code: 'mt', name: Translate.$gettext('Malta') },
				{ code: 'mh', name: Translate.$gettext('Marshall Islands') },
				{ code: 'mq', name: Translate.$gettext('Martinique') },
				{ code: 'mr', name: Translate.$gettext('Mauritania') },
				{ code: 'mu', name: Translate.$gettext('Mauritius') },
				{ code: 'yt', name: Translate.$gettext('Mayotte') },
				{ code: 'mx', name: Translate.$gettext('Mexico') },
				{ code: 'fm', name: Translate.$gettext('Micronesia') },
				{ code: 'md', name: Translate.$gettext('Moldova') },
				{ code: 'mc', name: Translate.$gettext('Monaco') },
				{ code: 'mn', name: Translate.$gettext('Mongolia') },
				{ code: 'me', name: Translate.$gettext('Montenegro') },
				{ code: 'ms', name: Translate.$gettext('Montserrat') },
				{ code: 'ma', name: Translate.$gettext('Morocco') },
				{ code: 'mz', name: Translate.$gettext('Mozambique') },
				{ code: 'mm', name: Translate.$gettext('Myanmar') },
				{ code: 'na', name: Translate.$gettext('Namibia') },
				{ code: 'nr', name: Translate.$gettext('Nauru') },
				{ code: 'np', name: Translate.$gettext('Nepal') },
				{ code: 'nl', name: Translate.$gettext('Netherlands') },
				{ code: 'nc', name: Translate.$gettext('New Caledonia') },
				{ code: 'nz', name: Translate.$gettext('New Zealand') },
				{ code: 'ni', name: Translate.$gettext('Nicaragua') },
				{ code: 'ne', name: Translate.$gettext('Niger') },
				{ code: 'ng', name: Translate.$gettext('Nigeria') },
				{ code: 'nu', name: Translate.$gettext('Niue') },
				{ code: 'nf', name: Translate.$gettext('Norfolk Island') },
				{ code: 'kp', name: Translate.$gettext('North Korea') },
				{ code: 'mp', name: Translate.$gettext('Northern Mariana Islands') },
				{ code: 'no', name: Translate.$gettext('Norway') },
				{ code: 'om', name: Translate.$gettext('Oman') },
				{ code: 'pk', name: Translate.$gettext('Pakistan') },
				{ code: 'pw', name: Translate.$gettext('Palau') },
				{ code: 'ps', name: Translate.$gettext('Palestinian Territory') },
				{ code: 'pa', name: Translate.$gettext('Panama') },
				{ code: 'pg', name: Translate.$gettext('Papua New Guinea') },
				{ code: 'py', name: Translate.$gettext('Paraguay') },
				{ code: 'pe', name: Translate.$gettext('Peru') },
				{ code: 'ph', name: Translate.$gettext('Philippines') },
				{ code: 'pn', name: Translate.$gettext('Pitcairn') },
				{ code: 'pl', name: Translate.$gettext('Poland') },
				{ code: 'pt', name: Translate.$gettext('Portugal') },
				{ code: 'pr', name: Translate.$gettext('Puerto Rico') },
				{ code: 'qa', name: Translate.$gettext('Qatar') },
				{ code: 'cg', name: Translate.$gettext('Republic of the Congo') },
				{ code: 're', name: Translate.$gettext('Reunion') },
				{ code: 'ro', name: Translate.$gettext('Romania') },
				{ code: 'ru', name: Translate.$gettext('Russia') },
				{ code: 'rw', name: Translate.$gettext('Rwanda') },
				{ code: 'bl', name: Translate.$gettext('Saint Barthelemy') },
				{ code: 'sh', name: Translate.$gettext('Saint Helena') },
				{ code: 'kn', name: Translate.$gettext('Saint Kitts and Nevis') },
				{ code: 'lc', name: Translate.$gettext('Saint Lucia') },
				{ code: 'mf', name: Translate.$gettext('Saint Martin') },
				{ code: 'pm', name: Translate.$gettext('Saint Pierre and Miquelon') },
				{
					code: 'vc',
					name: Translate.$gettext('Saint Vincent and the Grenadines'),
				},
				{ code: 'ws', name: Translate.$gettext('Samoa') },
				{ code: 'sm', name: Translate.$gettext('San Marino') },
				{ code: 'st', name: Translate.$gettext('Sao Tome and Principe') },
				{ code: 'sa', name: Translate.$gettext('Saudi Arabia') },
				{ code: 'sn', name: Translate.$gettext('Senegal') },
				{ code: 'rs', name: Translate.$gettext('Serbia') },
				{ code: 'sc', name: Translate.$gettext('Seychelles') },
				{ code: 'sl', name: Translate.$gettext('Sierra Leone') },
				{ code: 'sg', name: Translate.$gettext('Singapore') },
				{ code: 'sx', name: Translate.$gettext('Sint Maarten') },
				{ code: 'sk', name: Translate.$gettext('Slovakia') },
				{ code: 'si', name: Translate.$gettext('Slovenia') },
				{ code: 'sb', name: Translate.$gettext('Solomon Islands') },
				{ code: 'so', name: Translate.$gettext('Somalia') },
				{ code: 'za', name: Translate.$gettext('South Africa') },
				{
					code: 'gs',
					name: Translate.$gettext('South Georgia and the South Sandwich Islands'),
				},
				{ code: 'kr', name: Translate.$gettext('South Korea') },
				{ code: 'ss', name: Translate.$gettext('South Sudan') },
				{ code: 'es', name: Translate.$gettext('Spain') },
				{ code: 'lk', name: Translate.$gettext('Sri Lanka') },
				{ code: 'sd', name: Translate.$gettext('Sudan') },
				{ code: 'sr', name: Translate.$gettext('Suriname') },
				{ code: 'sj', name: Translate.$gettext('Svalbard and Jan Mayen') },
				{ code: 'sz', name: Translate.$gettext('Swaziland') },
				{ code: 'se', name: Translate.$gettext('Sweden') },
				{ code: 'ch', name: Translate.$gettext('Switzerland') },
				{ code: 'sy', name: Translate.$gettext('Syria') },
				{ code: 'tw', name: Translate.$gettext('Taiwan') },
				{ code: 'tj', name: Translate.$gettext('Tajikistan') },
				{ code: 'tz', name: Translate.$gettext('Tanzania') },
				{ code: 'th', name: Translate.$gettext('Thailand') },
				{ code: 'tg', name: Translate.$gettext('Togo') },
				{ code: 'tk', name: Translate.$gettext('Tokelau') },
				{ code: 'to', name: Translate.$gettext('Tonga') },
				{ code: 'tt', name: Translate.$gettext('Trinidad and Tobago') },
				{ code: 'tn', name: Translate.$gettext('Tunisia') },
				{ code: 'tr', name: Translate.$gettext('Turkey') },
				{ code: 'tm', name: Translate.$gettext('Turkmenistan') },
				{ code: 'tc', name: Translate.$gettext('Turks and Caicos Islands') },
				{ code: 'tv', name: Translate.$gettext('Tuvalu') },
				{ code: 'vi', name: Translate.$gettext('U.S. Virgin Islands') },
				{ code: 'ug', name: Translate.$gettext('Uganda') },
				{ code: 'ua', name: Translate.$gettext('Ukraine') },
				{ code: 'ae', name: Translate.$gettext('United Arab Emirates') },
				{ code: 'gb', name: Translate.$gettext('United Kingdom') },
				{ code: 'us', name: Translate.$gettext('United States') },
				{
					code: 'um',
					name: Translate.$gettext('United States Minor Outlying Islands'),
				},
				{ code: 'uy', name: Translate.$gettext('Uruguay') },
				{ code: 'uz', name: Translate.$gettext('Uzbekistan') },
				{ code: 'vu', name: Translate.$gettext('Vanuatu') },
				{ code: 'va', name: Translate.$gettext('Vatican') },
				{ code: 've', name: Translate.$gettext('Venezuela') },
				{ code: 'vn', name: Translate.$gettext('Vietnam') },
				{ code: 'wf', name: Translate.$gettext('Wallis and Futuna') },
				{ code: 'eh', name: Translate.$gettext('Western Sahara') },
				{ code: 'ye', name: Translate.$gettext('Yemen') },
				{ code: 'zm', name: Translate.$gettext('Zambia') },
				{ code: 'zw', name: Translate.$gettext('Zimbabwe') },
			];
		}

		return this.countries;
	}

	static getRegions(country: string): Region[] | undefined {
		if (!this.regions[country]) {
			if (country === 'us') {
				this.regions[country] = [
					{ code: 'al', name: Translate.$gettext('Alabama') },
					{ code: 'ak', name: Translate.$gettext('Alaska') },
					{ code: 'az', name: Translate.$gettext('Arizona') },
					{ code: 'ar', name: Translate.$gettext('Arkansas') },
					{ code: 'ca', name: Translate.$gettext('California') },
					{ code: 'co', name: Translate.$gettext('Colorado') },
					{ code: 'ct', name: Translate.$gettext('Connecticut') },
					{ code: 'de', name: Translate.$gettext('Delaware') },
					{ code: 'fl', name: Translate.$gettext('Florida') },
					{ code: 'ga', name: Translate.$gettext('Georgia') },
					{ code: 'hi', name: Translate.$gettext('Hawaii') },
					{ code: 'id', name: Translate.$gettext('Idaho') },
					{ code: 'il', name: Translate.$gettext('Illinois') },
					{ code: 'in', name: Translate.$gettext('Indiana') },
					{ code: 'ia', name: Translate.$gettext('Iowa') },
					{ code: 'ks', name: Translate.$gettext('Kansas') },
					{ code: 'ky', name: Translate.$gettext('Kentucky') },
					{ code: 'la', name: Translate.$gettext('Louisiana') },
					{ code: 'me', name: Translate.$gettext('Maine') },
					{ code: 'md', name: Translate.$gettext('Maryland') },
					{ code: 'ma', name: Translate.$gettext('Massachusetts') },
					{ code: 'mi', name: Translate.$gettext('Michigan') },
					{ code: 'mn', name: Translate.$gettext('Minnesota') },
					{ code: 'ms', name: Translate.$gettext('Mississippi') },
					{ code: 'mo', name: Translate.$gettext('Missouri') },
					{ code: 'mt', name: Translate.$gettext('Montana') },
					{ code: 'ne', name: Translate.$gettext('Nebraska') },
					{ code: 'nv', name: Translate.$gettext('Nevada') },
					{ code: 'nh', name: Translate.$gettext('New Hampshire') },
					{ code: 'nj', name: Translate.$gettext('New Jersey') },
					{ code: 'nm', name: Translate.$gettext('New Mexico') },
					{ code: 'ny', name: Translate.$gettext('New York') },
					{ code: 'nc', name: Translate.$gettext('North Carolina') },
					{ code: 'nd', name: Translate.$gettext('North Dakota') },
					{ code: 'oh', name: Translate.$gettext('Ohio') },
					{ code: 'ok', name: Translate.$gettext('Oklahoma') },
					{ code: 'or', name: Translate.$gettext('Oregon') },
					{ code: 'pa', name: Translate.$gettext('Pennsylvania') },
					{ code: 'ri', name: Translate.$gettext('Rhode Island') },
					{ code: 'sc', name: Translate.$gettext('South Carolina') },
					{ code: 'sd', name: Translate.$gettext('South Dakota') },
					{ code: 'tn', name: Translate.$gettext('Tennessee') },
					{ code: 'tx', name: Translate.$gettext('Texas') },
					{ code: 'ut', name: Translate.$gettext('Utah') },
					{ code: 'vt', name: Translate.$gettext('Vermont') },
					{ code: 'va', name: Translate.$gettext('Virginia') },
					{ code: 'wa', name: Translate.$gettext('Washington') },
					{ code: 'wv', name: Translate.$gettext('West Virginia') },
					{ code: 'wi', name: Translate.$gettext('Wisconsin') },
					{ code: 'wy', name: Translate.$gettext('Wyoming') },
					{ code: 'dc', name: Translate.$gettext('Washington D.C.') },
					{ code: 'as', name: Translate.$gettext('American Samoa') },
					{ code: 'gu', name: Translate.$gettext('Guam') },
					{ code: 'mp', name: Translate.$gettext('Northern Mariana Islands') },
					{ code: 'pr', name: Translate.$gettext('Puerto Rico') },
					{ code: 'vi', name: Translate.$gettext('U.S. Virgin Islands') },
				];
			} else if (country === 'ca') {
				this.regions[country] = [
					{ code: 'on', name: 'Ontario' },
					{ code: 'qc', name: 'Quebec' },
					{ code: 'ns', name: 'Nova Scotia' },
					{ code: 'nb', name: 'New Brunswick' },
					{ code: 'mb', name: 'Manitoba' },
					{ code: 'bc', name: 'British Columbia' },
					{ code: 'pe', name: 'Prince Edward Island' },
					{ code: 'sk', name: 'Saskatchewan' },
					{ code: 'ab', name: 'Alberta' },
					{ code: 'nl', name: 'Newfoundland and Labrador' },
					{ code: 'nt', name: 'Northwest Territories' },
					{ code: 'yt', name: 'Yukon' },
					{ code: 'nu', name: 'Nunavut' },
				];
			}
		}

		return this.regions[country];
	}
}
