package com.style24.batch.biz.job.naverpay;

import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.checkout.base.ChangedProductOrderInfo;
import com.style24.core.biz.checkout.base.ProductOrderChangeType;
import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbNaverPayService;
import com.style24.batch.biz.service.TsbOrderService;
import com.style24.core.biz.checkout.base.ChangedProductOrderInfo;
import com.style24.core.biz.service.TscNaverPayService;
import com.style24.core.biz.service.TscOrderChangeService;
import com.style24.core.biz.service.TscOrderService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Order;
import com.style24.persistence.domain.OrderChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Collection;

/**
 * 네이버페이 결제완료 데이터 수신
 *
 * @author xodud1202
 * @since 2021. 05. 27
 */
@Component
@Slf4j
public class TsbNaverPayRecvOrderCompleteJob extends TsbAbstractJob<ChangedProductOrderInfo[], ChangedProductOrderInfo[], Order> {
	@Autowired
	private TsbNaverPayService naverPayService;

	@Autowired
	private TscNaverPayService coreNaverPayService;

	private int succCnt = 0;
	private int failCnt = 0;

	@Override
	public ChangedProductOrderInfo[] read() throws Exception {
		log.info("<<<< 결제완료 수신 시작 >>>>");
		Order time = coreNaverPayService.setInitData();
		return coreNaverPayService.changedProductOrderList(ProductOrderChangeType.PAYED, time.getFrom(), time.getTo(), TsbConstants.REG_NO); // 변경 상품주문 조회;
	}

	@Override
	public ChangedProductOrderInfo[] process(ChangedProductOrderInfo[] changedProductOrderList) throws Exception {
		if(changedProductOrderList != null) {
			log.info("<<<< 수신건수: [{}]건 >>>>", changedProductOrderList.length);
		}
		return changedProductOrderList;
	}

	@Override
	public Order write(ChangedProductOrderInfo[] changedProductOrderList) throws Exception {
		Order result = new Order();
		if (changedProductOrderList != null) {
			try {
				result = coreNaverPayService.createOrderInfo(changedProductOrderList, TsbConstants.REG_NO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		log.info("<<<< 결제완료 수신 종료 >>>>");
		return result;
	}

	@Override
	public void notify(Order param) throws Exception {
		super.printResult(succCnt, failCnt);
	}
}