package org.kne.cloud.network.frpc;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;

import org.ini4j.Ini;
import org.ini4j.InvalidFileFormatException;

public class FrpcIni extends Ini {

	public FrpcIni() {
		super();
	}

	public FrpcIni(File input) throws IOException, InvalidFileFormatException {
		super(input);
	}

	public FrpcIni(InputStream input) throws IOException, InvalidFileFormatException {
		super(input);
	}

	public FrpcIni(Reader input) throws IOException, InvalidFileFormatException {
		super(input);
	}

	public FrpcIni(URL input) throws IOException, InvalidFileFormatException {
		super(input);
	}

	public FrpcIni(String str) throws IOException ,InvalidFileFormatException{
		super(new StringReader(str));
	}

	public void setServerAddr(String addr) {
		put("common", "server_addr", addr);
	}

	public String getServerAddr() {
		return get("common", "server_addr");
	}

	public void setToken(String addr) {
		put("common", "token", addr);
	}

	public String getToken() {
		return get("common", "token");
	}

	public void setProtocol(String protocol) {
		put("common", "protocol", protocol);
	}

	public String getProtocol() {
		return get("common", "protocol");
	}

	public void setServerPort(int port) {
		put("common", "server_port", port);
	}

	public int getServerPort() {
		return get("common", "server_port", int.class);
	}

	public void setDnsServer(String dns) {
		put("common", "dns_server", dns);
	}

	public String getDnsServer() {
		return get("common", "dns_server");
	}
	public void setConnectServerLocalIp(String connectserverlocalip) {
		put("common", "connect_server_local_ip", connectserverlocalip);
	}

	public String getConnectServerLocalIp() {
		return get("common", "connect_server_local_ip");
	}
	
	public void setUser(String user) {
		put("common", "user", user);
	}

	public String getUser() {
		return get("common", "user");
	}

	public void setTcpMux(boolean tcpmux) {
		put("common", "tcp_mux", tcpmux);
	}

	public boolean getTcpMux() {
		return get("common", "tcp_mux",boolean.class);
	}
	
	public void setTlsEnable(boolean tlsenable) {
		put("common", "tls_enable", tlsenable);
	}

	public boolean getTlsEnable() {
		return get("common", "tls_enable",boolean.class);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public void setType(String tunnel,String type) {
		put(tunnel, "type", type);
	}

	public String getType(String tunnel) {
		return get(tunnel, "type");
	}
	public void setLocalIP (String tunnel,String localip) {
		put(tunnel, "local_ip", localip);
	}

	public String getLocalIP(String tunnel) {
		return get(tunnel, "local_ip");
	}
	public void setLocalPort (String tunnel,int localport) {
		put(tunnel, "local_port", localport);
	}

	public int getLocalPort(String tunnel) {
		return get(tunnel, "local_port",int.class);
	}
	public void setRemotePort (String tunnel,int remoteport) {
		put(tunnel, "remote_port", remoteport);
	}

	public int getRemotePort(String tunnel) {
		return get(tunnel, "remote_port",int.class);
	}
	public void setUseEncryption (String tunnel,boolean useencryption) {
		put(tunnel, "use_encryption", useencryption);
	}

	public boolean getUseEncryption(String tunnel) {
		return get(tunnel, "use_encryption",boolean.class);
	}
	public void setUseCompression (String tunnel,boolean usecompression) {
		put(tunnel, "use_compression", usecompression);
	}

	public boolean getUseCompression(String tunnel) {
		return get(tunnel, "use_compression",boolean.class);
	}
	public void setSk(String tunnel,String sk) {
		put(tunnel, "sk", sk);
	}

	public String getSk(String tunnel) {
		return get(tunnel, "sk");
	}
	public void setRole(String tunnel,String sk) {
		put(tunnel, "role", sk);
	}

	public String getRole(String tunnel) {
		return get(tunnel, "role");
	}
	public void setServerName(String tunnel,String servername) {
		put(tunnel, "server_name", servername);
	}

	public String getServerName(String tunnel) {
		return get(tunnel, "server_name");
	}

	public void setBindAddr(String tunnel,String bindaddr) {
		put(tunnel, "bind_addr", bindaddr);
	}

	public String getBindAddr(String tunnel) {
		return get(tunnel, "bind_addr");
	}
	public void setBindPort (String tunnel,int bindport) {
		put(tunnel, "bind_port", bindport);
	}

	public int getBindPort(String tunnel) {
		return get(tunnel, "bind_port",int.class);
	}
	public void setKeepTunnelOpen (String tunnel,boolean keeptunnelopen) {
		put(tunnel, "keep_tunnel_open", keeptunnelopen);
	}

	public boolean getKeepTunnelOpen(String tunnel) {
		return get(tunnel, "keep_tunnel_open",boolean.class);
	}
	
	
	public String storeToString() {
		StringWriter sw = new StringWriter();
		try {
			store(sw);
			//System.out.println(sw);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sw.toString();
	}

	public void loadFromString(String ini) throws InvalidFileFormatException {
		try {
			load(new StringReader(ini));
		} catch (InvalidFileFormatException e) {
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
