package com.style24.front.biz.service;

import java.util.Collection;

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

import com.gagaframework.web.parameter.GagaMap;
import com.gagaframework.web.rest.server.GagaResponseStatus;
import com.style24.core.biz.thirdparty.Yes24Giftcard;
import com.style24.front.biz.dao.TsfGiftcardDao;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.domain.GiftCard;
import com.style24.persistence.domain.Order;

import lombok.extern.slf4j.Slf4j;

/**
 * 상품권 Service
 *
 * @author gagamel
 * @since 2020. 12. 29
 */
@Service
@Slf4j
public class TsfGiftcardService {

	@Autowired
	private TsfGiftcardDao giftcardDao;

	@Autowired
	private Yes24Giftcard yes24Gift;

	/**
	 * 상품권정보 조회
	 *
	 * @param GiftCard
	 * @author sowon
	 * @since 2021. 3. 12
	 */
	public GiftCard getGiftcardInfo(GiftCard giftcard) {

		// 고객번호 설정
		int custNo = TsfSession.getInfo().getCustNo();
		giftcard.setCustNo(custNo);

		return giftcardDao.getGiftcardInfo(giftcard);
	}

	/**
	 * 상품권 사용 정보 조회
	 *
	 * @param GiftCard
	 * @author sowon
	 * @since 2021. 3. 12
	 */
	public Collection<GiftCard> getGiftcardUseList(GiftCard giftcard) {
		// 고객번호 설정
		int custNo = TsfSession.getInfo().getCustNo();
		giftcard.setCustNo(custNo);
		giftcard.setMonthLength(giftcard.getMonth().length());

		return giftcardDao.getGiftcardUseList(giftcard);
	}

	/**
	 * 상품권 보유 조회
	 *
	 * @param GiftCard
	 * @author sowon
	 * @since 2021. 3. 12
	 */
	public Collection<GiftCard> getGiftcardOwnList(GiftCard giftcard) {
		// 고객번호 설정
		int custNo = TsfSession.getInfo().getCustNo();
		giftcard.setCustNo(custNo);

		return giftcardDao.getGiftcardOwnList(giftcard);
	}

	/**
	 * 상품권 저장
	 * 
	 * @param GiftCard
	 * @author sowon
	 * @throws Exception
	 * @since 2021. 3. 15
	 */
	@Transactional("shopTxnManager")
	public GagaMap createGiftcardUseConfirm(GiftCard giftcard) throws Exception {
		GagaMap map = new GagaMap();
		GagaMap error = new GagaMap();
		// 고객번호 설정
		int custNo = TsfSession.getInfo().getCustNo();
		String ip = TsfSession.getIpAddress();
		giftcard.setCustNo(custNo);
		giftcard.setRegNo(custNo);
		giftcard.setUpdNo(custNo);
		giftcard.setIpAddr(ip);  // 아이피주소
		
		
		String pubNo = giftcard.getGfcdNo();
		String result =yes24Gift.soapDataTransfer(pubNo,ip,custNo);

		// result message 설정
		map.set("status", GagaResponseStatus.FAIL.getCode());

		if (result.equals("01")) {
			giftcard.setResultVal(result);  // 승인 결과 insert
			giftcardDao.saveGiftcardApi(giftcard);
			// throw new IllegalArgumentException("존재하지 않는 상품권입니다.");
			error.set("message", "존재하지 않는 상품권입니다.");
			map.set("error", error);
		} else if (result.equals("02")) {
			giftcard.setResultVal(result);  
			giftcardDao.saveGiftcardApi(giftcard);
			// throw new IllegalArgumentException("이미 등록된 상품권입니다.");
			error.set("message", "이미 등록된 상품권입니다.");
			map.set("error", error);
		} else if (result.equals("03")) {
			giftcard.setResultVal(result);  
			giftcardDao.saveGiftcardApi(giftcard);
			// throw new IllegalArgumentException("삭제된 상품권입니다.");
			error.set("message", "삭제된 상품권입니다.");
			map.set("error", error);
		} else if (result.equals("04")) {
			giftcard.setResultVal(result);  
			giftcardDao.saveGiftcardApi(giftcard);
			// throw new IllegalArgumentException("기간이 만료된 상품권입니다.");
			error.set("message", "기간이 만료된 상품권입니다.");
			map.set("error", error);
		} else if (result.equals("99")) {
			giftcard.setResultVal(result);  
			giftcardDao.saveGiftcardApi(giftcard);
			// throw new IllegalArgumentException("시스템 오류입니다.");
			error.set("message", "시스템 오류입니다.");
			map.set("error", error);
		} else{
					
			giftcard.setResultVal("00");  
			giftcardDao.saveGiftcardApi(giftcard);
			
			// 성공일 경우 tb_cust_giftcard insert
			String[] param = result.split("[|]");
			// [2021-04-01, 오전, 12:00:00]
			String[] param2 = param[2].split(" ");
			String[] param3 = param[3].split(" ");
			
			giftcard.setChgGfcdAmt(Integer.parseInt(param[1])); // 승인금액
			giftcard.setRmGfcdAmt(Integer.parseInt(param[1])); // 승인금액
			giftcard.setUseStDate(param2[0]);
			giftcard.setUseExpDate(param3[0]);
			giftcardDao.createGiftcardUseConfirm(giftcard);
			
			int custGfcdSq = giftcardDao.getCustGiftCardSq(pubNo);
			giftcard.setCustGfcdSq(custGfcdSq);
			giftcard.setOccurGb("G074_11");
			giftcard.setOccurDtlDesc("상품권등록");
			giftcard.setGfcdAmt(Integer.parseInt(param[1]));
			
			giftcardDao.createCustGiftcardHistory(giftcard);

			map.set("status", GagaResponseStatus.SUCCESS.getCode());
			map.set("message", "상품권이 등록되었습니다.");
		}

		return map;
	}
	
	 /**
	 * 상품권 최근 12개월 년 월
	 *
	 * @param GiftCard
	 * @author sowon
	 * @since 2021. 3. 30
	 */
	 public Collection<GiftCard> getRecentlyDate(){
		 return giftcardDao.getRecentlyDate();
	 }

	/**
	 * 상품권 KCP 현금영수증 신청 정보 조회
	 *
	 * @param GiftCard
	 * @return GiftCard
	 * @author card007
	 * @since 2021. 04. 25
	 */
	public GiftCard getCashReceiptsInfo(GiftCard giftCard) {
		return giftcardDao.getCashReceiptsInfo(giftCard);
	}

	/**
	 * 상품권 KCP 현금영수증 신청 여부 조회
	 *
	 * @param GiftCard
	 * @return GiftCard
	 * @author card007
	 * @since 2021. 04. 25
	 */
	public GiftCard getCashReceiptsRequestInfo(Order order) {
		return giftcardDao.getCashReceiptsRequestInfo(order);
	}
}
