package com.style24.front.biz.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gagaframework.web.parameter.GagaMap;
import com.style24.core.biz.dao.TscOrderChangeDao;
import com.style24.core.biz.service.TscOrderChangeService;
import com.style24.core.support.env.TscConstants;
import com.style24.front.biz.dao.TsfOrderChangeDao;
import com.style24.persistence.domain.GiftCard;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderChange;
import com.style24.persistence.domain.Point;

import lombok.extern.slf4j.Slf4j;

/**
 * 주문 변경 Service
 *
 * @author card007
 * @since 2021. 02. 26
 */
@Service
@Slf4j
public class TsfOrderChangeService {

	@Autowired
	private TsfOrderChangeDao orderChangeDao;

	@Autowired
	private TscOrderChangeDao coreOrderChangeDao;

	@Autowired
	private TscOrderChangeService coreOrderChangeService;

	/**
	 * 마이페이지 취소 목록 조회
	 *
	 * @param Order
	 * @return Collection<Order>
	 * @author card007
	 * @since 2021. 03. 19
	 */
	public GagaMap getCancelListForMypage(Order order) {
		GagaMap map = new GagaMap();
		Collection<Order> result = new ArrayList<>();
		List<Integer> ordDtlNoList = new ArrayList<>();

		for (Order tmpOrder : orderChangeDao.getCancelListForMypage(order)) {
			// 세트상품 옵션별 배열로 담기
			if (tmpOrder.getItemNm().contains("!@!")) {
				tmpOrder.setItemNmArr(tmpOrder.getItemNm().split("!@!"));
				tmpOrder.setColorNmArr(tmpOrder.getColorNm().split(","));
				tmpOrder.setOptCd1Arr(tmpOrder.getOptCd1().split(","));
				tmpOrder.setOptCd2Arr(tmpOrder.getOptCd2().split(","));
			} else {
				String[] arr = {tmpOrder.getItemNm()}, arr2 = {tmpOrder.getColorNm()}, arr3 = {tmpOrder.getOptCd1()}, arr4 = {tmpOrder.getOptCd2()};
				tmpOrder.setItemNmArr(arr);
				tmpOrder.setColorNmArr(arr2);
				tmpOrder.setOptCd1Arr(arr3);
				tmpOrder.setOptCd2Arr(arr4);
			}
			
			ordDtlNoList.add(tmpOrder.getOrdDtlNo());
			result.add(tmpOrder);
		}

		Order oneData = result.iterator().next();
		oneData.setOrdDtlNoArr(ordDtlNoList.stream().mapToInt(Integer::intValue).toArray());

		map.set("oneData", oneData);
		map.set("cancelList", result);

		return map;
	}

	/**
	 * 마이페이지 반품 목록 조회
	 *
	 * @param Order
	 * @return Collection<Order>
	 * @author card007
	 * @since 2021. 02. 26
	 */
	public GagaMap getReturnListForMypage(Order order) {
		GagaMap map = new GagaMap();
		Collection<Order> result = new ArrayList<>();
		List<Integer> ordDtlNoList = new ArrayList<>();
		String returnAbleYn = "Y";

		for (Order tmpOrder : orderChangeDao.getReturnListForMypage(order)) {
			// 세트상품 옵션별 배열로 담기
			if (tmpOrder.getItemNm().contains("!@!")) {
				tmpOrder.setItemNmArr(tmpOrder.getItemNm().split("!@!"));
				tmpOrder.setColorNmArr(tmpOrder.getColorNm().split(","));
				tmpOrder.setOptCd1Arr(tmpOrder.getOptCd1().split(","));
				tmpOrder.setOptCd2Arr(tmpOrder.getOptCd2().split(","));
			} else {
				String[] arr = {tmpOrder.getItemNm()}, arr2 = {tmpOrder.getColorNm()}, arr3 = {tmpOrder.getOptCd1()}, arr4 = {tmpOrder.getOptCd2()};
				tmpOrder.setItemNmArr(arr);
				tmpOrder.setColorNmArr(arr2);
				tmpOrder.setOptCd1Arr(arr3);
				tmpOrder.setOptCd2Arr(arr4);
			}

			ordDtlNoList.add(tmpOrder.getOrdDtlNo());
			result.add(tmpOrder);
			
			if ("Y".equals(returnAbleYn)
				&& (!TscConstants.OrderChangeStat.RETURN.value().equals(tmpOrder.getChgStat()) || !TscConstants.OrderChangeStat.RETURN_WAIT.value().equals(tmpOrder.getChgStat()))) {
				returnAbleYn = "N";
			}
		}

		Order oneData = result.iterator().next();
		oneData.setOrdDtlNoArr(ordDtlNoList.stream().mapToInt(Integer::intValue).toArray());

		Collection<Order> returnOrderInfo = coreOrderChangeDao.getReturnOrderInfo(order);

		map.set("oneData", oneData);
		map.set("returnList", result);
		map.set("returnOrderInfo", returnOrderInfo);
		map.set("returnAbleYn", returnAbleYn);

		return map;
	}

