import org.pmw.tinylog.Logger;

import java.io.File;
import java.text.MessageFormat;

public class Main {
    private static String serverAddress="100.64.0.3";
    private static String Username="user1";
    private static String Password="12345678";
    private static jSIPClient client;
    private static String currentDir;
    public static void main(String[] args) {
        currentDir = System.getProperty("user.dir");
        GetArguments(args);
        Logger.info("Server Address: "+serverAddress);
        Logger.info("Username: "+Username);
        Logger.info("Password: "+Password);

        client = new jSIPClient();
        client.SetJavaSipEvent(new javaSipEvents() {
            @Override
            public void Registering(jSIPClient.SIP_Request req) {
                Logger.info("Registering to SIP Server, Request: {}",req);
            }

            @Override
            public void RegisterSuccesful(jSIPClient.SIP_Response resp) {
                Logger.info("Registered to SIP Server, Response: {}",resp);
            }

            @Override
            public void RegisterFailed(jSIPClient.SIP_Response resp) {
                Logger.info("Failed to register to SIP Server, Response: {}",resp);
            }

            @Override
            public void IncomingCall(jSIPClient.SIP_Request req, jSIPClient.SIP_Response resp) {
                Logger.info("Incoming Call, Request: {}, Response: {}",req,resp);
                client.AcceptIncomingCall(req);
            }

            @Override
            public void RemoteHangUp(jSIPClient.SIP_Request req) {
                Logger.info("Remote Hangup, Request: {}",req);
                client.HangUp();
            }

            @Override
            public void Ringing(jSIPClient.SIP_Response resp) {
                Logger.info("Ringing, Response: {}",resp);
            }

            @Override
            public void CalleePickup(jSIPClient.SIP_Response resp) {
                Logger.info("Callee Pickup, Response: {}",resp);
            }
        });
        ReconnectSIP();
    }

    @SuppressWarnings("unused")
    private static void CallTest(String extension){
        String callnumber = MessageFormat.format("sip:{0}@{1}", extension, serverAddress);
        if (client.Call(callnumber)){
            Logger.info("Call to {} is successful",callnumber);
            try{
                Thread.sleep(30*1000);
            } catch (InterruptedException ignored) {
            }
            client.HangUp();
        }
        else{
            Logger.error("Call to {} is failed",callnumber);
        }
    }

    @SuppressWarnings("unused")
    private static void StreamTest(String extension, String filename){
        File ff = new File(currentDir, filename);
        if (ff.isFile()){
            String callnumber = MessageFormat.format("sip:{0}@{1}", extension, serverAddress);
            if (client.StreamFile(callnumber, ff.getAbsolutePath())){
                Logger.info("Stream to {} is successful",callnumber);
                try{
                    Thread.sleep(30*1000);
                } catch (InterruptedException ignored) {
                }
                client.HangUp();
            }
            else{
                Logger.error("Stream to {} is failed",callnumber);
            }
        } else Logger.error("File not found: {}",ff.getAbsolutePath());
    }

    private static void ReconnectSIP(){
        if (client.Connect(serverAddress, Username, Password,"0.0.0.0")){
            Logger.info("Connected to SIP Server");
        }
        else{
            Logger.error("Failed to connect to SIP Server");
            try {
                Thread.sleep(20*1000);
            } catch (InterruptedException ignored) {
            }
            ReconnectSIP();
        }
    }

    private static void GetArguments(String[] args) {
        if (args != null){
            for(String a : args){
                if (a.startsWith("--serverip=")){
                    serverAddress=a.substring(11);
                }
                else if (a.startsWith("--username=")){
                    Username=a.substring(11);
                }
                else if (a.startsWith("--password=")){
                    Password=a.substring(11);
                }
            }
        }
    }
}