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

import com.swfarm.biz.chain.srv.ChainQueryService;
import com.swfarm.biz.ebay.bo.EbayAccount;
import com.swfarm.biz.ebay.bo.EbayCaseStatus;
import com.swfarm.biz.ebay.dto.SearchEbayCasesCriteria;
import com.swfarm.biz.ebay.srv.EbayQueryService;
import com.swfarm.biz.ebay.srv.EbayResolutionService;
import com.swfarm.biz.ebay.srv.EbayService;
import com.swfarm.pub.framework.PaginationSupport;
import com.swfarm.pub.utils.SpringUtils;

public class ShowEbayCasesController extends AbstractController {

	private EbayService ebayService;

	private EbayResolutionService ebayResolutionService;

	private ChainQueryService chainQueryService;

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

	public void setEbayResolutionService(EbayResolutionService ebayResolutionService) {
		this.ebayResolutionService = ebayResolutionService;
	}

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

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception {
		SearchEbayCasesCriteria criteria = (SearchEbayCasesCriteria) req.getAttribute("criteria");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		String startIndexStr = req.getParameter("startIndex");
		String pageSizeStr = req.getParameter("pageSize");
		int startIndex = 0;
		int pageSize = 50;
		if (StringUtils.isNotEmpty(startIndexStr)) {
			startIndex = new Integer(startIndexStr).intValue();
		} else {
			String pageStr = req.getParameter("page");
			if (StringUtils.isNotEmpty(pageStr)) {
				startIndex = PaginationSupport.getPageStartIndex(new Integer(pageStr), pageSize);
			}
		}
		if (StringUtils.isNotEmpty(pageSizeStr)) {
			pageSize = new Integer(pageSizeStr);
		}
		if (criteria == null) {
			criteria = new SearchEbayCasesCriteria();
			dataMap.put("criteria", criteria);
		}
		if (criteria.getEbayCaseStatuses() == null || criteria.getEbayCaseStatuses().length == 0) {
			criteria.setEbayCaseStatuses(new String[] { EbayCaseStatus.CANCEL_TRANSACTION_MY_RESPONSE_DUE.name(),
					EbayCaseStatus.EBP_INR_MY_RESPONSE_DUE.name(),
					EbayCaseStatus.EBP_INR_OTHER_PARTY_CONTACTED_CS_AWAITING_RESPONSE.name(),
					EbayCaseStatus.EBP_SNAD_MY_RESPONSE_DUE.name(),
					EbayCaseStatus.EBP_SNAD_OTHER_PARTY_CONTACTED_CS_AWAITING_RESPONSE.name(),
					EbayCaseStatus.EBP_SNAD_MY_PAYMENT_DUE.name(), EbayCaseStatus.INR_MY_RESPONSE_DUE.name(),
					EbayCaseStatus.INR_OTHER_PARTY_CONTACTED_CS_AWAITING_RESPONSE.name(),
					EbayCaseStatus.INR_MY_PAYMENT_DUE.name(), EbayCaseStatus.PAYPAL_INR_OPEN.name(),
					EbayCaseStatus.PAYPAL_INR_MY_RESPONSE_DUE.name(), EbayCaseStatus.PAYPAL_SNAD_OPEN.name(),
					EbayCaseStatus.PAYPAL_SNAD_MY_RESPONSE_DUE.name(), EbayCaseStatus.RETURN_MY_RESPONSE_DUE.name(),
					EbayCaseStatus.SNAD_MY_RESPONSE_DUE.name(), EbayCaseStatus.UPI_MY_RESPONSE_DUE.name() });
		}
		PaginationSupport pagination = this.ebayResolutionService.searchEbayCases(criteria, startIndex, pageSize);
		int page = pagination.getPage(startIndex);
		int[] pageRange = pagination.getPageRange(page);
		dataMap.put("pagination", pagination);
		dataMap.put("currentPage", page);
		dataMap.put("pageRange", pageRange);

		Map<String, Long> statMap = chainQueryService.getEbayCaseStatusStat();
		dataMap.put("statMap", statMap);

		referenceData(dataMap);
		return new ModelAndView("ebay/ebaycases", dataMap);
	}

	private void referenceData(Map<String, Object> dataMap) {
		EbayQueryService ebayQueryService = (EbayQueryService) SpringUtils.getBean("ebayQueryService");
		List<EbayAccount> ebayAccounts = ebayQueryService.findEbayAccounts();
		dataMap.put("ebayAccounts", ebayAccounts);
		EbayCaseStatus[] ebayCaseStatusArr = EbayCaseStatus.values();
		List ebayCaseStatusList = new ArrayList();
		for (int i = 0; i < ebayCaseStatusArr.length; i++) {
			EbayCaseStatus ebayCaseStatus = ebayCaseStatusArr[i];
			ebayCaseStatusList.add(new String[] { ebayCaseStatus.name(), ebayCaseStatus.getDesc() });
		}
		dataMap.put("ebayCaseStatusList", ebayCaseStatusList);
	}
}
