/*
 * Copyright 2010 PC-NG Inc. All rights reserved.
 * PC-NG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nexiwave.search.client.sample;

import java.io.File;
import java.net.URLEncoder;

import javax.activation.DataHandler;

import com.nexiwave.search.client.SearchService2Stub;
import com.nexiwave.search.client.SearchService2Stub.AddResponse;
import com.nexiwave.search.client.SearchService2Stub.AttachHumanTranscription;
import com.nexiwave.search.client.SearchService2Stub.AttachHumanTranscriptionFile;
import com.nexiwave.search.client.SearchService2Stub.AttachHumanTranscriptionFileResponse;
import com.nexiwave.search.client.SearchService2Stub.AttachHumanTranscriptionResponse;
import com.nexiwave.search.client.SearchService2Stub.GetTimeStampedHumanTranscriptionResponse;
import com.nexiwave.search.client.SearchService2Stub.TimeStampedSentence;
import com.nexiwave.search.client.sample.ClientUtil.LoopWork;

public class AttachHumanTranscriptionTest extends AbstractSampleClient{
	
	private long recordingId=0;

	public AttachHumanTranscriptionTest(String serverLoc, long groupId, String userId, String passwd) throws Exception {
		super(serverLoc, groupId, userId, passwd);
	}
	
	private long createRecording()throws Exception{
		if(recordingId<=0){
			String url=getRESTfulBaseURL("add")+"&data.mediaURL=http://test.nexiwave.com/static/audio/test.wav";
			String result=ClientUtil.get(url);
			
			AddResponse resp=SearchService2Stub.AddResponse.Factory.parse(ClientUtil.toXMLStreamReader(result));
			recordingId=resp.get_return();
			if(recordingId<=0){
				throw new RuntimeException("Expecting recordingId. Got"+result);
			}
		}
		
		return recordingId;
	}

	
	public void testAttachHumanTranscription()throws java.lang.Exception{
		long recordingId=createRecording();
		
		String humanTranscript="a number of method, including lean, that we can acutally";
		
		String url=getRESTfulBaseURL("attachHumanTranscription")+"&recordingId="+recordingId+"&humanTranscribed="+URLEncoder.encode(humanTranscript, "UTF-8");
		String result=ClientUtil.get(url);
		AttachHumanTranscriptionResponse resp1=SearchService2Stub.AttachHumanTranscriptionResponse.Factory.parse(ClientUtil.toXMLStreamReader(result));
		if(resp1.get_return()){
			examTimeStampedResult(recordingId);
		}
	}
	
	public void testAttachHumanTranscription_Post()throws java.lang.Exception{
		long recordingId=createRecording();
		
		String humanTranscript="a number of method, including lean, that we can acutally";
		
		AttachHumanTranscription req=new AttachHumanTranscription();
		
		SearchService2Stub.AuthData authData=new SearchService2Stub.AuthData();		
		authData.setEmail(data.getUserId());
		authData.setPasswd(data.getPasswd());
		authData.setGroupId(data.getChildAccountGroupId());

		req.setAuthData(authData);

		req.setRecordingId(recordingId);
		req.setHumanTranscribed(humanTranscript);
		
		AttachHumanTranscriptionResponse resp1=getStub().attachHumanTranscription(req);
		if(resp1.get_return()){
//			examTimeStampedResult(recordingId);
		}
	}
	
	public void testAttachHumanTranscriptionFile(File transcriptionFile)throws  java.lang.Exception{
		long recordingId=createRecording();
		AttachHumanTranscriptionFile req=new AttachHumanTranscriptionFile();
		
		SearchService2Stub.AuthData authData=new SearchService2Stub.AuthData();		
		authData.setEmail(data.getUserId());
		authData.setPasswd(data.getPasswd());
		authData.setGroupId(data.getChildAccountGroupId());

		req.setAuthData(authData);

		req.setRecordingId(recordingId);
		req.setHumanTranscribedFile(new DataHandler(transcriptionFile.toURI().toURL()));
		
		AttachHumanTranscriptionFileResponse resp1=getStub().attachHumanTranscriptionFile(req);
		if(resp1.get_return()){
			examTimeStampedResult(recordingId);
		}
	}

	private void examTimeStampedResult(final long recordingId)throws Exception{
		
		ClientUtil.doLoop(new LoopWork() {
			
			@Override
			public boolean doLoopWork(int lpCount) throws Exception {

				String url=getRESTfulBaseURL("getTimeStampedHumanTranscription")+"&recordingId="+recordingId;
				String result=ClientUtil.get(url);
				
				GetTimeStampedHumanTranscriptionResponse resp1=GetTimeStampedHumanTranscriptionResponse.Factory.parse(ClientUtil.toXMLStreamReader(result));
				TimeStampedSentence[] tsss=resp1.get_return();
				
				if(tsss!=null && tsss.length>0){
					for(TimeStampedSentence tss:tsss){
						StringBuilder sb=new StringBuilder();
						sb.append("Sentence ").append(tss.getIndexInDocument()).append(": ");
						sb.append(tss.getStartTimeInMS()+" - "+tss.getEndTimeInMS());
						tss.getOriginalDocumentWord();
						
					}
					return true;
				}
				
				return false;
			}
		}, 20*60*1000);

	}
}
