package no.cantara.csdb.settings;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.codehaus.jackson.map.ObjectMapper;

import no.cantara.cs.dto.Client;
import no.cantara.cs.dto.ClientAlias;
import no.cantara.csdb.cs_client.commands.CommandGetAllClientAliases;
import no.cantara.csdb.cs_client.commands.CommandGetAllClients;
import no.cantara.csdb.cs_client.commands.CommandGetIgnoredClientIds;
import no.cantara.csdb.cs_client.commands.CommandPutAignoredClient;
import no.cantara.csdb.cs_client.commands.CommandPutClientAlias;
import no.cantara.csdb.errorhandling.AppException;
import no.cantara.csdb.errorhandling.AppExceptionCode;
import no.cantara.csdb.util.CommandResponseHandler;


public class CSMappingSettingsDao implements SettingsDao {

	private Map<String, String> aliases = new HashMap<>();
	private Set<String> ignoredClients = new HashSet<String>();
	    
	@Override
	public Map<String, String> getAliases() {
		if(aliases!=null) {
			Map<String, String> m = new HashMap<String, String>(); 
			m.putAll(aliases);
			return m;
		}
		aliases = new HashMap<>();
		String clientsJson = new CommandGetAllClientAliases().execute();
		if (clientsJson != null) {
			ObjectMapper mapper = new ObjectMapper();
			try {
				List<ClientAlias> cal = Arrays.asList(mapper.readValue(clientsJson, ClientAlias[].class));

				for(ClientAlias ca : cal) {
					aliases.put(ca.clientId, ca.clientName);
				}
				
				

			} catch (IOException e) {
				e.printStackTrace();
			}


		}
		return aliases;
	}

	@Override
	public boolean addAlias(String clientId, String alias) throws AppException {
		//update in CS
		CommandPutClientAlias cmd = new CommandPutClientAlias(clientId, alias);
		cmd.execute();
		if(cmd.getStatusCode()==200) {
			aliases.put(clientId, alias);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Set<String> getIgnoredClients() {
		if(ignoredClients!=null) {
			return new HashSet<String>(ignoredClients);
		}
		String clientIdsJson = new CommandGetIgnoredClientIds().execute();
		if (clientIdsJson != null) {
			ObjectMapper mapper = new ObjectMapper();
			try {
				List<String> ids = Arrays.asList(mapper.readValue(clientIdsJson, String[].class));
				ignoredClients  = new HashSet<String>(ids);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


		}
		return ignoredClients;
	}

	@Override
	public boolean setIgnoredFlag(String clientId, boolean ignore) {
		
		CommandPutAignoredClient cmd = new CommandPutAignoredClient(clientId, ignore);
		cmd.execute();
		if(cmd.getStatusCode()==200) {
			if(ignore) {
				if(!ignoredClients.contains(clientId)) {
					ignoredClients.add(clientId);
				}
			} else {
				ignoredClients.remove(clientId);
			}
			return true;
		} else {
			return false;
		}	
		
	}

	

}

@@ -959,16 +959,18 @@
 o %7B%0D%0A%0D%0A%09
+//
 private 
@@ -1018,16 +1018,18 @@
 %3C%3E();%0D%0A%09
+//
 private 
@@ -1140,24 +1140,26 @@
 liases() %7B%0D%0A
+//
 %09%09if(aliases
@@ -1169,16 +1169,18 @@
 ull) %7B%0D%0A
+//
 %09%09%09Map%3CS
@@ -1231,16 +1231,18 @@
 g%3E(); %0D%0A
+//
 %09%09%09m.put
@@ -1252,24 +1252,26 @@
 (aliases);%0D%0A
+//
 %09%09%09return m;
@@ -1272,23 +1272,45 @@
 urn m;%0D%0A
+//
 %09%09%7D%0D%0A%09%09
+Map%3CString, String%3E 
 aliases 
@@ -2017,16 +2017,18 @@
 ) %7B%0D%0A%09%09%09
+//
 aliases.
@@ -2163,16 +2163,18 @@
 ts() %7B%0D%0A
+//
 %09%09if(ign
@@ -2191,24 +2191,26 @@
 s!=null) %7B%0D%0A
+//
 %09%09%09return ne
@@ -2245,21 +2245,78 @@
 ents);%0D%0A
+//
 %09%09%7D%0D%0A
+%09%09Set%3CString%3E ignoredClients = new HashSet%3CString%3E();%0D%0A
 %09%09String
@@ -2966,171 +2966,8 @@
  %7B%0D%0A
-%09%09%09if(ignore) %7B%0D%0A%09%09%09%09if(!ignoredClients.contains(clientId)) %7B%0D%0A%09%09%09%09%09ignoredClients.add(clientId);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09ignoredClients.remove(clientId);%0D%0A%09%09%09%7D%0D%0A
 %09%09%09r

remove the caches for aliases/ignoredclients cos they do not work with HA