package com.madeu.batch;

import com.madeu.dao.web.webkakao.WebKakaoSqlMapDAO;
import com.popbill.api.KakaoService;
import com.popbill.api.kakao.KakaoSentDetail;
import com.popbill.api.kakao.KakaoSentInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class KakaoPopbillStateUpdateBatch {

    private static String corpNum;
    private static String userId;

    @Value("${popbill.corp-num}")
    public void setCorpNum(String value){
        corpNum = value;
    }

    @Value("${popbill.user-id}")
    public void setUserId(String value){
        userId = value;
    }

    @Autowired
    private WebKakaoSqlMapDAO webKakaoSqlMapDAO;
    
    @Autowired
    private KakaoService kakaoService;

    //https://blog.naver.com/lovemema/140200056062
//    @Scheduled(cron = "30 0/5 * * * *") // 5분마다 실행 (30초에 실행)
    public void go() {
        log.debug("******************KakaoPopbillStateUpdateBatch START **********************************");

		try{
			log.debug("POPBILL_STATE, POPBILL_RESULT UPDATE START");

			// KAKAO 환경설정 정보 호출
			// 팝빌회원 사업자번호
			// 팝빌회원 아이디

			log.debug("corpNum : "+corpNum);
			log.debug("userId  : "+userId);

			// 접수번호 대상자 조회
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("popbillState", 0);

			List<Map<String, Object>> kakaoListMap = webKakaoSqlMapDAO.selectListPopbillState(paramMap);
			int kakaoListMapSize = kakaoListMap.size();

			log.debug("접수번호 갯수 : "+kakaoListMapSize);

			for(int i=0; i<kakaoListMapSize; i++) {

				// 접수번호
				String receiptNumber = String.valueOf(kakaoListMap.get(i).get("receiptNumber"));

				KakaoSentInfo sentInfos = kakaoService.getMessages(corpNum, receiptNumber);
				String templateCode = sentInfos.getTemplateCode();
				log.debug("templateCode : "+templateCode);

				// 전송갯수
				int sentInfosLength = Integer.parseInt(String.valueOf(sentInfos.getSendCnt()));

				for(int j=0; j<sentInfosLength; j++) {

					List<KakaoSentDetail> kakaoSentDetail = sentInfos.getMsgs();

					String receiveNumber = kakaoSentDetail.get(j).getReceiveNum();
					String receiveName	 = kakaoSentDetail.get(j).getReceiveName();
					String state		 = String.valueOf(kakaoSentDetail.get(j).getState());
					String result		 = String.valueOf(kakaoSentDetail.get(j).getResult());

					if(("3").equals(state)) {

						HashMap<String, Object> kakaoParamMap = new HashMap<String, Object>();

						kakaoParamMap.put("receiptNumber", receiptNumber);
						kakaoParamMap.put("receiveNumber", receiveNumber);
						kakaoParamMap.put("receiveName", receiveName);
						kakaoParamMap.put("popbillState", 0);
						kakaoParamMap.put("state", state);
						kakaoParamMap.put("result", result);
						kakaoParamMap.put("modId", "BatchSystem");
						kakaoParamMap.put("templateCode", templateCode);

						webKakaoSqlMapDAO.updateKakaoPopbillState(kakaoParamMap);

					}else {

						/*log.debug("receiptNumber                : "+receiptNumber);
						log.debug("sentInfos["+j+"] receiveNum   : "+receiveNumber);
						log.debug("sentInfos["+j+"] receiveName  : "+receiveName);
						log.debug("sentInfos["+j+"] state        : "+state);
						log.debug("sentInfos["+j+"] result       : "+result);*/
					}
				}
			}


			log.debug("POPBILL_STATE, POPBILL_RESULT UPDATE END");

		}catch(Exception e){
			log.debug("Exception KakaoPopbillStateUpdateBatch - run" + e.getMessage());
		}

        log.debug("******************KakaoPopbillStateUpdateBatch END **********************************");
    }
}
