package com.swfarm.biz.product.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.swfarm.biz.chain.bo.cnowapi.CnowCategory;
import com.swfarm.biz.chain.dto.SearchCnowCategoryCriteria;
import com.swfarm.biz.chain.srv.CnowService;
import com.swfarm.biz.product.bo.Category;
import com.swfarm.biz.product.bo.StockKeepingUnit;
import com.swfarm.biz.product.srv.ProductService;

public class EnterCreateCnowProductController extends AbstractController {

	@Resource
	private CnowService cnowService;
	
	@Resource
	private ProductService productService;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		String id = req.getParameter("id");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		StockKeepingUnit sku = this.productService.findStockKeepingUnit(new Long(id));
		dataMap.put("id", id);
		dataMap.put("stockKeepingUnit", sku);
		this.setCategory(dataMap);
		this.setContainBattery(sku, dataMap);
		this.setProductDeclaredName(sku, dataMap);
		return new ModelAndView("product/create_cnowproduct", dataMap);
	}
	
	private void setCategory(Map<String, Object> dataMap) {
		SearchCnowCategoryCriteria criteria = new SearchCnowCategoryCriteria();
		criteria.setCategory_level(0);
		List<CnowCategory> cnowCategories = this.cnowService.searchCnowCategories(criteria);
		if (CollectionUtils.isEmpty(cnowCategories)) {
			this.cnowService.getCategory();
		}
		cnowCategories = this.cnowService.searchCnowCategories(criteria);
		dataMap.put("cnowCategories", cnowCategories);
	}
	
	private void setContainBattery(StockKeepingUnit sku, Map<String, Object> dataMap) {
		List<String> tagCodes = sku.getSkuRelateTagCodes();
		if (CollectionUtils.isNotEmpty(tagCodes)) {
			for (String tagCode : tagCodes) {
				if ("builtinbattery".equalsIgnoreCase(tagCode)) {
					dataMap.put("contain_battery", 1);
				}
			}
		}
		dataMap.put("contain_battery", 0);
	}
	
	@SuppressWarnings("unchecked")
	private void setProductDeclaredName(StockKeepingUnit stockKeepingUnit, Map<String, Object> dataMap) {
		String declareEnName = null;
		String declareCnName = null;
		if (stockKeepingUnit != null) {
			declareEnName = stockKeepingUnit.getDeclareEnName();
			declareCnName = stockKeepingUnit.getDeclareCnName();
			if (StringUtils.isEmpty(declareEnName)
					|| StringUtils.isEmpty(declareCnName)) {
				Set relateCategories = stockKeepingUnit.getRelateCategories();
				if (CollectionUtils.isNotEmpty(relateCategories)) {
					Category relateCategory = (Category) relateCategories
							.iterator().next();
					if (StringUtils.isEmpty(declareEnName)) {
						declareEnName = relateCategory.getName();
					}
					if (StringUtils.isEmpty(declareCnName)) {
						declareCnName = relateCategory.getDisplayName();
					}
				}
			}
		}
		dataMap.put("product_declared_name", declareEnName);
		dataMap.put("product_declared_name_zh", declareCnName);
	}

}
