cv = require "opencv4nodejs"
pointToCountryCF = require "./index.coffee"
pointToCountryJS = require "./index.js"

lon = -124.04
lat = 45.75
console.log pointToCountryCF(lon, lat, true)
console.log pointToCountryJS(lon, lat, true)
process.exit()

countryCodes = [
	"abw",
	"afg",
	"ago",
	"aia",
	"ala",
	"alb",
	"and",
	"are",
	"arg",
	"arm",
	"asm",
	"ata",
	"atf",
	"atg",
	"aus",
	"aut",
	"aze",
	"bdi",
	"bel",
	"ben",
	"bfa",
	"bgd",
	"bgr",
	"bhr",
	"bhs",
	"bih",
	"blm",
	"blr",
	"blz",
	"bmu",
	"bol",
	"bra",
	"brb",
	"brn",
	"btn",
	"bvt",
	"bwa",
	"caf",
	"can",
	"cck",
	"che",
	"chl",
	"chn",
	"civ",
	"cmr",
	"cod",
	"cog",
	"cok",
	"col",
	"com",
	"cpv",
	"cri",
	"cub",
	"cxr",
	"cym",
	"cyp",
	"cze",
	"deu",
	"dji",
	"dma",
	"dnk",
	"dom",
	"dza",
	"ecu",
	"egy",
	"eri",
	"esh",
	"esp",
	"est",
	"eth",
	"fin",
	"fji",
	"flk",
	"fra",
	"fro",
	"fsm",
	"gab",
	"gbr",
	"geo",
	"ggy",
	"gha",
	"gib",
	"gin",
	"glp",
	"gmb",
	"gnb",
	"gnq",
	"grc",
	"grd",
	"grl",
	"gtm",
	"guf",
	"gum",
	"guy",
	"hkg",
	"hmd",
	"hnd",
	"hrv",
	"hti",
	"hun",
	"idn",
	"imn",
	"ind",
	"iot",
	"irl",
	"irn",
	"irq",
	"isl",
	"isr",
	"ita",
	"jam",
	"jey",
	"jor",
	"jpn",
	"kaz",
	"ken",
	"kgz",
	"khm",
	"kir",
	"kna",
	"kor",
	"kwt",
	"lao",
	"lbn",
	"lbr",
	"lby",
	"lca",
	"lie",
	"lka",
	"lso",
	"ltu",
	"lux",
	"lva",
	"mac",
	"maf",
	"mar",
	"mco",
	"mda",
	"mdg",
	"mdv",
	"mex",
	"mhl",
	"mkd",
	"mli",
	"mlt",
	"mmr",
	"mne",
	"mng",
	"mnp",
	"moz",
	"mrt",
	"msr",
	"mtq",
	"mus",
	"mwi",
	"mys",
	"myt",
	"nam",
	"ncl",
	"ner",
	"nfk",
	"nga",
	"nic",
	"niu",
	"nld",
	"nor",
	"npl",
	"nru",
	"nzl",
	"omn",
	"pak",
	"pan",
	"pcn",
	"per",
	"phl",
	"plw",
	"png",
	"pol",
	"pri",
	"prk",
	"prt",
	"pry",
	"pse",
	"pyf",
	"qat",
	"reu",
	"rou",
	"rus",
	"rwa",
	"sau",
	"sdn",
	"sen",
	"sgp",
	"sgs",
	"shn",
	"sjm",
	"slb",
	"sle",
	"slv",
	"smr",
	"som",
	"spm",
	"srb",
	"stp",
	"sur",
	"svk",
	"svn",
	"swe",
	"swz",
	"syc",
	"syr",
	"tca",
	"tcd",
	"tgo",
	"tha",
	"tjk",
	"tkl",
	"tkm",
	"tls",
	"ton",
	"tto",
	"tun",
	"tur",
	"tuv",
	"twn",
	"tza",
	"uga",
	"ukr",
	"umi",
	"ury",
	"usa",
	"uzb",
	"vat",
	"vct",
	"ven",
	"vgb",
	"vir",
	"vnm",
	"vut",
	"wlf",
	"wsm",
	"yem",
	"zaf",
	"zmb",
	"zwe",
	"usa-ak",
	"usa-al",
	"usa-ar",
	"usa-az",
	"usa-ca",
	"usa-co",
	"usa-ct",
	"usa-dc",
	"usa-de",
	"usa-fl",
	"usa-ga",
	"usa-hi",
	"usa-ia",
	"usa-id",
	"usa-il",
	"usa-in",
	"usa-ks",
	"usa-ky",
	"usa-la",
	"usa-ma",
	"usa-md",
	"usa-me",
	"usa-mi",
	"usa-mn",
	"usa-mo",
	"usa-ms",
	"usa-mt",
	"usa-nc",
	"usa-nd",
	"usa-ne",
	"usa-nh",
	"usa-nj",
	"usa-nm",
	"usa-nv",
	"usa-ny",
	"usa-oh",
	"usa-ok",
	"usa-or",
	"usa-pa",
	"usa-pr",
	"usa-ri",
	"usa-sc",
	"usa-sd",
	"usa-tn",
	"usa-tx",
	"usa-ut",
	"usa-va",
	"usa-vt",
	"usa-wa",
	"usa-wi",
	"usa-wv",
	"usa-wy"
]

