package com.style24.admin.biz.web;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.style24.admin.biz.service.TsaRendererService;
import com.style24.admin.biz.service.TsaStatisticsService;
import com.style24.admin.support.controller.TsaBaseController;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.Statistics;

import lombok.extern.slf4j.Slf4j;

/**
 * 통계 Controller
 *
 * @author gagamel
 * @since 2021. 8. 17
 */
@Controller
@RequestMapping("/statistics")
@Slf4j
public class TsaStatisticsController extends TsaBaseController {

	@Autowired
	private TsaStatisticsService statisticsService;

	@Autowired
	private TsaRendererService rendererService;

	@Autowired
	private ObjectMapper mapper;

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private Environment env;

	/**
	 * 일자별주문 화면
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 17
	 */
	@GetMapping("/daily/order/form")
	public ModelAndView dailyOrderForm() {
		ModelAndView mav = new ModelAndView();

		mav.setViewName("statistics/DailyOrderForm");

		return mav;
	}

	/**
	 * 일자별주문 목록
	 * @param statistics - 통계 정보
	 * @return
	 * @author gagamel
	 * @since 2021. 8. 17
	 */
	@PostMapping("/daily/order/list")
	@ResponseBody
	public Collection<Statistics> getDailyOrderList(@RequestBody Statistics statistics) {
		if (!StringUtils.isBlank(statistics.getExtmallIdList())) {
			try {
				String[] arrExtmallId = mapper.readValue(statistics.getExtmallIdList(), String[].class);
				statistics.setMultiExtmallId(arrExtmallId);
			} catch (Exception e) {
				throw new IllegalStateException("제휴몰 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		if (!StringUtils.isBlank(statistics.getSupplyCompCdList())) {
			try {
				String[] arrSupplyCompCd = mapper.readValue(statistics.getSupplyCompCdList(), String[].class);
				statistics.setMultiSupplyCompCd(arrSupplyCompCd);
			} catch (Exception e) {
				throw new IllegalStateException("업체코드 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		if (!StringUtils.isBlank(statistics.getAfLinkCdList())) {
			try {
				String[] arrAfLinkCd = mapper.readValue(statistics.getAfLinkCdList(), String[].class);
				statistics.setMultiAfLinkCd(arrAfLinkCd);
			} catch (Exception e) {
				throw new IllegalStateException("제휴채널 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		if (!StringUtils.isBlank(statistics.getBrandCdList())) {
			try {
				String[] arrBrandCd = mapper.readValue(statistics.getBrandCdList(), String[].class);
				statistics.setMultiBrandCd(arrBrandCd);
			} catch (Exception e) {
				throw new IllegalStateException("브랜드코드 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		if (!StringUtils.isBlank(statistics.getItemkindCdList())) {
			try {
				String[] arrItemkindCd = mapper.readValue(statistics.getItemkindCdList(), String[].class);
				statistics.setMultiItemkindCd(arrItemkindCd);
			} catch (Exception e) {
				throw new IllegalStateException("품목 검색중 오류로 인해 조회되지 않았습니다.");
			}
		}

		return statisticsService.getDailyOrderList(statistics);
	}

}
