package SIP;

import code.common;
import org.pmw.tinylog.Logger;
import peers.Config;
import peers.media.MediaMode;
import peers.sip.syntaxencoding.SipURI;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SuppressWarnings("unused")
public class CustomConfig implements Config {
    private InetAddress localIpAddress;
    private InetAddress publicIpAddress;
    private String registrar;
    private String username;
    private String password;
    private int sip_port;
    private int rtp_port;
    private transient MediaMode mm;			// tidak disave dan load
    private SipURI outbondproxy;
    private transient boolean mediadebug;	// tidak disave dan load
    private transient String mediafile;		// tidak disave dan load



    /**
     * Create CustomConfig
     * @param registrar Server IP
     * @param username username
     * @param password password
     */
    public CustomConfig(String registrar, String username, String password) {
        this.registrar = registrar;
        this.password = password;
        this.username = username;
        setLocalInetAddress(null);

    }

    /**
     * Create CustomConfig
     * @param registrar Server IP
     * @param username username
     * @param password password
     * @param localip local ethernet to use to register. if not valid, will use default LocalHost
     */
    public CustomConfig(String registrar, String username, String password, String localip) {
        this.registrar = registrar;
        this.password = password;
        this.username = username;
        SetLocalInetAddress(localip);

    }

    public CustomConfig() {
        this.registrar = "192.168.5.1";
        this.username = "1002";
        this.password = "1234";
        setLocalInetAddress(null);
    }

    /**
     * Get Local IP Address
     */
    @Override
    public InetAddress getLocalInetAddress() {
        return localIpAddress;

    }

    /**
     * Get public IP Address used
     */
    @Override
    public InetAddress getPublicInetAddress() {
        return publicIpAddress;
    }

    /**
     * Get Username to use for registering with SIP Server
     */
    @Override
    public String getUserPart() {
        return username;
    }

    /**
     * Get SIP Server (registrar)
     */
    @Override
    public String getDomain() {
        return registrar;
    }

    /**
     * Get Password
     */
    @Override
    public String getPassword() {
        return password;
    }

    /**
     * Get Outbond Proxy used
     */
    @Override
    public SipURI getOutboundProxy() {
        return outbondproxy;
    }

    /**
     * Get SIP Port . default is 5060
     */
    @Override
    public int getSipPort() {
        return sip_port;
    }

    /**
     * Get Current MediaMode
     */
    @Override
    public MediaMode getMediaMode() {
        return mm;
    }

    /**
     * Not used
     */
    @Override
    public boolean isMediaDebug() {
        return mediadebug;
    }

    /**
     * Get MediaFile used
     */
    @Override
    public String getMediaFile() {
        return mediafile;
    }

    /**
     * Get RTP Port used
     */
    @Override
    public int getRtpPort() {
        return rtp_port;
    }

    /**
     * Set Local Network Interface to use with SIP.jSIPClient
     * useful when having multiple network interfaces in the machine
     */
    @Override
    public void setLocalInetAddress(InetAddress inetAddress) {
        try {
            this.localIpAddress = inetAddress == null ? InetAddress.getLocalHost() : inetAddress;
        } catch (UnknownHostException e) {
            Logger.error("setLocalInetAddress exception = " + e.getMessage());
        }

    }

    /**
     * Set Local Network Interface to use with SIP.jSIPClient
     * useful when having multiple network interfaces in the machine
     * @param ipaddress IP Address of the network interface to use in the machine
     */
    public void SetLocalInetAddress(String ipaddress) {
        try {
            InetAddress xx = InetAddress.getByName(ipaddress);
            setLocalInetAddress(xx);
        } catch (UnknownHostException e) {
            Logger.error("SetLocalInetAddress exception = "+e.getMessage());
        }
    }

    /**
     * Set Public InetAddress to use with JSIPClient
     * personal note : dont know if really necessary
     */
    @Override
    public void setPublicInetAddress(InetAddress inetAddress) {
        publicIpAddress = inetAddress;
    }

    /**
     * Set Public IP Address to use with SIP.jSIPClient
     * Personal Note : dont know if really necessary
     * @param ipaddress IP Address in string
     */
    public void SetPublicInetAddress(String ipaddress) {
        try {
            InetAddress xx = InetAddress.getByName(ipaddress);
            setPublicInetAddress(xx);
        } catch (UnknownHostException e) {
            Logger.error("SetPublicInetAddress exception = "+e.getMessage());
        }
    }

    /**
     * Set Username to use for registering to SIP Server
     */
    @Override
    public void setUserPart(String username) {
        this.username = username;

    }

    /**
     * Set SIP Server (registrar) IP Address / Domain
     */
    @Override
    public void setDomain(String domain) {
        registrar = domain;

    }

    /**
     * Set Password to use for registering to SIP Server
     */
    @Override
    public void setPassword(String password) {
        this.password = password;

    }

    /**
     * Not used
     */
    @Override
    public void setOutboundProxy(SipURI outboundProxy) {
        outbondproxy = outboundProxy;

    }

    /**
     * Not used
     */
    @Override
    public void setSipPort(int sipPort) {
        this.sip_port = common.ValidPortNumber(sipPort) ? sipPort : 5060;

    }

    /**
     * Not used
     */
    @Override
    public void setMediaMode(MediaMode mediaMode) {
        mm = mediaMode;

    }

    /**
     * Not used
     */
    @Override
    public void setMediaDebug(boolean mediaDebug) {
        mediadebug = mediaDebug;

    }

    /**
     * Not used
     */
    @Override
    public void setMediaFile(String mediaFile) {
        mediafile = mediaFile;

    }

    /**
     * Not used
     */
    @Override
    public void setRtpPort(int rtpPort) {
        rtp_port = common.ValidPortNumber(rtpPort) ? rtpPort : 49152 ;

    }

    /**
     * Not used
     */
    @Override
    public void save() {}

}
