/*
 * 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 org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.client.Options;
import org.apache.axis2.transport.http.HTTPConstants;

import com.nexiwave.search.client.SearchService2Stub;
import com.nexiwave.speechindexing.client.SpeechIndexingStub;

public class AbstractSampleClient {

	protected TestData data;
	protected String server_loc="https://api.nexiwavecom/services";
	protected SearchService2Stub _stub;
	protected SpeechIndexingStub _stub2;
	private String serviceName="SpeechIndexing";

	/**
	 * Constructor
	 * @param server_loc
	 * @param userId
	 * @param passwd
	 * @throws Exception
	 */
	public AbstractSampleClient(String server_loc, long groupId, String userId, String passwd)throws Exception {
		super();
		TestData data=new TestData();
		data.setChildAccountGroupId(groupId);
		data.setUserId(userId);
		data.setPasswd(passwd);
		data.setSmallAudioFileURL("http://nexiwave.com/PC-NG-AA/audio/test.wav");
		data.setAudioFileURL("http://cisco-podcast.streamguys.net/cdc/uc/PresenceFed-Sneak.mp3");
		data.setLocalAudioFile(new File("./test.data/audio/test.wav"));

		init(server_loc, data);
	}
	
	/**
	 * Constructor
	 * @param server_loc
	 * @param data
	 * @throws Exception
	 */
	public AbstractSampleClient(String server_loc, TestData data) throws Exception{
		super();
		init(server_loc, data);
	}
	
	private void init(String server_loc, TestData data)throws Exception{
		this.data = data;
		this.server_loc=server_loc;
	}
	
	public SearchService2Stub getStub() throws AxisFault {
		if(this._stub==null){
		
			this._stub = new SearchService2Stub(server_loc+"/SpeechIndexing/services/SearchService2");
			
			// by default, axis2 client times out rather quickly. 
			// Let's set all timeout value to something more reasonable: 2 minutes (120000 ms):
			Options options = this._stub._getServiceClient().getOptions();
			options.setProperty(HTTPConstants.SO_TIMEOUT,new Integer(120000));
			options.setProperty(HTTPConstants.CONNECTION_TIMEOUT,new Integer(120000));
			options.setTimeOutInMilliSeconds(120000);
			options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_FALSE);
		}
		
		return _stub;
	}
	
	public SpeechIndexingStub getSpeechIndexingStub() throws AxisFault {
		if(this._stub2==null){
		
			this._stub2 = new SpeechIndexingStub(server_loc+"/SpeechIndexing/services/SpeechIndexing");
			
			// by default, axis2 client times out rather quickly. 
			// Let's set all timeout value to something more reasonable: 2 minutes (120000 ms):
			Options options = this._stub._getServiceClient().getOptions();
			options.setProperty(HTTPConstants.SO_TIMEOUT,new Integer(120000));
			options.setProperty(HTTPConstants.CONNECTION_TIMEOUT,new Integer(120000));
			options.setTimeOutInMilliSeconds(120000);
			options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, Constants.VALUE_FALSE);
		}
		
		return _stub2;
	}

	public TestData getData() {
		return data;
	}

	public void setData(TestData data) {
		this.data = data;
	}
	
	protected String getRESTfulBaseURL(String action){
		return 	server_loc+"/"+serviceName+"/"+action+"?authData.email="+data.getUserId()+"&authData.passwd="+data.getPasswd();
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	protected void fail(String msg){
		throw new RuntimeException(msg);
	}
}
