package kr.co.i4way.genesys.config;

import java.util.Collection;

import com.genesyslab.platform.applicationblocks.com.ConfigException;
import com.genesyslab.platform.applicationblocks.com.IConfService;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLogin;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLoginInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgDNGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.queries.CfgPersonQuery;
import com.genesyslab.platform.commons.collections.KeyValueCollection;
import com.genesyslab.platform.configuration.protocol.types.CfgFlag;
import com.genesyslab.platform.configuration.protocol.types.CfgObjectState;

public class Person {

	public Person() {
	}

	/**
	 * Person을 조회한다.
	 * 
	 * @param service 
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Collection<CfgPerson> SelectPersons(final IConfService service)
			throws ConfigException, InterruptedException {
		// Read configuration objects:
		Collection<CfgPerson> persons = service.retrieveMultipleObjects(
				CfgPerson.class, new CfgPersonQuery());

		return persons;
	}

	/**
	 * Person을 조회한다.
	 * 
	 * @param int isAgent (1=agent, 0=admin)
	 * @param service
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Collection<CfgPerson> SelectPersons(int isAgent,
			final IConfService service) throws ConfigException,
			InterruptedException {
		// Read configuration objects:
		CfgPersonQuery personquery = new CfgPersonQuery();
		personquery.setIsAgent(isAgent);
		Collection<CfgPerson> persons = service.retrieveMultipleObjects(
				CfgPerson.class, new CfgPersonQuery());

		return persons;
	}

	/**
	 * Person을 조회한다.
	 * 
	 * @param iTenantDBID
	 * @param sEmployeeId
	 * @param service
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgPerson SelectPersons(int iTenantDBID,
			String sEmployeeId, final IConfService service)
			throws ConfigException, InterruptedException {
		// Read configuration objects:
		CfgPerson rtnPerson = null;
		CfgPersonQuery personquery = new CfgPersonQuery();
		personquery.setTenantDbid(iTenantDBID);
		personquery.setEmployeeId(sEmployeeId);
		Collection<CfgPerson> persons = service.retrieveMultipleObjects(
				CfgPerson.class, personquery);
		if(persons != null){
			for(CfgPerson person : persons){
				rtnPerson = person;
			}
		}
		return rtnPerson;
	}

	/**
	 * Person을 조회한다.
	 * 
	 * @param iTenantDBID
	 * @param iPersonDBID
	 * @param service
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Collection<CfgPerson> SelectPersons(int iTenantDBID,
			int iPersonDBID, final IConfService service)
			throws ConfigException, InterruptedException {
		// Read configuration objects:
		CfgPersonQuery personquery = new CfgPersonQuery();
		personquery.setTenantDbid(iTenantDBID);
		personquery.setDbid(iPersonDBID);
		Collection<CfgPerson> persons = service.retrieveMultipleObjects(
				CfgPerson.class, personquery);
		//
		// System.out.println("Read " + persons.size() + " persons:");
		// for (CfgPerson person : persons) {
		// System.out.println("  " + person.getUserName() + " - "
		// + person.getFirstName() + " " + person.getLastName());
		// }
		return persons;
	}

	/**
	 * Person을 생성한다.
	 * 
	 * @param sFirstNm
	 *            //이름
	 * @param sLastNm
	 *            //성
	 * @param sEmployeeId
	 *            //사원번호
	 * @param sUserNm
	 *            //성명
	 * @param service
	 *            //IcfgService
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgPerson createPerson(int iTenantDBID, String sFirstNm,
			String sLastNm, String sEmployeeId, String sUserNm,
			final IConfService service) throws ConfigException,
			InterruptedException {
		// Read configuration objects:
		CfgPerson person = new CfgPerson(service);
		person.setTenantDBID(iTenantDBID);
		person.setFirstName(sFirstNm);
		person.setLastName(sLastNm);
		person.setEmployeeID(sEmployeeId);
		person.setUserName(sUserNm);
		person.setIsAgent(CfgFlag.CFGTrue);

		KeyValueCollection kvc = new KeyValueCollection();
		kvc.addString("app", "true");
		person.getUserProperties().addList("app", kvc);

		person.save();

		return person;
	}

	/**
	 * Person 객체를 수정한다.
	 * 
	 * @param person
	 * @param sFirstNm
	 * @param sLastNm
	 * @param sEmployeeId
	 * @param sUserNm
	 * @param service
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgPerson modifyPerson(CfgPerson person, String sFirstNm,
			String sLastNm, String sEmployeeId, String sUserNm,
			final IConfService service) throws ConfigException,
			InterruptedException {
		// Read configuration objects:
		person.setFirstName(sFirstNm);
		person.setLastName(sLastNm);
		person.setEmployeeID(sEmployeeId);
		person.setUserName(sUserNm);

		person.save();

		return person;
	}

	/**
	 * Person의 사용여부를 저장한다.
	 * 
	 * @param person
	 * @param flag
	 * @param service
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgPerson setPersonState(CfgPerson person, boolean flag,
			final IConfService service) throws ConfigException,
			InterruptedException {
		// Read configuration objects:
		if (flag) {
			person.setState(CfgObjectState.CFGEnabled);
		} else {
			person.setState(CfgObjectState.CFGDisabled);
		}

		person.save();

		return person;
	}

	/**
	 * Person에 AgentLoginID를 Assign한다.
	 * 
	 * @param iTenantDBID
	 * @param iPersonDBID
	 * @param iAgentLoginDBID
	 * @param service
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public String setAgentLoginToPerson(CfgPerson personobj,
			CfgAgentLogin agentloginobj, final IConfService service)
			throws ConfigException, InterruptedException {
		String rtnval = "";
		try {
			// Read configuration objects:
			CfgAgentLoginInfo li = new CfgAgentLoginInfo(service, null);
			li.setAgentLoginDBID(agentloginobj.getDBID());
			li.setWrapupTime(0);

			// ps.setAgentInfo(makeAgentLogin(iTenantDBID, iAgentLoginDBID,
			// service));
			personobj.getAgentInfo().getAgentLogins().add(li);
			personobj.save();
			rtnval = "success";
		} catch (Exception e) {
			rtnval = "success";
		}

		return rtnval;

	}

	/**
	 * 
	 * @param iTenantDBID
	 * @param iPersonDBID
	 * @param iAgentLoginDBID
	 * @param service
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public String removeAgentLoginToPerson(int iTenantDBID, int iPersonDBID,
			final IConfService service) throws ConfigException,
			InterruptedException {
		// Read configuration objects:

		CfgPersonQuery personquery = new CfgPersonQuery();
		personquery.setTenantDbid(iTenantDBID);
		personquery.setDbid(iPersonDBID);
		Collection<CfgPerson> persons = service.retrieveMultipleObjects(
				CfgPerson.class, personquery);

		if (persons != null) {
			if (persons.size() == 1) {
				for (CfgPerson ps : persons) {
					// ps.setAgentInfo(makeAgentLogin(iTenantDBID,
					// iAgentLoginDBID, service));
					ps.setAgentInfo(new CfgAgentInfo(service, null));
					ps.save();
				}

			} else {
				return "Duplication Person";
			}
		} else {
			return "fail";
		}
		return "success";
	}
}
