package com.swfarm.biz.ebay.web;

import java.util.HashMap;
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.ebay.soap.eBLBaseComponents.ItemType;
import com.ebay.soap.eBLBaseComponents.VariationType;
import com.ebay.soap.eBLBaseComponents.VariationsType;
import com.swfarm.biz.chain.bo.SaleChannel;
import com.swfarm.biz.chain.srv.ChainManagementService;
import com.swfarm.biz.chain.srv.GrossProfitTesterHelper;
import com.swfarm.biz.chain.web.ChainFrontHelper;
import com.swfarm.biz.ebay.bo.DefaultCustomLabelParser;
import com.swfarm.biz.ebay.bo.EbayAccount;
import com.swfarm.biz.ebay.bo.EbayItemSummary;
import com.swfarm.biz.ebay.srv.EbayService;

public class AddAuctionItemsController extends AbstractController {

	private EbayService ebayService;

	private ChainManagementService chainManagementService;

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

	public void setChainManagementService(ChainManagementService chainManagementService) {
		this.chainManagementService = chainManagementService;
	}

	protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception {
		Map dataMap = new HashMap();

		Long ebayItemSummaryId = new Long(req.getParameter("ebayItemSummaryId"));

		EbayItemSummary ebayItemSummary = this.ebayService.findEbayItemSummary(ebayItemSummaryId);

		String accountNumber = ebayItemSummary.getAccountNumber();
		String itemId = ebayItemSummary.getItemId();
		EbayAccount ebayAccount = this.ebayService.findEbayAccountByName(accountNumber);
		ItemType itemType = this.ebayService.syncItemFromEbay(accountNumber, itemId);
		String originTitle = itemType.getTitle();

		GrossProfitTesterHelper helper = new GrossProfitTesterHelper();

		if (ebayItemSummary.getIsMultipleItem() == true) {
			VariationsType variationsType = itemType.getVariations();
			VariationType[] variationTypes = variationsType.getVariation();
			for (int i = 0; i < variationTypes.length; i++) {
				VariationType variationType = variationTypes[i];
				String articleNumber = variationType.getSKU();
				Integer availableQuantity = variationType.getUnitsAvailable() == null ? 0
						: variationType.getUnitsAvailable();
				Integer quantity = variationType.getQuantity() == null ? 0 : variationType.getQuantity();
				Integer quantitySold = variationType.getSellingStatus() == null ? 0
						: variationType.getSellingStatus().getQuantitySold();
				quantitySold = quantitySold == null ? 0 : quantitySold;
				if (availableQuantity == 0) {
					availableQuantity = quantity - quantitySold;
				}
				if (availableQuantity > 0) {
					DefaultCustomLabelParser customLabelParser = new DefaultCustomLabelParser(articleNumber);
					String skuArticleNumber = customLabelParser.getArticleNumber();
					if (ChainFrontHelper.checkClearSkuLifeCyclePhase(skuArticleNumber)) {
						Double allocatableQuantity = ChainFrontHelper.getAllocatableQuantity(skuArticleNumber);
						if (allocatableQuantity > 0) {
							Double adjustedSalePrice = helper.calculateEbaySalePrice(skuArticleNumber,
									SaleChannel.CHANNEL_EBAY, ebayItemSummary.getEbaySite(), accountNumber,
									ebayItemSummary.getPriceAdjustmentSfmCode(), null, true, false, -0.5);
							if (adjustedSalePrice <= 0) {
								adjustedSalePrice = 0.01;
							}
							ItemType auctionItem = this.chainManagementService.addAuctionItem(ebayAccount, itemType,
									skuArticleNumber, originTitle, adjustedSalePrice);
							if (auctionItem != null) {
								ChainFrontHelper.reviseInventoryStatus(ebayAccount, ebayItemSummary.getItemId(),
										articleNumber, availableQuantity - 1);
							}
						}
					} else {
						this.chainManagementService.addAuctionItem(ebayAccount, itemType, skuArticleNumber, originTitle,
								null);
					}
				}
			}
		} else {
			String articleNumber = itemType.getSKU();
			Integer availableQuantity = itemType.getQuantityAvailable() == null ? 0 : itemType.getQuantityAvailable();
			Integer quantity = itemType.getQuantity() == null ? 0 : itemType.getQuantity();
			Integer quantitySold = itemType.getSellingStatus() == null ? 0
					: itemType.getSellingStatus().getQuantitySold();
			quantitySold = quantitySold == null ? 0 : quantitySold;
			if (availableQuantity == 0) {
				availableQuantity = quantity - quantitySold;
			}
			if (availableQuantity > 0) {
				DefaultCustomLabelParser customLabelParser = new DefaultCustomLabelParser(articleNumber);
				String skuArticleNumber = customLabelParser.getArticleNumber();
				if (ChainFrontHelper.checkClearSkuLifeCyclePhase(skuArticleNumber)) {
					Double allocatableQuantity = ChainFrontHelper.getAllocatableQuantity(skuArticleNumber);
					if (allocatableQuantity > 0) {
						Double adjustedSalePrice = helper.calculateEbaySalePrice(skuArticleNumber,
								SaleChannel.CHANNEL_EBAY, ebayItemSummary.getEbaySite(), accountNumber,
								ebayItemSummary.getPriceAdjustmentSfmCode(), null, true, false, -0.5);
						if (adjustedSalePrice <= 0) {
							adjustedSalePrice = 0.01;
						}
						ItemType auctionItem = this.chainManagementService.addAuctionItem(ebayAccount, itemType,
								skuArticleNumber, originTitle, adjustedSalePrice);
						if (auctionItem != null) {
							ChainFrontHelper.reviseInventoryStatus(ebayAccount, ebayItemSummary.getItemId(),
									articleNumber, availableQuantity - 1);
						}
					}
				} else {
					this.chainManagementService.addAuctionItem(ebayAccount, itemType, skuArticleNumber, originTitle,
							null);
				}
			}
		}
		return new ModelAndView("redirect:/ebay/search_ebayitemsummarys.shtml", dataMap);
	}

	public static void main(String[] args) {
		System.out.println("S123456".substring(0, "S123456".length() - 3));
	}
}
