package com.jens.rhasspy.visualtool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Settings
{
	/*
	 * Unchangable stuff:
	 */
		public static final String programName = "RhasspyVisualConversationTool";
		final static String logFileName = programName + "LogFile.txt";
		final static int logFilesAmount = 1;
		final static int maxLogSize = Settings.logMaxSize * 1024 * 1024;
		final static boolean logAppend = true;
		public static final String settingsFileName = programName + "-settings.ini";
		public final static String settingsTtsLedStateVariable = "<state>";
		public final static String settingsTtsSoundFileVariable = "<soundFilePath>";
		public final static long notificationWaitPeriod = 60*60*24;
		public final static int programVersion = 1;
		public final static long sessionTimeout = 120000;
		
		public final static String jsonObjectStringSiteId = "siteId";
		public final static String jsonObjectStringSessionId = "sessionId";
		
		public final static String topicNameWakewordRecognized = "hermes/hotword/+/detected"; 
		public final static String topicNameDisplayText = "hermes/tts/displayText";
		public final static String topicNameDisplayOptions = "hermes/tts/displayOptions";
		public final static String topicNameIntentsRecognized = "hermes/intent/#";
		public final static String topicNameSayText = "hermes/tts/say";
		public final static String topicNameSayFinished = "hermes/tts/sayFinished";
		public static String topicNamePlaySoundFinished = "hermes/audioServer/" + jsonObjectStringSiteId + "/playFinished";
		public final static String topicNameSessionEnded = "hermes/dialogueManager/sessionEnded";
		
	/*
	 * Unchangable stuff:
	 */

		public static final boolean default_sslDebug = false;
		public static final String default_keystoreFilename = "keystore.jks";
		public static final String default_keystorePassword = "keystorePassword";
		public static final String default_dateFormat = "dd.MM.yyyy HH:mm:ss:SSSS";
		public static final boolean default_logFileEnabled = false;
		public static final String default_logFolderPath = "/var/log/homecontrol";
		public static final boolean default_hideWindowWhenIdle = true;
		public static final int default_logLevel = 3;
		public static final int default_logMaxSize = 5;		
		public static final String default_encryptionKey="choose_a_password";
		public static final String default_language = "en-US";
		public static final String default_intentMqttPath = "hermes/intent/#";
		public static final boolean default_notificationsEnabled = true;
		public static final int default_notificationVerbosity = 2;
		public static final String default_notificationFromAddress = "admin@localhost.com";
		public static final String default_notificationToAddress = "admin@localhost.com";
		public static final String default_notificationMailServer = "smtp.host.com";
		public static final int default_notificationMailServerPort = 25;
		public static final String default_notificationEncryptionType = "starttls";
		public static final boolean default_notificationAuthenticate = false;
		public static final String default_notificationUsername = "smtpUsername";
		public static final String default_notificationPassword = "smtpPassword";
		public static final String default_mqttClientServerHostname = "";
		public static final int default_mqttClientServerPort = 1883;
		public static final boolean default_mqttClientUseSsl = false;
		public static final boolean default_mqttClientUseAuthentication = false;
		public static final String default_mqttClientUsername = "";
		public static final String default_mqttClientPassword = "";
		public static final String default_ignoredSatellites = "master";
		public static final String default_siteId = "unknown";
		
		
	public static String intentMqttPath;
	public static String language;
	public static boolean sslDebug;
	public static String keystoreFilename;
	public static String keystorePassword;
	public static boolean hideWindowWhenIdle;
	
	public static String dateFormat = default_dateFormat;

	public static boolean logFileEnabled;
	public static String logFolderPath;
	public static int logLevel = 5;						// Set a default value as logging is performed before settings can be loaded.
	public static int logMaxSize = 5;						// Set a default value as logging is performed before settings can be loaded.

	public static String scriptsPath;
	
	public static boolean notificationsEnabled;
	public static int notificationVerbosity;
	public static String notificationFromAddress;
	public static String notificationToAddress;
	public static String notificationMailServer;
	public static int notificationMailServerPort;
	public static String notificationEncryptionType;
	public static boolean notificationAuthenticate;
	public static String notificationUsername;
	public static String notificationPassword;
	
	public static String mqttClientServerHostname;
	public static int mqttClientServerPort;
	public static boolean mqttClientUseSsl;
	public static boolean mqttClientUseAuthentication;
	public static String mqttClientUsername;
	public static String mqttClientPassword;	
	
//	public static String ignoredSatellites;
	
	public static String siteId;
		
	public static boolean readSettings()
	{
		File sFile = new File(settingsFileName);
		Miscellaneous.logEvent("Loading settings from " + sFile.getAbsolutePath(), 3);
		
		Properties prop = new Properties();		
		
		try
		{
			// Local settings from settings file
			prop.load(new FileInputStream(sFile));
						
			intentMqttPath = prop.getProperty("intentMqttPath", default_intentMqttPath);
			language = prop.getProperty("language", default_language);
			sslDebug = Boolean.parseBoolean(prop.getProperty("sslDebug", String.valueOf(default_sslDebug)));
			keystoreFilename = prop.getProperty("keystoreFilename", default_keystoreFilename);
			keystorePassword = prop.getProperty("keystorePassword", default_keystorePassword);
			hideWindowWhenIdle = Boolean.parseBoolean(prop.getProperty("hideWindowWhenIdle", String.valueOf(default_hideWindowWhenIdle)));

			logFileEnabled = Boolean.parseBoolean(prop.getProperty("logFileEnabled", String.valueOf(default_logFileEnabled)));
			logFolderPath = prop.getProperty("logFolderPath", String.valueOf(default_logFolderPath));
			logLevel = Integer.parseInt(prop.getProperty("logLevel", String.valueOf(default_logLevel)));
			logMaxSize = Integer.parseInt(prop.getProperty("logMaxSize", String.valueOf(default_logMaxSize)));

			notificationsEnabled = Boolean.parseBoolean(prop.getProperty("notificationsEnabled", String.valueOf(default_notificationsEnabled)));
			notificationVerbosity = Integer.parseInt(prop.getProperty("notificationVerbosity", String.valueOf(default_notificationVerbosity)));
			notificationFromAddress = prop.getProperty("notificationFromAddress", default_notificationFromAddress);
			notificationToAddress = prop.getProperty("notificationToAddress", default_notificationToAddress);
			notificationMailServer = prop.getProperty("notificationMailServer", default_notificationMailServer);
			notificationMailServerPort = Integer.parseInt(prop.getProperty("notificationMailServerPort", String.valueOf(default_notificationMailServerPort)));
			notificationEncryptionType = prop.getProperty("notificationEncryptionType", default_notificationEncryptionType);
			notificationAuthenticate = Boolean.parseBoolean(prop.getProperty("notificationAuthenticate", String.valueOf(default_notificationAuthenticate)));
			notificationUsername = prop.getProperty("notificationUsername", default_notificationUsername);
			notificationPassword = prop.getProperty("notificationPassword", default_notificationPassword);

			mqttClientServerHostname = prop.getProperty("mqttClientServerHostname", default_mqttClientServerHostname);
			mqttClientServerPort = Integer.parseInt(prop.getProperty("mqttClientServerPort", String.valueOf(default_mqttClientServerPort)));
			mqttClientUseSsl = Boolean.parseBoolean(prop.getProperty("mqttClientUseSsl", String.valueOf(default_mqttClientUseSsl)));
			mqttClientUseAuthentication = Boolean.parseBoolean(prop.getProperty("mqttClientUseAuthentication", String.valueOf(default_mqttClientUseAuthentication)));
			mqttClientUsername = prop.getProperty("mqttClientUsername", default_mqttClientUsername);
			mqttClientPassword = prop.getProperty("mqttClientPassword", default_mqttClientPassword);
//			ignoredSatellites = prop.getProperty("ignoredSatellites", default_ignoredSatellites);
			
			siteId = prop.getProperty("siteId", default_siteId);
			
			topicNamePlaySoundFinished = topicNamePlaySoundFinished.replace(jsonObjectStringSiteId, siteId); 
			
			if(mqttClientUseSsl)
				loadKeystore();
		}
		catch (FileNotFoundException e)
		{
			Miscellaneous.logEvent(Miscellaneous.getStackTraceAsString(e), 1);
		}
		catch (IOException e)
		{
			Miscellaneous.logEvent(Miscellaneous.getStackTraceAsString(e), 1);
		}

		return true;
	}
	
	static void loadKeystore()
	{
		if((new File(Settings.keystoreFilename).exists()))
		{
			Miscellaneous.logEvent("Loading keystore " + Settings.keystoreFilename, 3);
			System.setProperty("javax.net.ssl.keyStore", Settings.keystoreFilename);
		    System.setProperty("javax.net.ssl.keyStorePassword", Settings.keystorePassword);		    	
	    	System.setProperty("javax.net.ssl.trustStore", Settings.keystoreFilename);
	    	System.setProperty("javax.net.ssl.trustStorePassword", Settings.keystorePassword);
//	    	System.setProperty("javax.net.ssl.trustStoreType","JCEKS");
		}
		else
		{
			Miscellaneous.logEvent("Could not load keystore, file does not exist.", 3);
			System.exit(1);
		}
	}
}