package de.hsadmin.remote;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.HashMap;
import java.util.Map;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import de.hsadmin.core.util.Config;


public class LongCustomerNameTest {

	private static final String CUSTMODULE = "member";
	private static final String PACMODULE = "pac";
	
	private XmlRpcClient client;
	private RemoteCASHelper cas;
	private Config config;
	
	private int tmp ;

	@Before
	public void setUp() throws Exception {
		client = RemoteTestHelper.getClient();
		cas = new RemoteCASHelper();
		config = Config.getInstance();
	}

	@After
	public void tearDown() throws Exception {
		client = null;
		cas = null;
	}

	@Test
	public void testAddCustomersAndPackets() {
		this.subTestAddCustomers( 99, 101) ;
		this.subTestAddPackets( "n99",  99, 101) ;
		this.subTestAddPackets( "n101",  99, 101) ;
	}

	//@Test
	private void subTestAddCustomers( int lo, int hi) {
		String user = "ad";
		int membersCount = -9999;
		try {
			membersCount = getCustomersCount(user);
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> setParams = new HashMap<String, String>();
		setParams.put("membercode", "hsh00-##");
		setParams.put("password", "geheimnIs");
		setParams.put("memberno", "2000##");
		setParams.put("membersince", "01.10.2010");
		setParams.put("contact_salut", "Herr");
		setParams.put("contact_title", "Dr.");
		setParams.put("contact_firstname", "Ömer Günther");
		setParams.put("contact_lastname", "Janßen-Müller");
		setParams.put("contact_street", "Hauptstr. ##");
		setParams.put("contact_zipcode", "99998");
		setParams.put("contact_city", "Musterstadt");
		setParams.put("contact_country", "D");
		setParams.put("contact_phone_private", "+49 9999 123456");
		setParams.put("contact_email", "rainer.mustermann@example.org");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				setParams };
		try {
//			client.execute(CUSTMODULE + ".add", params);
			for (int idx=lo; idx <= hi; idx++) {
				setParams.put("membercode", "hsh00-n" + idx);
				setParams.put("memberno", "" + (20000 + idx));
				setParams.put("contact_street", "Hauptstr. " + idx);	
				params[1] = cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL());
				client.execute(CUSTMODULE + ".add", params);
				assertEquals(membersCount + 1, tmp = getCustomersCount(user));
				membersCount = tmp;
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	//@Test
	public void subTestAddPackets(String customer, int lo, int hi) {
		int pacsCount = getPacsCount();
		String user = "ad";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, Object> pacParams = new HashMap<String, Object>();
		pacParams.put("name", "p#");
		pacParams.put("hive", "h99");
		pacParams.put("customer", customer);
		pacParams.put("basepac", "WEB/PAC");
		pacParams.put("curinetaddr", "176.9.242.76");
		HashMap<String, String> pacComponents = new HashMap<String, String>();
		pacComponents.put("QUOTA", "512");
		pacComponents.put("TRAFFIC", "5");
		pacComponents.put("DAEMON", "1");
		pacParams.put("components", pacComponents);
		Object[] pacCallBlock = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				pacParams };
		
		Map<String, Object> pwParams = new HashMap<String, Object>();
		pwParams.put("password", "test123");
		Map<String, String> pwhereParams = new HashMap<String, String>();
		pwhereParams.put("name", "p#");
		Object[] pwCallBlock = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				pwParams, pwhereParams };
		try {
			for (int idx=lo; idx <= hi; idx++) {
				pacParams.put("name", "p" + idx);
				pacCallBlock[1] = cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL());
				Object execute = client.execute(PACMODULE + ".add", pacCallBlock);
				assertTrue(execute instanceof Map<?, ?>);
				assertEquals(pacsCount + 1, tmp = getPacsCount());		
				pacsCount = tmp ;
				client.execute("user.update", pwCallBlock);
			}
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
	}

	private int getCustomersCount(String user) throws XmlRpcException {
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		Object execute = client.execute(CUSTMODULE + ".search", params);
		Object[] result = (Object[]) execute;
		return result.length;
	}

	private int getPacsCount() {
		int count = 0;
		String user = "hsh00-aaa";
		String grantingTicketURL = cas.getGrantingTicketURL(user);
		Map<String, String> whereParams = new HashMap<String, String>();
		whereParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
		Object[] params = new Object[] { user, 
				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
				whereParams };
		try {
			Object execute = client.execute(PACMODULE + ".search", params);
			Object[] result = (Object[]) execute;
			count = result.length;
		} catch (XmlRpcException e) {
			fail(e.getMessage());
		}
		return count;
	}
}
