package test;


import java.io.File;

import com.google.gson.Gson;
import com.sun.jna.Pointer;

import bass.BassAACLibrary;
import bass.BassLibrary;
import bass.BassLibrary.BASS_DEVICEINFO;
import bass.BassLibrary.BASS_FILEPROCS;
import bass.BassLibrary.FILECLOSEPROC;
import bass.BassLibrary.FILELENPROC;
import bass.BassLibrary.FILEREADPROC;
import bass.BassLibrary.FILESEEKPROC;
import code.JVAPIX;
import code.VapixAudio;
import code.common;
import code.VapixAudio.AudioControlServiceApi.VolumeRange;
import code.VapixAudio.AudioRelayServiceApi.AudioPeer;
import code.VapixAudio.AudioRelayServiceApi.DiscoveredAudioPeer;
import code.VapixAudio.AudioRelayServiceApi.PeerStatus;
import code.VapixAudio.AudioRelayServiceApi.SoundConfiguration;
import code.VapixAudio.CallServiceApi.SIPAccounts.SIPAccount;
import code.VapixAudio.CallServiceApi.SIPAccounts.SIPAccountStatus;
import code.VapixAudio.MediaApi.MediaClip;
import code.VapixDevice.DownloadResult;
import lombok.val;

@SuppressWarnings("all")
public class testvapix {
	static Gson gs = new Gson();
	static File currentpath = new File("");
	
	public static void testvapix() {
		String[] targets = new String[] {"192.168.5.153","192.168.5.182","192.168.5.192"};
		System.out.println("Axis Audio test");
		JVAPIX ja = new JVAPIX();
		
		String ip = targets[2];
		if (ja.Connect(ip, 80, "root", "pass","192.168.5.154")) {
			Object xx = ja.GetConnectedDevice(ip);
			if (xx instanceof VapixAudio) {
				VapixAudio va = (VapixAudio) xx;
				
				//vapix_test_callservice(va);
				//vapix_test_audiocontrolservice(va);
				//vapix_test_audiorelayservice(va);
				//vapix_test_mediaclip(va,0);
				vapix_test_audioreceive(va);
				
			}
		}
	}
	
	private static void vapix_test_mediaclip(VapixAudio va, final int outputid) {
		if (va.MediaApi.IsSupported(outputid)) {
			System.out.println("MediaClip API supported on output "+outputid);
			
			if (va.MediaApi.GetAvailableMediaClips()) {
				
				if (va.MediaApi.AvailableMediaClips!=null && va.MediaApi.AvailableMediaClips.length>0) {
					for(MediaClip mcx : va.MediaApi.AvailableMediaClips) {
						System.out.println(gs.toJson(mcx));
					}
				}
			} else System.out.println("GetAvailableMediaClips failed, or no clips installed");
			
			String[] acf = va.MediaApi.AudioClipFormat();
			if (acf!=null && acf.length>0) {
				System.out.println("Audio Clip Format : ");
				for(String format : acf) {
					System.out.println(format);
				}
			}
			
			// coba play yang simple
//			if (va.MediaApi.PlayClip(0, 47)) {
//				System.out.println("Play Clip success");
//				
//			}
			
			// coba play dan di stop setelah 10 detik
//			System.out.println(va.MediaApi.PlayClip2(1, 46, 1, 100));
			//common.Sleep(10000);
			//if (va.MediaApi.StopClip()) System.out.println("Stop success");
			
			//metode download jadinya byte array
//			byte[] data = va.MediaApi.DownloadClip(45);
//			if (data!=null && data.length>0) {
//				System.out.println("Download success, size = "+data.length);
//			} else System.out.println("Download failed");
			
			//metode download jadinya langsung file
//			DownloadResult dr = va.MediaApi.DownloadClip(45, currentpath.getAbsolutePath());
//			if (dr!=null) {
//				System.out.println("Download success, result = "+dr.toString());
//			} else System.out.println("Download failed");
			
			//va.MediaApi.UploadClip("Soundtrack","all_of_me.mp3");
			//va.MediaApi.PlayClip(0, 0);
			//va.MediaApi.RemoveClip(0);
			//va.MediaApi.RenameClip(0, "AllOfMe");
		} else System.out.println("MediaClip API not supported on output "+outputid);
	}
	
