package test;

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

import code.jSIPClient;
import code.jSIPClient.SIP_Request;
import code.jSIPClient.SIP_Response;

public class testsipclient {
	private static jSIPClient client;
	private static String server_ip;
	
	public static void testsip(String serverip, String username, String password) {
		server_ip = serverip;
		client = new jSIPClient();
		client.Connect(serverip, username, password, "192.168.5.154");
		SipEvents se = new SipEvents(client);
		client.SetJavaSipEvent(se);
		
		//calltest("2001");
		//streamtest("2001","lagu8khz16bitmono.wav");
		//streamtest("2001","all_of_me.mp3");
		//streamtest("2001","store_closing_example.mp3");
	}
	
	@SuppressWarnings("unused")
	private static void streamtest(String numbertocall, String filename) {
		// kasih waktu supaya registered ke server
		try {
			Thread.sleep(5*1000);
		} catch (InterruptedException e) {
			
		}
		
		File currentfolder = new File("");

		File ff = new File(currentfolder.getAbsolutePath(),filename);
		if (ff.exists()) {
			System.out.println("File found : "+ff.getAbsolutePath());
			
			String extensiontocall = MessageFormat.format("sip:{0}@{1}", numbertocall, server_ip);
			System.out.println("Calling extension "+extensiontocall);
			
			client.StreamFile(extensiontocall, ff.getAbsolutePath());
			
			
			// call duration = 10 detik
			try {
				Thread.sleep(53*1000);
			} catch (InterruptedException e) {
				
			}
			client.HangUp();
			
		} else System.out.println("File not found : "+ff.getAbsolutePath());
		
	}
	
	@SuppressWarnings("unused")
	private static void calltest(String numbertocall) {
		// kasih waktu supaya registered ke server
		try {
			Thread.sleep(5*1000);
		} catch (InterruptedException e) {
			
		}
		
		String extensiontocall = MessageFormat.format("sip:{0}@{1}", numbertocall, server_ip);
		System.out.println("Calling extension "+extensiontocall);
		client.Call(extensiontocall);
		
		// call duration = 10 detik
		try {
			Thread.sleep(10*1000);
		} catch (InterruptedException e) {
			
		}
		client.HangUp();
	}
	
	
}

class SipEvents implements JavaSipEvents{
	private final jSIPClient client;
	
	public SipEvents(jSIPClient _client) {
		client = _client;
	}
	
	@Override
	public void Registering(SIP_Request req) {
		System.out.println("Registering, Request = ");
		System.out.println(req);
		System.out.println("");
	}

	@Override
	public void RegisterSuccesful(SIP_Response resp) {
		System.out.println("Register Succesful, Response = ");
		System.out.println(resp);
		System.out.println("");
	}

	@Override
	public void RegisterFailed(SIP_Response resp) {
		System.out.println("Register Failed, Response = ");
		System.out.println(resp);
		System.out.println("");
	}

	@Override
	public void IncomingCall(SIP_Request req, SIP_Response resp) {
		System.out.println("Incoming Call");
		System.out.println("Request = ");
		System.out.println(req);
		System.out.println("Response = ");
		System.out.println(resp);
		System.out.println("");
		
		client.AcceptIncomingCall(req);
		
	}

	@Override
	public void RemoteHangUp(SIP_Request req) {
		System.out.println("Remote HangUp, Request = ");
		System.out.println(req);
		System.out.println("");
		
		client.HangUp();
	}

	@Override
	public void Ringing(SIP_Response resp) {
		System.out.println("Ringing, Response = ");
		System.out.println(resp);
		System.out.println("");
		
	}

	@Override
	public void CalleePickup(SIP_Response resp) {
		System.out.println("Callee Pickup, Response = ");
		System.out.println(resp);
		System.out.println("");
	}
	
}