	/**
	 * 사용 상품권 정보 조회
	 *
	 * @param Order
	 * @return Collection<GiftCard>
	 * @author card007
	 * @since 2021. 03. 02
	 */
	public Collection<GiftCard> getUsedGiftcardInfo(Order order) {
		return orderChangeDao.getUsedGiftcardInfo(order);
	}

	/**
	 * 사용 포인트 정보 조회
	 *
	 * @param Order
	 * @return Collection<Point>
	 * @author card007
	 * @since 2021. 03. 02
	 */
	public Collection<Point> getUsedPointInfo(Order order) {
		return orderChangeDao.getUsedPointInfo(order);
	}

	/**
	 * 마이페이지 교환 옵션 정보 조회
	 *
	 * @param OrderChange
	 * @return Collection<OrderChange>
	 * @author card007
	 * @since 2021. 03. 31
	 */
	public Collection<OrderChange> getExchangeOptionInfo(OrderChange orderChange) {
		Collection<OrderChange> exchangeOptionInfo = new ArrayList<>(); 
		for (OrderChange tmpOrderChange : orderChangeDao.getExchangeOptionInfo(orderChange)) {
			tmpOrderChange.setOptCdArr(tmpOrderChange.getChgOptCd().split(","));
			tmpOrderChange.setOptCd2Arr(tmpOrderChange.getChgOptCd2().split(","));
			tmpOrderChange.setCurrStockQtyArr(tmpOrderChange.getChgCurrStockQty().split(","));
			tmpOrderChange.setSoldoutYnArr(tmpOrderChange.getChgSoldoutYn().split(","));
			
			exchangeOptionInfo.add(tmpOrderChange);
		}
		
		return exchangeOptionInfo;
	}

	/**
	 * 마이페이지 취반교 목록 페이징 처리 주문변경번호 수 조회
	 *
	 * @param OrderChange
	 * @return int
	 * @author card007
	 * @since 2021. 04. 07
	 */
	public int getPagingCreListCount(OrderChange orderChange) { return orderChangeDao.getPagingCreListCount(orderChange); }

	/**
	 * 마이페이지 취반교 목록 페이징 처리 주문변경번호 조회
	 *
	 * @param OrderChange
	 * @return Collection<OrderChange>
	 * @author card007
	 * @since 2021. 04. 07
	 */
	public Collection<OrderChange> getPagingCreList(OrderChange orderChange) { return orderChangeDao.getPagingCreList(orderChange); }

