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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.style24.admin.biz.dao.TsaCardPromotionDao;
import com.style24.admin.support.security.session.TsaSession;
import com.style24.persistence.domain.CardPromotion;
import com.style24.persistence.domain.CardPromotionCondition;
import com.style24.persistence.domain.CardPromotionTarget;

import lombok.extern.slf4j.Slf4j;

/**
 * 카드프로모션 Service
 *
 * @author eskim
 * @since 2021. 01. 29
 */
@Service
@Slf4j
public class TsaCardPromotionService {

	@Autowired
	private TsaCardPromotionDao cardPromotionDao;

	/**
	 * 카드무이자할부 목록 건수
	 * @param cardPromotion
	 * @return
	 * @author eskim
	 * @since 2021. 01. 29
	 */
	public int getCardInterestListCount(CardPromotion cardPromotion) {
		return cardPromotionDao.getCardInterestListCount(cardPromotion);
	}

	/**
	 * 카드무이자할부 목록
	 * @param cardPromotion
	 * @return
	 * @author eskim
	 * @since 2021. 01. 29
	 */
	public Collection<CardPromotion> getCardInterestList(CardPromotion cardPromotion) {
		return cardPromotionDao.getCardInterestList(cardPromotion);
	}

	/**
	 * 카드무이자할부 저장
	 * @param cardPromotion
	 * @return
	 * @author eskim
	 * @since 2021. 02. 1
	 */
	@Transactional("shopTxnManager")
	public void saveCardInterest(CardPromotion cardPromotion) {

		Integer userNo = TsaSession.getInfo().getUserNo();
		// 카드 프로모션 저장
		cardPromotion.setRegNo(userNo);
		cardPromotion.setUpdNo(userNo);

		cardPromotion.setPrmtStd(cardPromotion.getPrmtStd().replace("-", ""));
		cardPromotion.setPrmtEdd(cardPromotion.getPrmtEdd().replace("-", ""));

		cardPromotionDao.saveCardPromotion(cardPromotion);
		// 카드 프로모션 행사조건 저장
		if ("B".equals(cardPromotion.getPrmtGb())) {
			if (cardPromotion.getArrMinNoItrt().length > 0) {
				int cardPromotionSize = cardPromotion.getArrMinNoItrt().length;
				for(int i=0; i < cardPromotionSize; i++ ) {

					CardPromotionCondition cardPromotionCondition = new CardPromotionCondition();
					cardPromotionCondition.setRegNo(userNo);
					cardPromotionCondition.setUpdNo(userNo);
					cardPromotionCondition.setCardPrmtSq(cardPromotion.getCardPrmtSq());

					cardPromotionCondition.setMinNoItrt(cardPromotion.getArrMinNoItrt()[i].toString());
					cardPromotionCondition.setMaxNoItrt(cardPromotion.getArrMaxNoItrt()[i].toString());
					cardPromotionCondition.setMinPayAmt(Integer.parseInt(cardPromotion.getArrMinPayAmt()[i].toString()));
					if (cardPromotion.getCardPrmtCdtSq() != null && cardPromotion.getCardPrmtCdtSq().length > 0) {
						if (!StringUtils.isBlank(cardPromotion.getCardPrmtCdtSq()[i])) {
							cardPromotionCondition.setCardPrmtCdtSq(Integer.parseInt(cardPromotion.getCardPrmtCdtSq()[i].toString()));
						}
					}

					cardPromotionDao.saveCardPromotionCondition(cardPromotionCondition);
					// 카드 프로모션 카드 삭제
					cardPromotionDao.deleteCardPromotionTarget(cardPromotionCondition.getCardPrmtCdtSq());

					// 카드 프로모션 카드목록 저장
					if (cardPromotion.getArrCardCd().length > 0) {
						String cardCd =  cardPromotion.getArrCardCd()[i].toString(); //  형식 G941_01/G941_06/G941_11
						String [] arrCardCd = cardCd.split("/");
						int cardSize = arrCardCd.length;
						for(int j=0; j < cardSize; j++ ) {
							CardPromotionTarget cardPromotionTarget = new CardPromotionTarget();
							cardPromotionTarget.setRegNo(userNo);
							cardPromotionTarget.setUpdNo(userNo);
							cardPromotionTarget.setCardPrmtCdtSq(cardPromotionCondition.getCardPrmtCdtSq());
							cardPromotionTarget.setPrmtTargetCd(arrCardCd[j].toString());

							cardPromotionDao.createCardPromotionTarget(cardPromotionTarget);
						}
					}
				}
			}
		}else {

			CardPromotionCondition cardPromotionCondition = new CardPromotionCondition();
			cardPromotionCondition.setRegNo(userNo);
			cardPromotionCondition.setUpdNo(userNo);
			cardPromotionCondition.setCardPrmtSq(cardPromotion.getCardPrmtSq());

			cardPromotionCondition.setDcWay(cardPromotion.getDcWay());
			cardPromotionCondition.setDcVal(cardPromotion.getDcVal());
			cardPromotionCondition.setMaxDcAmt(cardPromotion.getMaxDcAmt());
			cardPromotionCondition.setMinPayAmt(cardPromotion.getMinPayAmt());
			if (cardPromotion.getCardPrmtCdtSq() != null && cardPromotion.getCardPrmtCdtSq().length > 0) {
				if (!StringUtils.isBlank(cardPromotion.getCardPrmtCdtSq()[0])) {
					cardPromotionCondition.setCardPrmtCdtSq(Integer.parseInt(cardPromotion.getCardPrmtCdtSq()[0].toString()));
				}
			}

			cardPromotionDao.saveCardPromotionCondition(cardPromotionCondition);
			// 카드 프로모션 카드 삭제
			cardPromotionDao.deleteCardPromotionTarget(cardPromotionCondition.getCardPrmtCdtSq());

			// 카드 프로모션 카드목록 저장
			if (cardPromotion.getArrCardCd().length > 0) {
				String cardCd =  cardPromotion.getArrCardCd()[0].toString(); //  형식 G941_01/G941_06/G941_11
				String [] arrCardCd = cardCd.split("/");
				int cardSize = arrCardCd.length;
				for(int j=0; j < cardSize; j++ ) {
					CardPromotionTarget cardPromotionTarget = new CardPromotionTarget();
					cardPromotionTarget.setRegNo(userNo);
					cardPromotionTarget.setUpdNo(userNo);
					cardPromotionTarget.setCardPrmtCdtSq(cardPromotionCondition.getCardPrmtCdtSq());
					cardPromotionTarget.setPrmtTargetCd(arrCardCd[j].toString());

					cardPromotionDao.createCardPromotionTarget(cardPromotionTarget);
				}
			}

		}


		// 삭제할 프로모션 조건이 있는경우
		if(!StringUtils.isEmpty(cardPromotion.getDelCardPrmtCdtSq())) {
			String [] delCardPrmtCdtSq = cardPromotion.getDelCardPrmtCdtSq().split("/");
			int cardPrmtCdtSq = 0;
			for(int j=0; j < delCardPrmtCdtSq.length; j++ ) {
				if (!StringUtils.isBlank(delCardPrmtCdtSq[j])) {
					cardPrmtCdtSq = Integer.parseInt(delCardPrmtCdtSq[j]);
					cardPromotionDao.deleteCardPromotionCondition(cardPrmtCdtSq);
					cardPromotionDao.deleteCardPromotionTarget(cardPrmtCdtSq);
				}
			}

		}
	}