	private static void vapix_test_audiorelayservice(VapixAudio va) {
		if (va.AudioRelayServiceApi.IsSupported()) {
			System.out.println("AudioRelayService Supported");
			//
			AudioPeer[] ap = va.AudioRelayServiceApi.GetAudioPeers();
			if (ap!=null && ap.length>0) {
				for(AudioPeer apx : ap) {
					System.out.println("Configuration = "+gs.toJson(apx.Configuration));
					System.out.println("Metadata = "+gs.toJson(apx.MetaData));
					System.out.println("ConnectionStatus = "+apx.ConnectionStatus);
					System.out.println("OutputGainDefinitions = "+gs.toJson(apx.OutputGainDefinitions));
				}
			} else System.out.println("AudioPeer not available");
			
			//
			PeerStatus[] ps = va.AudioRelayServiceApi.GetAudioPeerStatus();
			if (ps!=null && ps.length>0) {
				for(PeerStatus psx : ps) {
					System.out.println(gs.toJson(psx));
				}
			} else System.out.println("PeerStatus not available");
			
			
			//
			DiscoveredAudioPeer[] dap = va.AudioRelayServiceApi.GetDiscoveredAudioPeers();
			if (dap!=null && dap.length>0) {
				for(DiscoveredAudioPeer dapx : dap) {
					System.out.println(gs.toJson(dapx));
				}
			} else System.out.println("DiscoveredAudioPeer not available");
			
			
			//
			SoundConfiguration sc = va.AudioRelayServiceApi.GetSoundConfiguration();
			System.out.println(sc!=null ? sc.toString() : "SoundConfiguration not available");
			
			if (va.AudioRelayServiceApi.SetSoundConfiguration_MasterVolume(0,false)) System.out.println("SetSoundConfiguration Success"); else System.out.println("SetSoundConfiguration failed");
		
		} else System.out.println("AudioRelayService not supported");
	}
	
	
	
	private static void vapix_test_callservice(VapixAudio va) {
		if (va.CallServiceApi.IsSupported()) {
			System.out.println("CallService Supported");
			va.CallServiceApi.SIPConfiguration.Get();
			va.CallServiceApi.SIPAccounts.Get();
			SIPAccount[] accounts = va.CallServiceApi.SIPAccounts.Registered_SIP_Accounts();
			for(SIPAccount sa : accounts) {
				
				System.out.println(sa.toString());
				SIPAccountStatus sas = va.CallServiceApi.SIPAccounts.GetSIPAccountStatus(sa.Id);
				System.out.println(sas.toString());
			}
			
			//va.CallServiceApi.SIPAccounts.GetCallStatuses();
			//va.CallServiceApi.SIPAccounts.GetCallStatus("In-0-1681892244.939170-1d7474353e0c79d205a0fd114f663425@192.168");
			//if (va.CallServiceApi.SIPAccounts.TerminateCall("In-0-1681892244.939170-1d7474353e0c79d205a0fd114f663425@192.168")) System.out.println("Terminate cal success");
		
//			String callid = va.CallServiceApi.SIPAccounts.Call("3000");
//			if (common.ValidString(callid)) {
//				System.out.println("Call success ");
//				va.CallServiceApi.SIPAccounts.GetCallStatuses();
//				common.Sleep(20*1000);
//				if (va.CallServiceApi.SIPAccounts.TerminateCall(callid)) {
//					System.out.println("Terminate call success");
//				} else System.out.println("Terminate Call failed");
//			} else System.out.println("Call failed");
//			
			
		} else System.out.println("CallService is not supported");
	}
	
