package com.swfarm.biz.amazon.web;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.swfarm.biz.amazon.dto.SearchFBAWarehouseRecordsCriteria;
import com.swfarm.pub.framework.OrderItem;
import com.swfarm.pub.utils.DateUtils;

public class SearchFBAWarehouseRecordsController extends AbstractController {
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		SearchFBAWarehouseRecordsCriteria criteria = new SearchFBAWarehouseRecordsCriteria();
		String sku = req.getParameter("sku");
		if (StringUtils.isNotEmpty(sku)) criteria.setSku(sku);
		String sellerSku = req.getParameter("sellerSku");
		if (StringUtils.isNotEmpty(sellerSku)) criteria.setSellerSku(sellerSku);
		String productManagerNo = req.getParameter("productManagerNo");
		if (StringUtils.isNotEmpty(productManagerNo)) {
			criteria.setProductManagerNo(productManagerNo);
		}
		String accountNumber = req.getParameter("accountNumber");
		if (StringUtils.isNotEmpty(accountNumber)) {
			criteria.setAccountNumber(accountNumber);
		}
		String[] skuLifeCyclePhases = req
				.getParameterValues("skuLifeCyclePhases");
		if (skuLifeCyclePhases != null && skuLifeCyclePhases.length > 0) {
			List skuLifeCyclePhaseList = new ArrayList();
			for (int i = 0; i < skuLifeCyclePhases.length; i++) {
				if (StringUtils.isNotEmpty(skuLifeCyclePhases[i])) {
					skuLifeCyclePhaseList.add(skuLifeCyclePhases[i]);
				}
			}
			if (CollectionUtils.isNotEmpty(skuLifeCyclePhaseList)) {
				skuLifeCyclePhases = new String[skuLifeCyclePhaseList.size()];
				skuLifeCyclePhaseList.toArray(skuLifeCyclePhases);
				criteria.setSkuLifeCyclePhases(skuLifeCyclePhases);
			}
		}
		String minQuantity = req.getParameter("minQuantity");
		if (StringUtils.isNotEmpty(minQuantity)) 
			criteria.setMinQuantity(new Integer(
					minQuantity));
		String maxQuantity = req.getParameter("maxQuantity");
		if (StringUtils.isNotEmpty(maxQuantity)) 
			criteria.setMaxQuantity(new Integer(
					maxQuantity));
		String minInStockQuantity = req.getParameter("minInStockQuantity");
		if (StringUtils.isNotEmpty(minInStockQuantity)) 
			criteria.setMinInStockQuantity(new Integer(
					minInStockQuantity));
		String maxInStockQuantity = req.getParameter("maxInStockQuantity");
		if (StringUtils.isNotEmpty(maxInStockQuantity)) 
			criteria.setMaxInStockQuantity(new Integer(
					maxInStockQuantity));
		String minEarliestAvailability = req.getParameter("minEarliestAvailability");
		if (StringUtils.isNotEmpty(minEarliestAvailability)) {
			criteria.setMinEarliestAvailability(DateUtils.stringToDate(
					minEarliestAvailability, "yyyy/MM/dd"));
		}
		String maxEarliestAvailability = req.getParameter("maxEarliestAvailability");
		if (StringUtils.isNotEmpty(maxEarliestAvailability)) {
			criteria.setMaxEarliestAvailability(DateUtils.stringToDate(
					maxEarliestAvailability, "yyyy/MM/dd"));
		}
		String[] orderItemInfos = req.getParameterValues("orderItem");
		if (orderItemInfos != null && orderItemInfos.length > 0) {
			for (int i = 0; i < orderItemInfos.length; i++) {
				String[] orderItemInfo = StringUtils.split(orderItemInfos[i],
						"|");
				OrderItem orderItem = new OrderItem();
				orderItem.setField(orderItemInfo[0]);
				orderItem.setStatus(orderItemInfo[1]);
				orderItem.setIndex(new Integer(orderItemInfo[2]));
				criteria.addOrderItem(orderItem);
			}
		}
		Map dataMap = new HashMap();
		if (!(criteria.getSku() == null
				&& criteria.getSellerSku() == null
				&& criteria.getProductManagerNo() == null
				&& criteria.getAccountNumber() == null
				&& criteria.getSkuLifeCyclePhases() == null
				&& criteria.getMinQuantity() == null
				&& criteria.getMaxQuantity() == null
				&& criteria.getMinInStockQuantity() == null
				&& criteria.getMaxInStockQuantity() == null
				&& criteria.getMinEarliestAvailability() == null
				&& criteria.getMaxEarliestAvailability() == null
				&& CollectionUtils.isEmpty(criteria.getOrderItemList()))) {
			dataMap.put("criteria", criteria);
		}
		String saction = req.getParameter("saction");
		if ("synchronizeInStock".equals(saction)) {
			return new ModelAndView("forward:/amazon/synchronize_amazoninstockdata.shtml", dataMap);
		} else {
			return new ModelAndView(
				"forward:/amazon/fbawarehouserecord.shtml", dataMap);
		}
	}

}