	/**
	 * 카드무이자할부 조회
	 * @param cardPrmtSq
	 * @return
	 * @author eskim
	 * @since 2021. 02. 1
	 */
	public CardPromotion getCardInterest(Integer cardPrmtSq) {
		CardPromotion cardPrmt = new CardPromotion();
		cardPrmt.setCardPrmtSq(cardPrmtSq);
		cardPrmt.setPrmtGb("B");	// 무이자
		cardPrmt.setSearch("1");	// 프로모션id
		cardPrmt.setCondition(Integer.toString(cardPrmtSq));
		Collection<CardPromotion> cardInterestList = cardPromotionDao.getCardInterestList(cardPrmt);

		CardPromotion cardPromotion = new CardPromotion();
		if (cardInterestList != null && !cardInterestList.isEmpty()) {
			cardPromotion = cardInterestList.iterator().next();
		}

		Collection<CardPromotionCondition> cardPrmtCdtList = cardPromotionDao.getCardPromotionConditionList(cardPrmtSq);


		for (CardPromotionCondition cardPromotionCondition: cardPrmtCdtList) {
			String cardCd = "";
			String cardNm = "";
			Collection<CardPromotionTarget> cardPrmtTgtList = cardPromotionDao.getCardPromotionTargetList(cardPromotionCondition.getCardPrmtCdtSq());
			for(CardPromotionTarget cardPromotionTarget : cardPrmtTgtList) {
				cardCd += cardPromotionTarget.getPrmtTargetCd() +"/";
				cardNm += cardPromotionTarget.getPrmtTargetNm() +"/";
			}

			cardPromotionCondition.setArrCardCd(cardCd.substring(0, cardCd.lastIndexOf("/")));
			cardPromotionCondition.setArrCardNm(cardNm.substring(0, cardNm.lastIndexOf("/")));
		}

		cardPromotion.setCardPrmtCdtList(cardPrmtCdtList);

		return cardPromotion;
	}