cca3Color = {}
for code in countryCodes
	cca3Color[code] = [
		Math.round 255 * Math.random()
		Math.round 255 * Math.random()
		Math.round 255 * Math.random()
	]

step  = 1

cols = Math.round((2 * 180 + 1) / step)
rows = Math.round((2 * 90 + 1) / step)
console.log cols + " x " + rows + " = " + cols * rows

img = new cv.Mat rows, cols, cv.CV_8UC3

# time = Date.now()
# for lon in [-180..180] by step
# 	for lat in [-90..90] by step
# 		x = Math.round (lat +  90) / step
# 		y = Math.round (lon + 180) / step

# 		country = pointToCountryJS lon, lat, true
# 		# if not country
# 		# 	country = pointToCountryCF lon, lat, true
# 		# else
# 		# 	country = null

# 		cca3 = country?.cca3?.toLowerCase()
# 		color = cca3Color[cca3] or [0, 0, 0]

# 		img.set (rows - 1 - x), y, color
# 	break
# 	console.log lon
# console.log Date.now() - time

# cv.imwrite "countries_js.jpg", img



timesCF = []
timesJS = []
stats = {true: 0, false: 0}
# for lon in [-180..180] by step
# 	for lat in [-90..90] by step
for lon in [-180..180] by step
	for lat in [35..45] by step

		#
		# ACCURACY COMPARISON
		#

		# countryCF = pointToCountryCF lon, lat, false
		# countryJS = pointToCountryJS lon, lat, false
		# if countryCF?.cca2 is countryJS?.cca2
		# 	stats.true++
		# else
		# 	stats.false++

		countryCF = pointToCountryCF lon, lat, true
		if countryCF
			countryJS = pointToCountryJS lon, lat, true
			if countryCF?.cca2 is countryJS?.cca2
				stats.true++
			else
				stats.false++

		# countryCF = pointToCountryCF lon, lat, false
		# if not countryCF
		# 	countryCF = pointToCountryCF lon, lat, true
		# 	if countryCF
		# 		countryJS = pointToCountryJS lon, lat, true
		# 		if countryCF?.cca2 is countryJS?.cca2
		# 			stats.true++
		# 		else
		# 			console.log countryCF.name.common, countryJS.name.common, lon, lat
		# 			stats.false++

		#
		# RUN TIME COMPARISON
		#

		# time = Date.now()
		# countryJS = pointToCountryJS lon, lat, false
		# timesJS.push Date.now() - time
		# time = Date.now()
		# countryCF = pointToCountryCF lon, lat, false
		# timesCF.push Date.now() - time

		# countryCF = pointToCountryCF lon, lat, false
		# if not countryCF
		# 	countryCF = pointToCountryCF lon, lat, true
		# 	if countryCF
		# 		time = Date.now()
		# 		countryJS = pointToCountryJS lon, lat, true
		# 		timesJS.push Date.now() - time
		# 		time = Date.now()
		# 		countryCF = pointToCountryCF lon, lat, true
		# 		timesCF.push Date.now() - time

	console.log stats

	# if timesCF.length
	# 	console.log timesCF.length
	# 	timesCF.sort (a, b)-> a - b
	# 	timesJS.sort (a, b)-> a - b
	# 	console.log (timesCF.reduce (a, b)-> a + b) / timesCF.length, timesCF[Math.round(timesCF.length/2)]
	# 	console.log (timesJS.reduce (a, b)-> a + b) / timesJS.length, timesJS[Math.round(timesJS.length/2)]
	# 	console.log()

	console.log lon

console.log stats