package config;

import org.json.JSONException;
import org.json.JSONObject;

public class Configuration {
	
	
	
	private SConf sConf; //Configuration du serveur
	private Conf conf; //Configuration réseau

	
	public Configuration() {
		this.sConf = new SConf();
		this.conf = new Conf();
	}
	
	public Configuration(JSONObject obj) throws JSONException {
		this();
		if(obj.has("serveur")) {
			this.sConf = new SConf(obj.getJSONObject("serveur"));
			if(obj.has("config")) {
				this.conf = new Conf(obj.getJSONObject("config"));
			}
		}
	}
	
	public static class SConf {
		private JSONObject obj;
		
		private boolean ok;
		private String url;
		private int portServer;
		
		private final String OK = "actif";
		private final String URL = "url";
		private final String PORT = "port";
		
		
		public SConf() {
			this.ok = false;
			this.url= "";
		}
		
		public SConf (JSONObject obj) {
			this();
			this.obj = obj;
			checkServerOk();
			if(this.ok) {
				checkServerUrl();
				if(this.url.isBlank()) {
					this.ok = false;
				}else {
					checkServerPort();
				}
			}
		}
		
		private void checkServerOk() {
			try {
				this.ok = obj.getInt(OK) == 1;
			}catch(JSONException e) {
				this.ok = false;
			}
		}
		
		private void checkServerUrl() {
			try {
				this.url = obj.getString(URL);
			}catch(JSONException e) {
				this.url = "";
			}
		}
		
		private void checkServerPort() {
			try {
				this.portServer = obj.getInt(PORT);
			}catch(JSONException e) {
				this.portServer = 30000; //port 30000 set par defaut
			}
		}
		
		public boolean isOK() {
			return this.ok;
		}
		
		public String getUrl() {
			return this.url;
		}
		
		public int getPortServer() {
			return this.portServer;
		}
	}

	public static class Conf{
		
		private JSONObject obj;
		
		private String interfaceRzo;
		private int srcPort;
		private int destPort;
		
		private final String INTERFACE = "interface";
		private final String SRC_PORT = "portSrc";
		private final String DEST_PORT = "portDest";

		public Conf() {
			this.interfaceRzo = "";
			this.srcPort = 25000;
			this.destPort = 25001;
		}

		public Conf(JSONObject obj) {
			this();
			this.obj = obj;
			checkInterfaceRzo();
			checkSrcPort();
			checkDestPort();
			
		}
		
		private void checkInterfaceRzo() {
			try {
				this.interfaceRzo = obj.getString(INTERFACE);
			}catch(JSONException e) {
				this.interfaceRzo = "";
			}
		}
		
		private void checkSrcPort() {
			try {
				this.srcPort = obj.getInt(SRC_PORT);
			}catch(JSONException e) {
				this.srcPort = 25000; //port 25000 set par defaut pour port src
			}
		}
		
		private void checkDestPort() {
			try {
				this.destPort = obj.getInt(DEST_PORT);
			}catch(JSONException e) {
				this.destPort = 25001; //port 25001 set par defaut pour port dest
			}
		}
	
		public String getInterface() {
			return this.interfaceRzo;
		}
		
		public int getPortSrc() {
			return this.srcPort;
		}
		
		public int getDestPort() {
			return this.destPort;
		}
		
	}
	
	public Conf getConf() {
		return this.conf;
	}
	
	
	public SConf getsConf() {
		return this.sConf;
	}
}
