package com.uas.search.console.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.uas.search.console.core.util.ContextUtils;
import com.uas.search.console.dao.ComponentSimpleInfoDao;
import com.uas.search.console.model.BrandSimpleInfo;
import com.uas.search.console.model.ComponentSimpleInfo;
import com.uas.search.console.model.KindSimpleInfo;
import com.uas.search.console.service.InnerSearchService;
import com.uas.search.model.PageParams;
import com.uas.search.service.SearchService;

/**
 * 搜索请求
 * 
 * @author suntg
 * @since 2016年8月1日上午9:18:05
 */
@Controller
@RequestMapping("/search")
public class SearchController {

	@Autowired
	private SearchService searchService;

	private InnerSearchService innerSearchService = ContextUtils.getApplicationContext().getBean("searchServiceImpl",
			InnerSearchService.class);

	@RequestMapping("/kindIds/{keyword}")
	@ResponseBody
	public List<Long> seachKindIds(@PathVariable String keyword) {
		return searchService.getKindIds(keyword);
	}

	@RequestMapping("/kinds/{keyword}")
	@ResponseBody
	public List<Map<String, Object>> seachKinds(@PathVariable String keyword) {
		return searchService.getKinds(keyword);
	}

	@RequestMapping("/brandIds/{keyword}")
	@ResponseBody
	public List<Long> searchBrandIds(@PathVariable String keyword) {
		return searchService.getBrandIds(keyword);
	}

	@RequestMapping("/brands/{keyword}")
	@ResponseBody
	public List<Map<String, Object>> searchBrand(@PathVariable String keyword) {
		return searchService.getBrands(keyword);
	}

	@RequestMapping("/componentIds")
	@ResponseBody
	public Map<String, Object> searchComponentIds(String keyword, Integer page, Integer size, String filters) {
		PageParams pageParams = new PageParams();
		if (page != null) {
			pageParams.setPage(page);
		}
		if (size != null) {
			pageParams.setSize(size);
		}
		Map<String, Object> filtersMap = new HashMap<>();
		if (!StringUtils.isEmpty(filters)) {
			filtersMap.putAll(JSONObject.parseObject(filters));
		}
		pageParams.setFilters(filtersMap);
		return searchService.getComponentIds(keyword, pageParams);
	}

	@Autowired
	private ComponentSimpleInfoDao componentDao;

	@RequestMapping("/components")
	@ResponseBody
	public List<ComponentSimpleInfo> searchComponents(String keyword, Integer page, Integer size, String filters) {
		@SuppressWarnings("unchecked")
		List<Long> ids = (List<Long>) searchComponentIds(keyword, page, size, filters).get("componentIds");
		if (CollectionUtils.isEmpty(ids)) {
			return new ArrayList<ComponentSimpleInfo>();
		}

		Long[] idsLong = new Long[ids.size()];
		int i = 0;
		for (Long id : ids) {
			idsLong[i++] = id;
		}
		return componentDao.findByIdsInOrder(idsLong);
	}

	@RequestMapping("/kindIdsByComponent")
	@ResponseBody
	public Set<Long> searchKindIdsBySearchComponent(String keyword, String brandId) {
		return searchService.getKindIdsBySearchComponent(keyword, brandId);
	}

	@RequestMapping("/kindsByComponent")
	@ResponseBody
	public List<Map<String, Object>> searchKindsBySearchComponent(String keyword, String brandId) {
		return searchService.getKindsBySearchComponent(keyword, brandId);
	}

	@RequestMapping("/brandIdsByComponent")
	@ResponseBody
	public Set<Long> searchBrandIdsBySearchComponent(String keyword, String kindId) {
		return searchService.getBrandIdsBySearchComponent(keyword, kindId);
	}

	@RequestMapping("/brandsByComponent")
	@ResponseBody
	public List<Map<String, Object>> searchBrandsBySearchComponent(String keyword, String kindId) {
		return searchService.getBrandsBySearchComponent(keyword, kindId);
	}

	@RequestMapping("/similarKeywords/{keyword}")
	@ResponseBody
	public List<String> getSimilarKeywords(@PathVariable String keyword) {
		return searchService.getSimilarKeywords(keyword);
	}

	@RequestMapping("/similarComponents/{componentCode}")
	@ResponseBody
	public List<Map<String, Object>> getSimilarComponents(@PathVariable String componentCode) {
		return searchService.getSimilarComponents(componentCode);
	}

	@RequestMapping("/similarBrands/{brandName}")
	@ResponseBody
	public List<Map<String, Object>> getSimilarBrands(@PathVariable String brandName) {
		return searchService.getSimilarBrands(brandName);
	}

	@RequestMapping("/similarKinds/{kindName}")
	@ResponseBody
	public List<Map<String, Object>> getSimilarKinds(@PathVariable String kindName) {
		return searchService.getSimilarKinds(kindName);
	}

	@RequestMapping("/similarLeafKinds/{kindName}")
	@ResponseBody
	public List<Map<String, Object>> getSimilarLeafKinds(@PathVariable String kindName) {
		return searchService.getSimilarLeafKinds(kindName);
	}

	@RequestMapping("/similarKindsByLevel")
	@ResponseBody
	public List<Map<String, Object>> getSimilarKindsByLevel(String kindName, Short level) {
		return searchService.getSimilarKindsByLevel(kindName, level);
	}

	@RequestMapping("/similarPropertyValues")
	@ResponseBody
	public List<Map<String, String>> getSimilarPropertyValues(Long kindId, Long propertyId, String keyword,
			Long topNum) {
		return searchService.getSimilarPropertyValues(kindId, propertyId, keyword, topNum);
	}

	@RequestMapping("/kind/{id}")
	@ResponseBody
	public KindSimpleInfo getKind(@PathVariable Long id) {
		return innerSearchService.getKind(id);
	}

	@RequestMapping("/brand/{id}")
	@ResponseBody
	public BrandSimpleInfo getBrand(@PathVariable Long id) {
		return innerSearchService.getBrand(id);
	}

	@RequestMapping("/component/{id}")
	@ResponseBody
	public ComponentSimpleInfo getComponent(@PathVariable Long id) {
		return innerSearchService.getComponent(id);
	}
}