	/**
	 * 마이페이지 취반교 목록 조회
	 *
	 * @param OrderChange
	 * @return Collection<OrderChange>
	 * @author card007
	 * @since 2021. 04. 07
	 */
	public Collection<GagaMap> getCreList(OrderChange orderChange) {
		Collection<GagaMap> creList = new ArrayList<>();
		Collection<OrderChange> orderChangeList = new ArrayList<>();
		Collection<OrderChange> exchangeOptionList = new ArrayList<>();
		int ordChgSq = 0;
		int index = 0;

		for (OrderChange tmpOrderChange : orderChangeDao.getCreList(orderChange)) {
			// 세트상품 옵션별 배열로 담기
			if (tmpOrderChange.getItemNm().contains("!@!")) {
				tmpOrderChange.setItemNmArr(tmpOrderChange.getItemNm().split("!@!"));
				tmpOrderChange.setColorNmArr(tmpOrderChange.getColorNm().split(","));
				tmpOrderChange.setOptCd1Arr(tmpOrderChange.getOptCd1().split(","));
				tmpOrderChange.setOptCd2Arr(tmpOrderChange.getOptCd2().split(","));
			} else {
				String[] arr = {tmpOrderChange.getItemNm()}, arr2 = {tmpOrderChange.getColorNm()}, arr3 = {tmpOrderChange.getOptCd1()}, arr4 = {tmpOrderChange.getOptCd2()};
				tmpOrderChange.setItemNmArr(arr);
				tmpOrderChange.setColorNmArr(arr2);
				tmpOrderChange.setOptCd1Arr(arr3);
				tmpOrderChange.setOptCd2Arr(arr4);
			}

			// 취반교 목록 데이터 설정
			if (index > 0 && ordChgSq != tmpOrderChange.getOrdChgSq()) {
				creList.add(setOrderChangeMap(orderChangeList, exchangeOptionList));
				orderChangeList = new ArrayList<>();
				exchangeOptionList = new ArrayList<>();
			}

			// 교환 시 교환옵션정보 설정
			if ("G680_40".equals(tmpOrderChange.getChgGb())) {
				OrderChange exchangeGoodsOptionInfo = coreOrderChangeDao.getExchangeGoodsOptionInfoList(tmpOrderChange);
				if (exchangeGoodsOptionInfo.getItemNm().contains("!@!")) {
					exchangeGoodsOptionInfo.setItemNmArr(exchangeGoodsOptionInfo.getItemNm().split("!@!"));
					exchangeGoodsOptionInfo.setColorNmArr(exchangeGoodsOptionInfo.getColorNm().split(","));
					exchangeGoodsOptionInfo.setOptCd1Arr(exchangeGoodsOptionInfo.getOptCd1().split(","));
					exchangeGoodsOptionInfo.setOptCd2Arr(exchangeGoodsOptionInfo.getOptCd2().split(","));
				} else {
					String[] arr = {exchangeGoodsOptionInfo.getItemNm()}, arr2 = {exchangeGoodsOptionInfo.getColorNm()}, arr3 = {exchangeGoodsOptionInfo.getOptCd1()}, arr4 = {tmpOrderChange.getOptCd2()};
					exchangeGoodsOptionInfo.setItemNmArr(arr);
					exchangeGoodsOptionInfo.setColorNmArr(arr2);
					exchangeGoodsOptionInfo.setOptCd1Arr(arr3);
					exchangeGoodsOptionInfo.setOptCd2Arr(arr4);
				}
				exchangeOptionList.add(exchangeGoodsOptionInfo);
			}

			orderChangeList.add(tmpOrderChange);
			ordChgSq = tmpOrderChange.getOrdChgSq();
			index++;
		}

		if (orderChangeList.size() > 0) {
			creList.add(setOrderChangeMap(orderChangeList, exchangeOptionList));
		}

		return creList;
	}

	private GagaMap setOrderChangeMap(Collection<OrderChange> creList, Collection<OrderChange> exchangeOptionList) {
		OrderChange orderChange = creList.iterator().next();

		GagaMap map = new GagaMap();
		map.set("giftPackYn", orderChange.getGiftPackYn());
		map.set("ordChgSq", orderChange.getOrdChgSq());
		map.set("ordDt", orderChange.getOrdDt());
		map.set("oneData", orderChange);
		map.set("creList", creList);
		map.set("exchangeOptionList", exchangeOptionList);

		return map;
	}

}
