package com.swfarm.biz.product.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.swfarm.biz.aliexpress.bo.AliExpressItem;
import com.swfarm.biz.aliexpress.dto.SearchAliExpressItemsCriteria;
import com.swfarm.biz.aliexpress.srv.AliExpressService;
import com.swfarm.biz.ebay.bo.EbayItemSummary;
import com.swfarm.biz.ebay.dto.SearchEbayItemSummarysCriteria;
import com.swfarm.biz.ebay.srv.EbayService;
import com.swfarm.biz.product.bo.StockKeepingUnit;
import com.swfarm.biz.product.dto.SearchStockKeepingUnitsCriteria;
import com.swfarm.biz.product.srv.ProductService;
import com.swfarm.biz.report.utils.ExcelUtils;
import com.swfarm.biz.wish.bo.WishProduct;
import com.swfarm.biz.wish.dto.SearchWishProductsCriteria;
import com.swfarm.biz.wish.srv.WishService;

public class DownloadItemIdByEbaySiteController extends AbstractController {
	
	Logger logger = Logger.getLogger(DownloadItemIdByEbaySiteController.class);
	
	private String[] titles = {"货号", "状态", "采购价", "名称", "澳洲", "美国", "英国", "德国", "速卖通", "WISH"};
	
	private String fileName = "DownloadItemIdByEbaySite.xls";
	private String sheetName = "DownloadItemIdByEbaySite";
	
	private static final int DownLoad_Size = 3000;
	
	private ProductService productService;
	
	private EbayService ebayService;
	
	private AliExpressService aliExpressService;
	
	private WishService wishService;
	
	public void setWishService(WishService wishService) {
		this.wishService = wishService;
	}
	
	public void setAliExpressService(AliExpressService aliExpressService) {
		this.aliExpressService = aliExpressService;
	}
	
	public void setEbayService(EbayService ebayService) {
		this.ebayService = ebayService;
	}
	
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		String[] stockKeepingUnitIdStrs = req
				.getParameterValues("stockKeepingUnitId");
		List<StockKeepingUnit> stockKeepingUnits = new ArrayList<StockKeepingUnit>();
		if (stockKeepingUnitIdStrs != null && stockKeepingUnitIdStrs.length > 0) {
			List<Long> stockKeepingUnitIds = new ArrayList<Long>();
			for (int i = 0; i < stockKeepingUnitIdStrs.length; i++) {
				Long stockKeepingUnitId = new Long(stockKeepingUnitIdStrs[i]);
				stockKeepingUnitIds.add(stockKeepingUnitId);
			}
			stockKeepingUnits = this.productService
					.findStockKeepingUnitsByIds(stockKeepingUnitIds);
		} else {
			SearchStockKeepingUnitsCriteria criteria = (SearchStockKeepingUnitsCriteria) req
					.getAttribute("criteria");
			if (criteria != null) {
				stockKeepingUnits = this.productService
						.searchStockKeepingUnits((SearchStockKeepingUnitsCriteria) criteria);
			}
		}
		logger.warn("stockKeepingUnits.size <<<<<< " + stockKeepingUnits.size());
		
		List<String[]> cellValueList = new ArrayList<String[]>();
		List<String[]> cellValues = new ArrayList<String[]>();
		int downloadQuantity = stockKeepingUnits.size() / DownloadItemIdByEbaySiteController.DownLoad_Size + 1;
		
