package vindecoder

type rangeSegment struct {
	low     byte
	high    byte
	country string
}

// source: https://en.wikipedia.org/wiki/Vehicle_identification_number
//
// note ascending sort order: A-Z. then 1-9, then 0
var vinCountryCodeRangeMap = map[byte][]rangeSegment{
	'A': {
		rangeSegment{low: 'A', high: 'H', country: "South Africa"},
		rangeSegment{low: 'J', high: 'K', country: "Cote d'Ivoire"},
		rangeSegment{low: 'L', high: 'L', country: "algeria"},
		rangeSegment{low: 'M', high: 'N', country: "Cote d'Ivoire"},
		//		rangeSegment{low: 'P', high: 'P', country: ""},
		rangeSegment{low: 'R', high: 'R', country: "algeria"},
		//		rangeSegment{low: 'S', high: 'Z', country: ""},
		//		rangeSegment{low: '1', high: '9', country: ""},
		//		rangeSegment{low: '0', high: '0', country: ""}
	},
	'B': {
		rangeSegment{low: 'A', high: 'E', country: "SAngola"},
		rangeSegment{low: 'F', high: 'K', country: "Kenya"},
		rangeSegment{low: 'L', high: 'R', country: "Tanzania"},
		//      rangeSegment{low: 'S', high: '0', country: ""},
	},
	'C': {
		rangeSegment{low: 'A', high: 'E', country: "Benin"},
		rangeSegment{low: 'F', high: 'K', country: "Madagascar"},
		rangeSegment{low: 'L', high: 'R', country: "Tunisia"},
		//      rangeSegment{low: 'S', high: '0", "" },
	},
	'D': {
		rangeSegment{low: 'A', high: 'E', country: "Egypt"},
		rangeSegment{low: 'F', high: 'K', country: "Morocco"},
		rangeSegment{low: 'L', high: 'R', country: "Zambia"},
		//    	rangeSegment{low: 'S', high: '0', country: ""},
	},
	'E': {
		rangeSegment{low: 'A', high: 'E', country: "Ethiopia"},
		rangeSegment{low: 'F', high: 'K', country: "Mozambique"},
		//    	rangeSegment{low: 'L', high: '0', country: ""},
	},
	'F': {
		rangeSegment{low: 'A', high: 'E', country: "Ghana"},
		rangeSegment{low: 'F', high: 'K', country: "Nigeria"},
		//    	rangeSegment{low: 'L', high: '0', country: ""},
	},
	//	'G': {
	//    	rangeSegment{low: 'A', high: '0', country: ""},
	//	},
	//	'H': {
	//    	rangeSegment{low: 'A', high: '0', country: ""},
	//	},
	'J': {
		rangeSegment{low: 'A', high: 'Z', country: "Japan"},
		rangeSegment{low: '1', high: '9', country: "Japan"},
		rangeSegment{low: '0', high: '0', country: "Japan"},
	},
	'K': {
		rangeSegment{low: 'A', high: 'E', country: "Sri Lanka"},
		rangeSegment{low: 'F', high: 'K', country: "Israel"},
		rangeSegment{low: 'L', high: 'R', country: "Korea (South)"},
		rangeSegment{low: 'S', high: 'S', country: "Jordan"},
		rangeSegment{low: 'T', high: 'Z', country: "Kazakhstan"},
		rangeSegment{low: '1', high: '9', country: "Kazakhstan"},
		rangeSegment{low: '0', high: '0', country: "Kazakhstan"},
	},
	'L': {
		rangeSegment{low: 'A', high: 'Z', country: "China"},
		rangeSegment{low: '1', high: '9', country: "China"},
		rangeSegment{low: '0', high: '0', country: "China"},
	},
	'M': {
		rangeSegment{low: 'A', high: 'E', country: "India"},
		rangeSegment{low: 'F', high: 'K', country: "Indonesia"},
		rangeSegment{low: 'L', high: 'R', country: "Thailand"},
		rangeSegment{low: 'S', high: 'Y', country: "Myanmar"},
		rangeSegment{low: 'Z', high: 'Z', country: "India"},
		rangeSegment{low: '1', high: '9', country: "Myanmar"},
		rangeSegment{low: '0', high: '0', country: "Myanmar"},
	},
	'N': {
		rangeSegment{low: 'A', high: 'E', country: "Iran"},
		rangeSegment{low: 'F', high: 'K', country: "Pakistan"},
		rangeSegment{low: 'L', high: 'R', country: "Turkey"},
		//    	rangeSegment{low: 'S', high: '0', country: "" },
	},
	'P': {
		rangeSegment{low: 'A', high: 'E', country: "Philippines"},
		rangeSegment{low: 'F', high: 'K', country: "Singapore"},
		rangeSegment{low: 'L', high: 'R', country: "Malaysia"},
		//    	rangeSegment{low: 'S', high: '0', country: "" },
		rangeSegment{low: 'A', high: 'E', country: "United Arab Emirates"},
		rangeSegment{low: 'F', high: 'K', country: "Taiwan"},
	},
	'R': {
		rangeSegment{low: 'L', high: 'R', country: "Vietnam"},
		rangeSegment{low: 'S', high: 'Z', country: "Saudi Arabia"},
		rangeSegment{low: '1', high: '9', country: "Saudi Arabia"},
		rangeSegment{low: '0', high: '0', country: "Saudi Arabia"},
	},
	'S': {
		rangeSegment{low: 'A', high: 'M', country: "United Kingdom"},
		rangeSegment{low: 'N', high: 'T', country: "Germany"},
		rangeSegment{low: 'U', high: 'Z', country: "Poland"},
		rangeSegment{low: '1', high: '4', country: "Latvia"},
		//    	rangeSegment{low: '5', high: '0', country: "" },
	},
	'T': {
		rangeSegment{low: 'A', high: 'H', country: "Switzerland"},
		rangeSegment{low: 'J', high: 'P', country: "Czech Republic"},
		rangeSegment{low: 'R', high: 'V', country: "Hungary"},
		rangeSegment{low: 'W', high: 'Z', country: "Portugal"},
		rangeSegment{low: '1', high: '1', country: "Portugal"},
		//    	rangeSegment{low: '2', high: '0', country: "" },
	},
	'U': {
		//    	rangeSegment{low: 'A', high: 'G', country: "" },
		rangeSegment{low: 'H', high: 'M', country: "Denmark"},
		rangeSegment{low: 'N', high: 'T', country: "Ireland"},
		rangeSegment{low: 'U', high: 'Z', country: "Romania"},
		//    	rangeSegment{low: '1', high: '4', country: "" },
		rangeSegment{low: '5', high: '7', country: "Slovakia"},
		//    	rangeSegment{low: '8', high: '0', country: "" },
	},
	'V': {
		rangeSegment{low: 'A', high: 'E', country: "Austria"},
		rangeSegment{low: 'F', high: 'R', country: "France"},
		rangeSegment{low: 'S', high: 'W', country: "Spain"},
		rangeSegment{low: 'X', high: 'Z', country: "Serbia"},
		rangeSegment{low: '1', high: '2', country: "Serbia"},
		rangeSegment{low: '3', high: '5', country: "Croatia"},
		rangeSegment{low: '6', high: '9', country: "Estonia"},
		rangeSegment{low: '0', high: '0', country: "Estonia"},
	},
	'W': {
		rangeSegment{low: 'A', high: 'Z', country: "Germany"},
		rangeSegment{low: '1', high: '9', country: "Germany"},
		rangeSegment{low: '0', high: '0', country: "Germany"},
	},
	'X': {
		rangeSegment{low: 'A', high: 'E', country: "Bulgaria"},
		rangeSegment{low: 'F', high: 'K', country: "Greece"},
		rangeSegment{low: 'L', high: 'R', country: "Netherlands"},
		rangeSegment{low: 'S', high: 'W', country: "Russia"},
		rangeSegment{low: 'X', high: 'Z', country: "Luxembourg"},
		rangeSegment{low: '1', high: '2', country: "Luxembourg"},
		rangeSegment{low: '3', high: '9', country: "Russia"},
		rangeSegment{low: '0', high: '0', country: "Russia"},
	},
	'Y': {
		rangeSegment{low: 'A', high: 'E', country: "Belgium"},
		rangeSegment{low: 'F', high: 'K', country: "Finland"},
		rangeSegment{low: 'L', high: 'R', country: "Malta"},
		rangeSegment{low: 'S', high: 'W', country: "Sweden"},
		rangeSegment{low: 'X', high: 'Z', country: "Norway"},
		rangeSegment{low: '1', high: '2', country: "Norway"},
		rangeSegment{low: '3', high: '5', country: "Belarus"},
		rangeSegment{low: '6', high: '9', country: "Ukraine"},
		rangeSegment{low: '0', high: '0', country: "Ukraine"},
	},
	'Z': {
		rangeSegment{low: 'A', high: 'R', country: "Italy"},
		//    	rangeSegment{low: 'S', high: 'W', country: "" },
		rangeSegment{low: 'X', high: 'Z', country: "Slovenia"},
		rangeSegment{low: '1', high: '2', country: "Slovenia"},
		rangeSegment{low: '3', high: '5', country: "Lithuania"},
		//    	rangeSegment{low: '6', high: '0', country: "" },
	},
	'1': {
		rangeSegment{low: 'A', high: 'Z', country: "United States"},
		rangeSegment{low: '1', high: '9', country: "United States"},
		rangeSegment{low: '0', high: '0', country: "United States"},
	},
	'2': {
		rangeSegment{low: 'A', high: 'Z', country: "Canada"},
		rangeSegment{low: '1', high: '9', country: "Canada"},
		rangeSegment{low: '0', high: '0', country: "Canada"},
	},
	'3': {
		rangeSegment{low: 'A', high: 'W', country: "Mexico"},
		rangeSegment{low: 'X', high: 'Z', country: "Costa Rica"},
		rangeSegment{low: '1', high: '7', country: "Costa Rica"},
		rangeSegment{low: '8', high: '9', country: "Cayman Islands"},
		//    	rangeSegment{low: '0', high: '0', country: "" },
	},
	'4': {
		rangeSegment{low: 'A', high: 'Z', country: "United States"},
		rangeSegment{low: '1', high: '9', country: "United States"},
		rangeSegment{low: '0', high: '0', country: "United States"},
	},
	'5': {
		rangeSegment{low: 'A', high: 'Z', country: "United States"},
		rangeSegment{low: '1', high: '9', country: "United States"},
		rangeSegment{low: '0', high: '0', country: "United States"},
	},
	'6': {
		rangeSegment{low: 'A', high: 'Z', country: "Australia"},
		rangeSegment{low: '1', high: '9', country: "Australia"},
		rangeSegment{low: '0', high: '0', country: "Australia"},
	},
	'7': {
		rangeSegment{low: 'A', high: 'Z', country: "New Zealand"},
		rangeSegment{low: '1', high: '9', country: "New Zealand"},
		rangeSegment{low: '0', high: '0', country: "New Zealand"},
	},
	'8': {
		rangeSegment{low: 'A', high: 'E', country: "Argentina"},
		rangeSegment{low: 'F', high: 'K', country: "Chile"},
		rangeSegment{low: 'L', high: 'R', country: "Ecuador"},
		rangeSegment{low: 'S', high: 'W', country: "Peru"},
		rangeSegment{low: 'X', high: 'Z', country: "Venezuela"},
		rangeSegment{low: '1', high: '2', country: "Venezuela"},
		rangeSegment{low: '2', high: '2', country: "Bolivia"},
		//    	rangeSegment{low: '3', high: '0', country: "" },
	},
	'9': {
		rangeSegment{low: 'A', high: 'E', country: "Brazil"},
		rangeSegment{low: 'F', high: 'K', country: "Colombia"},
		rangeSegment{low: 'L', high: 'R', country: "Paraguay"},
		rangeSegment{low: 'S', high: 'W', country: "Uruguay"},
		rangeSegment{low: 'X', high: 'Z', country: "Trinidad & Tobago"},
		rangeSegment{low: '1', high: '2', country: "Trinidad & Tobago"},
		rangeSegment{low: '3', high: '9', country: "Brazil"},
		//    	rangeSegment{low: '0', high: '0', country: "" }
	},
}

func LookupCountry(countryCode string) string {
	var key = countryCode[0]
	var elem = countryCode[1]

	segments, found := vinCountryCodeRangeMap[key]
	if !found {
		return countryCode
	}

	for _, segment := range segments {
		if segment.low <= elem && elem <= segment.high {
			return segment.country
		}
	}

	return countryCode
}
