package com.swfarm.biz.chain.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.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.swfarm.biz.chain.bo.CustomerOrderProcessSteps;
import com.swfarm.biz.chain.bo.SaleChannel;
import com.swfarm.biz.chain.dto.SearchCustomerOrdersCriteria;
import com.swfarm.biz.chain.srv.ChainQueryService;
import com.swfarm.biz.chain.srv.ChainService;
import com.swfarm.biz.ebay.srv.EbayQueryService;
import com.swfarm.biz.ebay.srv.EbayService;
import com.swfarm.pub.utils.SpringUtils;

public class ShowUsCustomerOrdersController extends AbstractController {

	private ChainQueryService chainQueryService;

	private ChainService chainService;

	private EbayService ebayService;

	public void setChainQueryService(ChainQueryService chainQueryService) {
		this.chainQueryService = chainQueryService;
	}

	public void setChainService(ChainService chainService) {
		this.chainService = chainService;
	}

	public void setEbayService(EbayService ebayService) {
		this.ebayService = ebayService;
	}

	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		SearchCustomerOrdersCriteria criteria = (SearchCustomerOrdersCriteria) req
				.getAttribute("criteria");
		if (criteria == null) {
			criteria = new SearchCustomerOrdersCriteria();
		}
		criteria.setSaleChannel(SaleChannel.CHANNEL_EBAY);
		List countryNameList = new ArrayList();
		countryNameList.add("US");
		countryNameList.add("USA");
		countryNameList.add("United States");
		countryNameList.add("Puerto Rico");
		criteria.setCountryNameList(countryNameList);
		criteria.setCustomerOrderProcessSteps(new String[] {
				CustomerOrderProcessSteps
						.getStep(CustomerOrderProcessSteps.STEP_UNDER_CONFIRMATION)[0],
				CustomerOrderProcessSteps
						.getStep(CustomerOrderProcessSteps.STEP_ALLOCATE_PRODUCTS)[0] });
		List customerOrders = this.chainQueryService
				.searchCustomerOrders(criteria);
		Map dataMap = new HashMap();
		dataMap.put("customerOrders", customerOrders);
		EbayQueryService ebayQueryService = (EbayQueryService) SpringUtils.getBean("ebayQueryService");
		List ebayAccounts = ebayQueryService.findEbayAccounts();
		dataMap.put("ebayAccounts", ebayAccounts);
		List shippingMethods = this.chainService.findShippingMethods();
		dataMap.put("shippingMethods", shippingMethods);
		return new ModelAndView("chain/uscustomerorders", dataMap);
	}
}