		if(stockKeepingUnits.size() > DownloadItemIdByEbaySiteController.DownLoad_Size){			
			for(int i=0;i<downloadQuantity;i++){
				List<StockKeepingUnit> keepingUnits = new ArrayList<StockKeepingUnit>();
				if((i+1)*DownloadItemIdByEbaySiteController.DownLoad_Size < stockKeepingUnits.size()){
					keepingUnits = stockKeepingUnits.subList(i*DownloadItemIdByEbaySiteController.DownLoad_Size,
							(i+1)*DownloadItemIdByEbaySiteController.DownLoad_Size);
				}
				else{
					keepingUnits = stockKeepingUnits.subList(i*DownloadItemIdByEbaySiteController.DownLoad_Size,
							stockKeepingUnits.size());
				}
				
				cellValues = getCellValue(keepingUnits);
				cellValueList.addAll(cellValues);
			}
		}else{
			cellValueList = getCellValue(stockKeepingUnits);
		}
		ExcelUtils.generateExcelForm(cellValueList, fileName, sheetName, titles, res);
		return null;
	}
	
	public List<String[]> getCellValue(List<StockKeepingUnit> stockKeepingUnits){
		
		List<String[]> cellValueList = new ArrayList<String[]>();
		List<EbayItemSummary> ebayItemSummaries = new ArrayList<EbayItemSummary>();
		List<AliExpressItem> aliExpressItems = new ArrayList<AliExpressItem>();
		List<WishProduct> wishProducts = new ArrayList<WishProduct>();
		if(CollectionUtils.isNotEmpty(stockKeepingUnits)){
			List<String> articleNumbers = new ArrayList<String>();
			for(StockKeepingUnit stockKeepingUnit : stockKeepingUnits){
				articleNumbers.add(stockKeepingUnit.getArticleNumber());
			}
			
			SearchEbayItemSummarysCriteria ebayCriteria = new SearchEbayItemSummarysCriteria();
			SearchAliExpressItemsCriteria aliCriteria = new SearchAliExpressItemsCriteria();
			SearchWishProductsCriteria wishCriteria = new SearchWishProductsCriteria();
			
			ebayCriteria.setArticleNumbers(articleNumbers);
			ebayItemSummaries = this.ebayService.searchEbayItemSummarys(ebayCriteria);
			aliCriteria.setArticleNumbers(articleNumbers);
			aliExpressItems = this.aliExpressService.searchAliExpressItems(aliCriteria);
			wishCriteria.setArticleNumbers(articleNumbers);
			wishProducts = this.wishService.searchWishProducts(wishCriteria);
			
			for(StockKeepingUnit stockKeepingUnit : stockKeepingUnits){
				
				String[] arr = new String[10];
				StringBuilder australiaItemIds = new StringBuilder();
				StringBuilder usItemIds = new StringBuilder();
				StringBuilder ukItemIds = new StringBuilder();
				StringBuilder germanyItemIds = new StringBuilder();
				StringBuilder aliexpressItemIds = new StringBuilder();
				StringBuilder wishItemIds = new StringBuilder();
				
				String articleNumber = stockKeepingUnit.getArticleNumber();
				if(CollectionUtils.isNotEmpty(ebayItemSummaries)){
					for(EbayItemSummary ebayItemSummary : ebayItemSummaries){
						if(ebayItemSummary.getSkuArticleNumberList().contains(articleNumber)){
							String str = ebayItemSummary.getAccountNumber() + ":" + ebayItemSummary.getItemId() + "\n";
							if("Australia".equals(ebayItemSummary.getEbaySite())){
								australiaItemIds.append(str);
							} if("US".equals(ebayItemSummary.getEbaySite())){
								usItemIds.append(str);
							} if("UK".equals(ebayItemSummary.getEbaySite())){
								ukItemIds.append(str);
							} if("Germany".equals(ebayItemSummary.getEbaySite())){
								germanyItemIds.append(str);
							}
						}
					}
				}
				
				if(CollectionUtils.isNotEmpty(aliExpressItems)){
					for(AliExpressItem aliExpressItem : aliExpressItems){
						if(aliExpressItem.getSkuArticleNumberList().contains(articleNumber)){	
							aliexpressItemIds.append(aliExpressItem.getAccountNumber() + ":" + aliExpressItem.getProductId() + "\n");
						}
					}
				}
				
				if(CollectionUtils.isNotEmpty(wishProducts)){
					for(WishProduct wishProduct : wishProducts){
						if(wishProduct.getSkuArticleNumberList().contains(articleNumber)){
							wishItemIds.append(wishProduct.getAccountNumber() + ":" + wishProduct.getParentSku() + "\n");
						}
					}
				}
				
				arr[0] = stockKeepingUnit.getArticleNumber();
				arr[1] = stockKeepingUnit.getSkuLifeCyclePhaseName();
				arr[2] = String.valueOf(stockKeepingUnit.getPurchasePrice());
				arr[3] = stockKeepingUnit.getDisplayName();
				arr[4] = australiaItemIds.toString();
				arr[5] = usItemIds.toString();
				arr[6] = ukItemIds.toString();
				arr[7] = germanyItemIds.toString();
				arr[8] = aliexpressItemIds.toString();
				arr[9] = wishItemIds.toString();
				
				cellValueList.add(arr);
			}
			
		}
		
		return cellValueList;
	}

}
