package com.jens.rhasspy.shoppinglist;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Settings
{
	public static Map<String,String> languageBlock = new HashMap<>();
	
	// Settings from file
	public static String databaseUsername;
	public static String databasePassword;
	public static String databaseName;
	public static String[] databaseServerNames;
	public static int databasePort;
	public static String logFolderPath;
	public static String notificationFromAddress;
	public static String[] notificationToAddresses;
	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 language;
	public static int logLevel = 1;						// Set a default value as logging is performed before settings can be loaded.
	
	// Constants not user modifiable.
	public static final String programName = "ShoppingList";
	public static final String logFileName = programName + "LogFile.txt";
	public static final String settingsFileName = programName + "-settings.ini";
	public static final String dateFormat = "dd.MM.yyyy HH:mm:ss:SSSS";
	public static final int logMaxSize = 5000;						// Set a default value as logging is performed before settings can be loaded.
	public static final boolean notificationsEnabled = true;
	public static final int notificationVerbosity = 2;
	
	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));
						
			databaseUsername = prop.getProperty("databaseUsername", "error");
			databasePassword = prop.getProperty("databasePassword", "error");
			databaseName = prop.getProperty("databaseName", "error");
			databaseServerNames = prop.getProperty("databaseServerNames", "error").split(",");
			databasePort = Integer.parseInt(prop.getProperty("databasePort", "error"));
			logFolderPath = prop.getProperty("logFolderPath", "error");
			notificationFromAddress = prop.getProperty("notificationFromAddress", "error");
			notificationToAddresses = prop.getProperty("notificationToAddresses", "error").split(",");
			notificationMailServer = prop.getProperty("notificationMailServer", "error");
			notificationMailServerPort = Integer.parseInt(prop.getProperty("notificationMailServerPort", "error"));
			notificationEncryptionType = prop.getProperty("notificationEncryptionType", "error");
			notificationAuthenticate = Boolean.parseBoolean(prop.getProperty("notificationEncryptionType", "error"));
			notificationUsername = prop.getProperty("notificationUsername", "error");
			notificationPassword = prop.getProperty("notificationPassword", "error");
			language = prop.getProperty("language", "error");
			logLevel = Integer.parseInt(prop.getProperty("logLevel", "1"));
		}
		catch (FileNotFoundException e)
		{
			Start.exitWithError(Settings.languageBlock.get("settingsFileNotFound") + " " + settingsFileName);
		}
		catch (IOException e)
		{
			Start.exitWithError(Settings.languageBlock.get("settingsCouldNotBeRead") + " " + settingsFileName);
		}
		
		fillLanguageVariable();

		return true;
	}
	
	static void fillLanguageVariable()
	{
		if(language != null && language.equalsIgnoreCase("de"))
		{
			languageBlock.put("dbCouldNotConnect", "Konnte nicht zur Datenbank verbinden.");
			languageBlock.put("dbDriverNotFound", "Datenbanktreiber nicht gefunden.");
			languageBlock.put("settingsFileNotFound", "Konfigurationsdatei nicht gefunden.");
			languageBlock.put("settingsCouldNotBeRead", "Konfigurationsdatei konnte nicht gelesen werden.");
			languageBlock.put("noActionDefined", "Keine Aktion angegeben.");
			languageBlock.put("couldNotCreateList", "Konnte keine Liste anlegen.");
			languageBlock.put("productNotFound", "Produkt nicht gefunden.");
			languageBlock.put("couldNotAddProdToList", "Produkt konnte nicht auf die Liste gesetzt werden.");
			languageBlock.put("noProdSpecified", "Kein Produkt angegeben.");
			languageBlock.put("couldNotRemoveProdFromList", "Produkt konnte nicht von Liste entfernt werden.");
			languageBlock.put("couldNotSendList", "Liste konnte nicht verschickt werden.");
			languageBlock.put("couldNotCreateNewList", "Neue Liste konnte nicht erstellt werden.");
		}
		else
		{
			languageBlock.put("dbCouldNotConnect", "Could not connect to database.");
			languageBlock.put("dbDriverNotFound", "Database driver not found.");
			languageBlock.put("settingsFileNotFound", "Configuration file not found.");
			languageBlock.put("settingsCouldNotBeRead", "Could not read configuration file.");
			languageBlock.put("noActionDefined", "No action specified.");
			languageBlock.put("couldNotCreateList", "Could not create list.");
			languageBlock.put("productNotFound", "Product not found.");
			languageBlock.put("couldNotAddProdToList", "Product could not be added to list.");
			languageBlock.put("noProdSpecified", "No product specified.");
			languageBlock.put("couldNotRemoveProdFromList", "Product could not be removed from list.");
			languageBlock.put("couldNotSendList", "List could not be sent.");
			languageBlock.put("couldNotCreateNewList", "New list could not be created.");
		}
	}
}
