import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime as dt
import time

metody1 = [["EM-kC", "EM-vM", "MM-kC", "MM-vM", "Adaboost"], ["NS", "HyT-EM", "FreMEn", "Histogram", "Průměr"]]
parametry1 = [["30", "25", "1", "6", "1"], ["4", "1", "3", "24", ""]]
trenovaci = "../../../data/tunnelers/doplneno/"

for i in [0, 4]:
	for kr in range(0,2):
		metody = metody1[kr]
		parametry = parametry1[kr]
		fig, ax = plt.subplots()
		print(trenovaci+"test_all_"+str(i)+".txt")
		times = np.loadtxt(trenovaci+"test_all_"+str(i)+".txt")
		dates=[dt.datetime.fromtimestamp(ts) for ts in times[:,0]]
		data = times[:,1]

		xfmt = md.DateFormatter('%Y-%m-%d')
		ax.xaxis.set_major_formatter(xfmt)
		ax.scatter(dates, data, label="data", s=2)

		for j in range(0, len(metody)):
			fajl = str(i)+metody[j]
			if i == 0:
				fajl = fajl+"_"+parametry[j]+".txt"
			else:
				fajl = fajl+"_model.txt"
			print(fajl)
			predikce = np.loadtxt(fajl)
			ax.plot(dates, predikce[:,1], label=metody[j]+" "+parametry[j])

		#if i == 0:
		plt.subplots_adjust(bottom=0.25, right=0.8)
		#else:
		#	plt.subplots_adjust(bottom=0.2, right=0.75)
		ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
		ax.grid(axis="both")
		
		xt = None
		#if i == 0:
		#	xt = np.arange(np.min(times[:,0]),np.max(times[:,0]),86400*7)
		#else:
		xt = np.arange(np.min(times[:,0]),np.max(times[:,0]),86400)
		xt = xt - np.mod(xt, 86400) + 86400
		ax.xaxis.set_ticks([dt.datetime.fromtimestamp(ts) for ts in xt])
		plt.xticks(rotation=90)
		plt.xlim(dt.datetime.fromtimestamp(max(np.min(times[:,0]), np.max(times[:,0])-608400)), dt.datetime.fromtimestamp(np.max(times[:,0])))
		
		ax.set_ylabel('data')
		if i == 0:
			ax.set_title('Úloha s vytížeností: Trénovací sada (výřez)')
		else:
			ax.set_title('Úloha s vytížeností: Testovací sada '+str(i))
		plt.show()

"""
	file=fopen(argv[2],"r");
	string test = string(argv[2]);
	ofstream myfile0("predictions/"+test.substr(test.length()-5, 1)+string(argv[3])+"_"+string(argv[4])+".txt");

	while (feof(file)==0){
		fscanf(file,"%i\n",&testingTime);
		float prediction = temporalModel->predict(testingTime);
		myfile0 << testingTime << " " << prediction << std::endl;
		predictions[testingLength++] = prediction;
	}
	fclose(file);
	myfile0.close();
"""
