package com.swfarm.biz.ebay.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
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.ebay.sdk.call.GetSellerTransactionsCall;
import com.ebay.soap.eBLBaseComponents.TransactionType;
import com.swfarm.biz.ebay.bo.EbayAccount;
import com.swfarm.biz.ebay.srv.EbayQueryService;
import com.swfarm.biz.ebay.srv.EbayService;
import com.swfarm.pub.framework.PaginationSupport;
import com.swfarm.pub.utils.DateUtils;
import com.swfarm.pub.utils.SpringUtils;

public class ShowEbayTransactionsController extends AbstractController {

	private EbayService ebayService;

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

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		Map dataMap = new HashMap();
		String startIndexStr = req.getParameter("startIndex");
		int startIndex = 0;
		int pageSize = 500;
		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);
			}
		}
		int startPage = PaginationSupport.getPage(startIndex, pageSize);
		String account = req.getParameter("account");
		String startDateStr = req.getParameter("startDate");
		Date startDate = null;
		if (StringUtils.isNotEmpty(startDateStr)) {
			startDate = DateUtils.stringToDate(startDateStr, "yyyy/MM/dd");
		}
		String endDateStr = req.getParameter("endDate");
		Date endDate = null;
		if (StringUtils.isNotEmpty(endDateStr)) {
			endDate = DateUtils.stringToDate(endDateStr, "yyyy/MM/dd");
		}
		if (StringUtils.isNotEmpty(account)) {
			dataMap.put("account", account);
			EbayAccount ebayAccount = this.ebayService
					.findEbayAccountByName(account);
			String token = ebayAccount.getToken();
			if (endDate == null) {
				endDate = this.ebayService.getEbayTime(ebayAccount).getTime();
				endDateStr = DateUtils.dateToString(endDate, "yyyy/MM/dd");
			}
			if (startDate == null) {
				startDate = DateUtils.getBeforeDate(endDate, 2);
				startDateStr = DateUtils.dateToString(startDate, "yyyy/MM/dd");
			}
			GetSellerTransactionsCall getSellerTransactionsCall = this.ebayService
					.getSellerTransactions(ebayAccount, startDate, endDate,
							startPage, pageSize);
			List<TransactionType> transactionList = new ArrayList<TransactionType>();
			TransactionType[] transactions = getSellerTransactionsCall
					.getSellerTransactions();
			if (transactions != null) {
				for (int i = 0; i < transactions.length; i++) {
					transactionList.add(transactions[i]);
				}
			}
			Collections.sort(transactionList, new Comparator() {

				public int compare(Object o1, Object o2) {
					TransactionType transaction1 = (TransactionType) o1;
					TransactionType transaction2 = (TransactionType) o2;
					return transaction1
							.getShippingDetails()
							.getSellingManagerSalesRecordNumber()
							.compareTo(
									transaction2
											.getShippingDetails()
											.getSellingManagerSalesRecordNumber());
				}

			});
			PaginationSupport pagination = new PaginationSupport(
					transactionList, getSellerTransactionsCall
							.getPaginationResult().getTotalNumberOfEntries(),
					startIndex);
			pagination.setPageSize(pageSize);
			int page = pagination.getPage(startIndex);
			int[] pageRange = pagination.getPageRange(page);
			dataMap.put("account", account);
			dataMap.put("ebayAccount", ebayAccount);
			dataMap.put("pagination", pagination);
			dataMap.put("currentPage", page);
			dataMap.put("pageRange", pageRange);
		}
		EbayQueryService ebayQueryService = (EbayQueryService) SpringUtils.getBean("ebayQueryService");
		List<EbayAccount> ebayAccounts = ebayQueryService.findEbayAccounts();
		dataMap.put("accounts", ebayAccounts);
		if (StringUtils.isNotEmpty(startDateStr)) {
			dataMap.put("startDateString", startDateStr);
		}
		if (StringUtils.isNotEmpty(endDateStr)) {
			dataMap.put("endDateString", endDateStr);
		}
		return new ModelAndView("ebay/transactions", dataMap);
	}

}
