package com.swfarm.biz.amazon.web;

import java.util.ArrayList;
import java.util.Arrays;
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.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.swfarm.biz.amazon.job.SynchronizeAmazonOrderStatusTask;
import com.swfarm.biz.amazon.srv.AmazonService;
import com.swfarm.biz.chain.srv.ChainManagementService;
import com.swfarm.biz.chain.srv.ChainService;
import com.swfarm.biz.oa.srv.UserService;
import com.swfarm.biz.warehouse.srv.WarehouseService;
import com.swfarm.pub.utils.SpringUtils;

@Controller
public class PushAmazonOrderStatusController {

	private Logger logger = Logger.getLogger(PushAmazonOrderStatusController.class);

	@Autowired
	private AmazonService amazonService;

	@Autowired
	private ChainManagementService chainManagementService;

	@Autowired
	private WarehouseService warehouseService;

	@Autowired
	private ChainService chainService;

	@RequestMapping("/amazon/push_amazonorderstatus.shtml")
	protected ModelAndView pushAmazonOrderStatus(HttpServletRequest req, HttpServletResponse res) throws Exception {
		Map dataMap = new HashMap();
		String[] accountNumbers = req.getParameterValues("account");
		String amazonOrderId = req.getParameter("amazonOrderId");
		if (StringUtils.isNotEmpty(amazonOrderId)) {
			List saleChannelOrderIds = new ArrayList();
			saleChannelOrderIds.add(amazonOrderId);
			pushAmazonOrderStatus(null, saleChannelOrderIds);
			dataMap.put("amazonOrderId", amazonOrderId);
		} else if (!ArrayUtils.isEmpty(accountNumbers)) {
			pushAmazonOrderStatus(accountNumbers, null);
			dataMap.put("accountNumberArr", getAccountNumberJsArr(accountNumbers));
		}
		return new ModelAndView("forward:/amazon/amazonorders.shtml");
	}

	private void pushAmazonOrderStatus(String[] accountNumbers, List saleChannelOrderIds) {
		SynchronizeAmazonOrderStatusTask task = new SynchronizeAmazonOrderStatusTask();
		AmazonService amazonService = (AmazonService) SpringUtils.getBean("amazonService");
		ChainService chainService = (ChainService) SpringUtils.getBean("chainService");
		UserService userService = (UserService) SpringUtils.getBean("userService");
		WarehouseService warehouseService = (WarehouseService) SpringUtils.getBean("warehouseService");
		ChainManagementService chainManagementService = (ChainManagementService) SpringUtils
				.getBean("chainManagementService");
		task.setAmazonService(amazonService);
		task.setChainService(chainService);
		task.setUserService(userService);
		task.setWarehouseService(warehouseService);
		task.setChainManagementService(chainManagementService);
		if (!ArrayUtils.isEmpty(accountNumbers)) {
			task.setAccountNumbers(Arrays.asList(accountNumbers));
		}
		if (CollectionUtils.isNotEmpty(saleChannelOrderIds)) {
			task.pushAmazonOrderStatus(saleChannelOrderIds, true);
		} else {
			task.pushAmazonOrderStatus();
		}
	}

	private String getAccountNumberJsArr(String[] accountNumbers) {
		StringBuffer jsBuffer = new StringBuffer();
		jsBuffer.append("[");
		if (accountNumbers != null && accountNumbers.length > 0) {
			for (int i = 0; i < accountNumbers.length; i++) {
				String accountNumber = accountNumbers[i];
				jsBuffer.append("'");
				jsBuffer.append(accountNumber);
				jsBuffer.append("'");
				if (i < accountNumbers.length - 1) {
					jsBuffer.append(",");
				}
			}
		}
		jsBuffer.append("]");
		return jsBuffer.toString();
	}

}
