package com.islandpacific.gui.server.customDataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.islandpacific.gui.server.ipmsRemoteCall.PoApproval;
import com.islandpacific.gui.server.ipmsRemoteCall.PoApprovalResponse;
import com.islandpacific.gui.server.ipmsRemoteCall.domain.PoApprovalStatus;
import com.isomorphic.datasource.BasicDataSource;
import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DSResponse;
import com.isomorphic.servlet.ISCHttpServletRequest;
import com.isomorphic.servlet.RequestContext;

public class PoApprovalDS extends BasicDataSource {

	private static final long serialVersionUID = 1L;
	private static final String PO_APPROVAL_FIELD_poList = "PoList";
	private static final String PO_APPROVAL_FIELD_fileGroupNumber = "FileGroupNumber";
	private static final String PO_APPROVAL_FIELD_printPo = "PrintPo";
	private static final String PO_APPROVAL_FIELD_printPackAndShipReports = "PrintPackAndShipReports";
	private static final String PO_APPROVAL_FIELD_printStoreRecievingWorksheet = "PrintStoreRecievingWorksheet";
	private static final String PO_APPROVAL_FIELD_printTicketOrderReport = "PrintTicketOrderReport";
	private static final String PO_APPROVAL_FIELD_printTotalsByColor = "PrintTotalsByColor";
	private static final String PO_APPROVAL_FIELD_printTotalsBySize = "PrintTotalsBySize";
	private static final String PO_APPROVAL_FIELD_printPrepackComponentInformation = "PrintPrepackComponentInformation";
	private static final String PO_APPROVAL_FIELD_poStatusList = "PoStatusList";
	private static final String PO_APPROVAL_STATUS_FIELD_poNumber = "PoNumber";
	private static final String PO_APPROVAL_STATUS_FIELD_isError = "IsError";
	private static final String PO_APPROVAL_STATUS_FIELD_statusCode = "StatusCode";
	private static final String PO_APPROVAL_STATUS_FIELD_statusMessage = "StatusMessage";
	
	@SuppressWarnings("unchecked")
	@Override
	public DSResponse execute(DSRequest req) throws Exception {
		
		DSResponse response = new DSResponse();

		if (req.getOperationType().equals("validate"))
		{
			//response.addError(PO_APPROVAL_FIELD_poList, "PO No Good");
			response = super.execute(req);
			return response;
		}
			//return super.execute(req);
		if (req.getOperationType().equals("update") || req.getOperationType().equals("add") ) {
			Map values = req.getValues();
			if (values == null) {
				response.setData("No values provided for PO Revision.");
				response.setFailure();
				return response;
			}

			List<String> poList = (List<String>) req.getValues().get(PO_APPROVAL_FIELD_poList);
			String[] poArray = new String[poList.size()];
			int poIndex = 0;
			for (String poNumber: poList) {
				poArray[poIndex++] = poNumber;
			}
			
			int fileGroup = Integer.valueOf(values.get(PO_APPROVAL_FIELD_fileGroupNumber).toString());
		
			Object valueObj = null;

			boolean printPo = false;
			valueObj = values.get(PO_APPROVAL_FIELD_printPo);
			if (valueObj != null) {
				printPo = (Boolean)valueObj;
			}
			String printPackAndShipReports= "N";
			valueObj = values.get(PO_APPROVAL_FIELD_printPackAndShipReports);
			if (valueObj != null) {
				printPackAndShipReports = (String)valueObj;
			}
			String printStoreRecievingWorksheet = "N";
			valueObj = values.get(PO_APPROVAL_FIELD_printStoreRecievingWorksheet);
			if (valueObj != null) {
				printStoreRecievingWorksheet = (String)valueObj;
			}
			boolean printTicketOrderReport = false;
			valueObj = values.get(PO_APPROVAL_FIELD_printTicketOrderReport);
			if (valueObj != null) {
				printTicketOrderReport = (Boolean)valueObj;
			}
			boolean printTotalsByColor = false;
			valueObj = values.get(PO_APPROVAL_FIELD_printTotalsByColor);
			if (valueObj != null) {
				printTotalsByColor = (Boolean)valueObj;
			}
			boolean printTotalsBySize = false;
			valueObj = values.get(PO_APPROVAL_FIELD_printTotalsBySize);
			if (valueObj != null) {
				printTotalsBySize = (Boolean)valueObj;
			}
			boolean printPrepackComponentInformation = false;
			valueObj = values.get(PO_APPROVAL_FIELD_printPrepackComponentInformation);
			if (valueObj != null) {
				printPrepackComponentInformation = (Boolean)valueObj;
			}
			
			PoApproval poApproval = new PoApproval();
			PoApprovalResponse approvalResponse = poApproval.approvePO(fileGroup, poArray, printPo, printPackAndShipReports, printStoreRecievingWorksheet, 
								printTicketOrderReport, printTotalsByColor, printTotalsBySize, printPrepackComponentInformation);

			if (approvalResponse.isError()) {
				response.setData(approvalResponse.getErrorMessages().get(0).getMessageDetails());
				response.setFailure();
			}
			
			Map responseData = new HashMap();
			
			List poStatusList = new ArrayList<String>();

			for (String poNumber: poList) {
				PoApprovalStatus status = approvalResponse.getPoApprovalList().get(poNumber);
				if (status != null) {
					Map poStatusEntry = new HashMap();
					poStatusEntry.put(PO_APPROVAL_STATUS_FIELD_poNumber, poNumber);
					poStatusEntry.put(PO_APPROVAL_STATUS_FIELD_statusCode, status.getStatusMessage().getMessage());
					poStatusEntry.put(PO_APPROVAL_STATUS_FIELD_statusMessage, status.getStatusMessage().getMessageDetails());
					poStatusEntry.put(PO_APPROVAL_STATUS_FIELD_isError, status.isError());
					poStatusList.add(poStatusEntry);
				}
			}
			
			responseData.put(PO_APPROVAL_FIELD_poStatusList, poStatusList);
			response.setData(responseData);
			
		}
		else {
			response.setData("Invalid operation type for PO Approval. Only 'update' is allowed.");
			response.setFailure();
		}

		return response;

	}
	
	protected static HttpSession getSessionFromDSRequest(DSRequest request) {
		HttpSession session = null;
		if (request instanceof IpDSRequest) {
			session = ((IpDSRequest)request).getSession();
		}
		if (session == null) {
			RequestContext requestContext = request.getRequestContext();
			if (requestContext != null) {
				ISCHttpServletRequest srvReq = requestContext.request;
				if (srvReq != null) {
					session = srvReq.getSession();
				}
			}
		}
		return session;
	}

}