	private static void vapix_test_audiocontrolservice(VapixAudio va) {
		if (va.AudioControlServiceApi.IsSupported()) {
			System.out.println("AudioControlService supported");
			va.AudioControlServiceApi.GetControlCapabilities();
			va.AudioControlServiceApi.GetVolume();
			if (va.AudioControlServiceApi.SetVolume()) System.out.println("SetVolume success"); else System.out.println("SetVolume failed");
			
			String[] chid = va.AudioControlServiceApi.ControlCapabilities.ChannelIds;
			if (common.ValidArrayOfString(chid)) {
				for(String ch : chid) System.out.println(ch);
			}
			String[] inid = va.AudioControlServiceApi.ControlCapabilities.InputIds;
			if (common.ValidArrayOfString(inid)) {
				for(String id : inid) System.out.println(id);
			}
			
			VolumeRange[] volrange = va.AudioControlServiceApi.ControlCapabilities.VolumeRanges;
			if (volrange != null && volrange.length>0) {
				for(VolumeRange vv : volrange) {
					System.out.println(vv.toString());
				}
			} else System.out.println("VolumeRange length =0 or null");
			
		} else System.out.println("AudioControlService not supported");
		
	}
	
	private static void vapix_test_audioreceive(VapixAudio va) {
		if (va.AudioApi.IsSupported()) {
			System.out.println("AudioAPI Supported");
			
			BASS_DEVICEINFO dev = new BASS_DEVICEINFO();
			if (BassLibrary.BASS.BASS_GetDeviceInfo(1, dev)) {
				System.out.println(dev.toString());
			} else System.out.println("BASS_GetDeviceInfo failed, error="+BassLibrary.BASS.GetBassError("BASS_GetDeviceInfo"));
			BassLibrary.BASS.BASS_Init(1, 48000, 0);
			
			
			BASS_FILEPROCS bfp = new BASS_FILEPROCS();
			bfp.FILECLOSEPROC = new FILECLOSEPROC() {

				@Override
				public void FILECLOSEPROC(Pointer user) {
					System.out.println("FILECLOSEPROC called");
					
				}
				
			};
			bfp.FILELENPROC = new FILELENPROC() {

				@Override
				public long FILELENPROC(Pointer user) {
					System.out.println("FILELENPROC called");
					return 0;
				}
				
			};
			bfp.FILEREADPROC = new FILEREADPROC() {

				@Override
				public int FILEREADPROC(Pointer buffer, int length, Pointer user) {
					System.out.println("FILREADPROC called, length="+length);
					byte[] buf = va.AudioApi.GetReceivedBuffers_bySize(length);
					if (buf!=null && buf.length>0) {
						System.out.println("FILEREADPROC called, buf.length="+buf.length);
						buffer.write(0, buf, 0, buf.length);
						return buf.length;
					} else System.out.println("FILEREADPROC called but buf is invalid");
					return -1;
				}
				
			};
			bfp.FILESEEKPROC = new FILESEEKPROC() {

				@Override
				public boolean FILESEEKPROC(long offset, Pointer user) {
					System.out.println("FILESEEKPROC called");
					return false;
				}
				
			};
			
			if (va.AudioApi.Start_Receive()) {
				System.out.println("Wait until buffer enough");
				while(va.AudioApi.AvailableReceivedBytes()<8000) {
					common.Sleep(1);
				}
				System.out.println("Buffer size = "+va.AudioApi.AvailableReceivedBytes());
				int handle = BassAACLibrary.BASS_AAC.BASS_AAC_StreamCreateFileUser(BassLibrary.Constant.STREAMFILE_BUFFER, 0, bfp, null);
				if (handle!=0) {
					System.out.println("BASS AAC Stream created");
					if (BassLibrary.BASS.BASS_ChannelStart(handle)) {
						System.out.println("ChannelStart success");
					} else System.out.println("ChannelStart failed, error="+BassLibrary.BASS.GetBassError("BASS_ChannelStart"));
				} else System.out.println("BASS AAC Stream not created, code = "+BassLibrary.BASS.BASS_ErrorGetCode());
				
				
				common.Sleep(10);
				va.AudioApi.Stop_Receive();
				
				if (handle!=0) {
					if (BassLibrary.BASS.BASS_ChannelFree(handle)) {
						System.out.println("ChannelFree success");
					} else System.out.println("ChannelFree failed, error="+BassLibrary.BASS.GetBassError("BASS_ChannelFree"));
					
				}
				
			} else System.out.println("Start_Receive failed");
			
			
		} else System.out.println("AudioAPI not supported");
	}
	
}
