package com.style24.batch.biz.service;

import com.gagaframework.web.parameter.GagaMap;
import com.style24.batch.biz.dao.TsbCouponDao;
import com.style24.batch.support.env.TsbConstants;
import com.style24.core.biz.service.TscCouponService;
import com.style24.core.biz.service.TscKakaotalkService;
import com.style24.core.biz.service.TscMailService;
import com.style24.core.support.env.TscConstants;
import com.style24.persistence.domain.Coupon;
import com.style24.persistence.domain.CustCoupon;
import com.style24.persistence.domain.Customer;
import com.style24.persistence.domain.CustomerSearch;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * 쿠폰 Service
 *
 * @author jsshin
 * @since 2021. 05. 27
 */
@Service
@Slf4j
public class TsbCouponService {
	@Autowired
	private TsbCouponDao couponDao;

	@Autowired
	private TsbCustomerService customerService;

	@Autowired
	private TscKakaotalkService kakaotalkService;

	@Autowired
	private TscCouponService coreCouponService;

	@Autowired
	private TscMailService mailService;


	@Transactional("shopTxnManager")
	public GagaMap saveDivisonCombackCoupon(CustCoupon custCoupon, CustomerSearch customerSearch, int pageSize) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		int totalCount = customerService.getComBackNotiTarketListCount(customerSearch);
		if (totalCount < 1 ) {
			result.setInt("succCnt", succCnt);
			result.setInt("failCnt", failCnt);
			return result;
		}

		int totalPage = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			totalPage++;
		}

		for (int page = 0; page < totalPage; page++) {
			int limitStartRow = (page * pageSize + 1) - 1;
			customerSearch.setLimitStartRow(limitStartRow);
			customerSearch.setPageSize(pageSize);
			Collection<Customer> customerCollection = customerService.getComBackNotiTarketList(customerSearch);
			result = saveCombackCoupon(customerCollection, custCoupon);
			succCnt += result.getInt("succCnt");
			failCnt += result.getInt("failCnt");
		}

		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);
		return result;
	}

	public GagaMap saveCombackCoupon(Collection<Customer> customerCollection, CustCoupon custCoupon) {
		GagaMap result = new GagaMap();
		int succCnt = 0;
		int failCnt = 0;
		Integer senderNo = TsbConstants.REG_NO;
		for (Customer customer: customerCollection) {
			try {
				custCoupon.setCustNo(customer.getCustNo());
				custCoupon.setEmailAgreeDt(customer.getEmailAgreeDt());
				custCoupon.setPubReason(TscConstants.PubReason.DORMANT_COMABK_COUPON.value());
				custCoupon.setEndAlimSendYn("N");		// 알림 발송 여부(발송되면 Y)
				custCoupon.setUpdNo(customer.getCustNo());
				custCoupon.setRegNo(customer.getCustNo());
				coreCouponService.saveCouponIssue(custCoupon);

				if (StringUtils.isNotBlank(customer.getCellPhnno()) && "Y".equals(customer.getSmsAgreeYn())) {
					try {
						kakaotalkService.sendCombackCoupon(customer, custCoupon, senderNo);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
				}

				if (StringUtils.isNotBlank(customer.getEmail()) && "Y".equals(customer.getEmailAgreeYn())) {
					try {
						mailService.sendCombackCoupon(customer, custCoupon, senderNo);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
				}

				succCnt++;
			} catch (Exception e) {
				log.error(e.getMessage());
				failCnt++;
			}
		}
		result.setInt("succCnt", succCnt);
		result.setInt("failCnt", failCnt);
		return result;
	}

	/**
	 * 쿠폰혜택 및 정보
	 *
	 * @param cpnId - 쿠폰아이디
	 * @return CustCoupon
	 * @author jsshin
	 * @since  2021. 07. 27
	 */
	public CustCoupon getCombackCouponInfo(Integer cpnId) {
		CustCoupon custCoupon = new CustCoupon();
		custCoupon.setSiteCd(TscConstants.Site.STYLE24.value());
		custCoupon.setCpnId(cpnId);
		return couponDao.getCouponBenefitInfo(custCoupon);
	}
	
	/**
	 * 쿠폰 기간만료 알림톡(7일) 리스트
	 *
	 * @param coupon
	 * @return Collection<Coupon>
	 * @author sowon
	 * @since  2021. 05. 31
	 */
	@Transactional("shopTxnManager")
	public Collection<Coupon> getSendCouponExpectList(Coupon coupon){
		return couponDao.getSendCouponExpectList(coupon);
	}

}