package com.style24.admin.biz.service;

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.data.redis.connection.SortParameters;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.rest.server.GagaResponseStatus;
import com.style24.admin.biz.dao.TsaPgDao;
import com.style24.core.biz.dao.TscOrderDao;
import com.style24.core.support.env.TscConstants;
import com.style24.core.support.message.TscMessageByLocale;
import com.style24.persistence.domain.Goods;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderSample;
import com.style24.persistence.domain.OrderSampleGoods;

import lombok.extern.slf4j.Slf4j;

/**
 * PG 연동 Service
 *
 * @author jsh77b
 * @since 2020. 11. 16
 */
@Service
@Slf4j
public class TsaPgService {

	@Autowired
	private TscMessageByLocale message;

	@Autowired
	private Environment env;

	@Autowired
	private TscOrderDao orderDao;

	@Autowired
	private ObjectMapper mapper;
	
	@Autowired
	private TsaPgDao pgDao;
	
	/* 주문 샘플 데이터 (추후 삭제 예정) */
	public Collection<Goods> getOrderSampleGoodsInfo(Goods goods) {
		return pgDao.getOrderSampleGoodsInfo(goods);
	}
	
	public Goods getOrderSampleGoodsStock(Goods goods) {
		return pgDao.getOrderSampleGoodsStock(goods);
	}
	
	public GagaMap createOrderSample(OrderSample orderSample) {
		GagaMap result = new GagaMap();
		
		int custNo = orderSample.getCustNo();
		String ordNm = orderSample.getOrdNm();
		String ordPhnno = orderSample.getOrdPhnno();
		String ordEmail = orderSample.getOrdEmail();
		String recipZipcode = orderSample.getRecipZipcode();
		String recipBaseAddr = orderSample.getRecipBaseAddr();
		String recipDtlAddr = orderSample.getRecipDtlAddr();
		
		
		OrderSample order = new OrderSample();
		order.setCustNo(custNo);
		order.setOrdNm(ordNm);
		order.setOrdPhnno(ordPhnno);
		order.setOrdEmail(ordEmail);
		pgDao.createOrderSample(order);
		
		int ordNo = order.getOrdNo();
		
		OrderSample deliveryAddr = new OrderSample();
		deliveryAddr.setOrdNm(ordNm);
		deliveryAddr.setOrdPhnno(ordPhnno);
		deliveryAddr.setRecipZipcode(recipZipcode);
		deliveryAddr.setRecipBaseAddr(recipBaseAddr);
		deliveryAddr.setRecipDtlAddr(recipDtlAddr);
		deliveryAddr.setCustNo(custNo);
		pgDao.createDelveryAddrSample(deliveryAddr);
		
		int delvAddrSq = deliveryAddr.getDelvAddrSq();

		int delvFeeSq = 0;
		int ordDtlNo = 0;
		for (OrderSampleGoods orderSampleGoods : orderSample.getGoods()) {
			String goodsCd = orderSampleGoods.getGoodsCd();
			String compsGoodsCd = orderSampleGoods.getCompsGoodsCd();
			int ordQty = orderSampleGoods.getOrdQty();
			String optCd = orderSampleGoods.getOptCd();
			
			
			OrderSample deliveryFee = new OrderSample();
			deliveryFee.setOrdNo(ordNo);
			deliveryFee.setDelvFeeGb(TscConstants.DeliveryFeeGb.ORIGIN_DELIVERY_FEE.value());
			deliveryFee.setCustNo(custNo);
			deliveryFee.setGoodsCd(goodsCd);
			pgDao.createDeliveryFeeSample(deliveryFee);
			
			if (deliveryFee.getDelvFeeSq() != 0) {
				delvFeeSq = deliveryFee.getDelvFeeSq();
			}
			
			OrderSample orderDetail = new OrderSample();
			orderDetail.setOrdNo(ordNo);
			orderDetail.setOrdDtlStat(TscConstants.OrderDetailStat.PAYMENT_COMPLETE.value());
			orderDetail.setOrdQty(ordQty);
			orderDetail.setDelvAddrSq(delvAddrSq);
			orderDetail.setCustNo(custNo);
			orderDetail.setGoodsCd(goodsCd);
			pgDao.createOrderDetailSample(orderDetail);

			if (orderDetail.getOrdDtlNo() != 0) {
				ordDtlNo = orderDetail.getOrdDtlNo();
			}
			
			OrderSample orderDetailItem = new OrderSample();
			orderDetailItem.setOrdQty(ordQty);
			orderDetailItem.setCustNo(custNo);
			orderDetailItem.setCompsGoodsCd(compsGoodsCd);
			orderDetailItem.setOptCd(optCd);
			orderDetailItem.setOrdDtlNo(ordDtlNo);
			pgDao.createOrderDetailItemSample(orderDetailItem);
			
			int ordDtlItemSq = orderDetailItem.getOrdDtlItemSq();
		}
		
		OrderSample payment = new OrderSample();
		payment.setCustNo(custNo);
		payment.setOrdNo(ordNo);
		pgDao.createPaymentSample(payment);
		
		int paySq = payment.getPaySq();
		
		OrderSample updateDeliveryFee = new OrderSample();
		updateDeliveryFee.setPaySq(paySq);
		updateDeliveryFee.setOrdNo(ordNo);
		pgDao.updateDeliveryFeeSample(updateDeliveryFee);

		result.set("status", GagaResponseStatus.SUCCESS.getCode());

		return result;
	}
	/* //주문 샘플 데이터 (추후 삭제 예정) */
}