	/**
	 * 카드프로모션 비노출처리
	 * @param cardPromotion
	 * @return
	 * @author eskim
	 * @since 2021. 02. 2
	 */
	@Transactional("shopTxnManager")
	public void updateCardPromotionDsipYn(CardPromotion cardPromotion) {
		cardPromotion.setUpdNo(TsaSession.getInfo().getUserNo());
		cardPromotionDao.updateCardPromotionDsipYn(cardPromotion);
	}

	/**
	 * 카드프로모션 삭제
	 * @param cardPromotion
	 * @return
	 * @author eskim
	 * @since 2021. 02. 2
	 */
	@Transactional("shopTxnManager")
	public void deleteCardPromotion(CardPromotion cardPromotion) {

		for(String tmpCardPrmtSq: cardPromotion.getArrCardPrmtSq()) {
			int cardPrmtSq = Integer.parseInt(tmpCardPrmtSq);
			//카드프로모션 조건 조회
			Collection<CardPromotionCondition> cardPrmtCdtList = cardPromotionDao.getCardPromotionConditionList(cardPrmtSq);
			for (CardPromotionCondition cardPromotionCondition : cardPrmtCdtList){
				//카드프로모션 대상 삭제
				cardPromotionDao.deleteCardPromotionTarget(cardPromotionCondition.getCardPrmtCdtSq());
				//카드프로모션 조건 삭제
				cardPromotionDao.deleteCardPromotionCondition(cardPromotionCondition.getCardPrmtCdtSq());
			}
			//카드프로모션삭제
			cardPromotionDao.deleteCardPromotion(cardPrmtSq);
		}
	}

	/**
	 * 카드프로모션 목록 건수
	 * @param cardPromotion
	 * @return
	 * @author eskim
	 * @since 2021. 02. 03
	 */
	public int getCardPromotionListCount(CardPromotion cardPromotion) {
		return cardPromotionDao.getCardPromotionListCount(cardPromotion);
	}

	/**
	 * 카드프로모션 목록
	 * @param cardPromotion
	 * @return
	 * @author eskim
	 * @since 2021. 02. 03
	 */
	public Collection<CardPromotion> getCardPromotionList(CardPromotion cardPromotion) {
		return cardPromotionDao.getCardPromotionList(cardPromotion);
	}

	/**
	 * 카드무이자할부 조회
	 * @param cardPrmtSq
	 * @return
	 * @author eskim
	 * @since 2021. 02. 1
	 */
	public CardPromotion getCardPromotion(Integer cardPrmtSq) {
		CardPromotion cardPrmt = new CardPromotion();
		cardPrmt.setCardPrmtSq(cardPrmtSq);
		cardPrmt.setSearch("1");	// 프로모션id
		cardPrmt.setCondition(Integer.toString(cardPrmtSq));
		Collection<CardPromotion> cardInterestList = cardPromotionDao.getCardPromotionList(cardPrmt);

		CardPromotion cardPromotion = new CardPromotion();
		if (cardInterestList != null && !cardInterestList.isEmpty()) {
			cardPromotion = cardInterestList.iterator().next();
//			cardPromotion.setCardPrmtSq(tmpCardPromotionCondition.getCardPrmtSq());
//			cardPromotion.setPrmtNm(tmpCardPromotionCondition.getPrmtNm());
//			cardPromotion.setPrmtStd(tmpCardPromotionCondition.getPrmtStd());
//			cardPromotion.setPrmtEdd(tmpCardPromotionCondition.getPrmtEdd());
//			cardPromotion.setPrmtGb(tmpCardPromotionCondition.getPrmtGb());
//			cardPromotion.setDcGb(tmpCardPromotionCondition.getDcGb());
//			cardPromotion.setLinkUrl(tmpCardPromotionCondition.getLinkUrl());
//			cardPromotion.setNote(tmpCardPromotionCondition.getNote());
//			cardPromotion.setDispYn(tmpCardPromotionCondition.getDispYn());
		}

		Collection<CardPromotionCondition> cardPrmtCdtList = cardPromotionDao.getCardPromotionConditionList(cardPrmtSq);


		for (CardPromotionCondition cardPromotionCondition: cardPrmtCdtList) {
			String cardCd = "";
			String cardNm = "";
			Collection<CardPromotionTarget> cardPrmtTgtList = cardPromotionDao.getCardPromotionTargetList(cardPromotionCondition.getCardPrmtCdtSq());
			for(CardPromotionTarget cardPromotionTarget : cardPrmtTgtList) {
				cardCd += cardPromotionTarget.getPrmtTargetCd() +"/";
				cardNm += cardPromotionTarget.getPrmtTargetNm() +"/";
			}

			cardPromotionCondition.setArrCardCd(cardCd.substring(0, cardCd.lastIndexOf("/")));
			cardPromotionCondition.setArrCardNm(cardNm.substring(0, cardNm.lastIndexOf("/")));
		}

		cardPromotion.setCardPrmtCdtList(cardPrmtCdtList);

		return cardPromotion;
	}
}
