package com.madeu.batch;

import com.madeu.dao.web.webdevice.WebDeviceSqlMapDAO;
import com.madeu.dao.web.webkakaotemplate.WebKakaoTemplateSqlMapDAO;
import com.madeu.dao.web.webpush.WebPushSqlMapDAO;
import com.madeu.dao.web.webreserve.WebReserveSqlMapDAO;
import com.madeu.service.common.autokakao.AutoKakaoService;
import com.madeu.service.common.autopush.AutoPushService;
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.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class TodayReservePushAutoSendBatch {

	@Autowired
	private WebPushSqlMapDAO webPushSqlMapDAO;

	@Autowired
	private WebReserveSqlMapDAO	webReserveSqlMapDAO;

	@Autowired
	private AutoPushService autoPushService;

	@Autowired
	private AutoKakaoService autoKakaoService;

	@Value("${popbill.sender-phone-num}")
	String senderPhoneNumber;

	@Autowired
	private WebKakaoTemplateSqlMapDAO webKakaoTemplateSqlMapDAO;

	@Autowired
	private WebDeviceSqlMapDAO webDeviceSqlMapDAO;

//    @Scheduled(fixedDelay = 60000) // 1분마다
//    @Scheduled(cron = "0 0 9 * * *") // 매일 9시
	public void go() {

		log.debug("TodayReservePushAutoSendBatch - run start");

		try {
			String tId = String.valueOf(System.currentTimeMillis());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c1 = Calendar.getInstance();
			String tDate = sdf.format(c1.getTime());
			
			HashMap<String, Object> selectMap = new HashMap<String, Object>();

			// 당일 예약자 조회
			LocalDate today = LocalDate.now();
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			selectMap.put("searchDate", today.format(dateTimeFormatter));

			List<Map<String, Object>> reserveListMap = webReserveSqlMapDAO.selectListReserveTodayBatch(selectMap);
			int reserveListSize = reserveListMap.size();

			if(0<reserveListSize){
				// 자동발송 (알림톡 + 푸시)
				// 템플릿 조회
				HashMap<String, Object> insertKakaoMap = new HashMap<String, Object>();
				insertKakaoMap.put("templateCode", "024110000460");
				List<Map<String, Object>> templateListMap = webKakaoTemplateSqlMapDAO.selectKakaoTemplate(insertKakaoMap);


				// 고객명, 예약날짜, 예약시간, 시술명
				String KakaoTemplateTitle = String.valueOf(templateListMap.get(0).get("title"));
				String KakaoTemplateContent = String.valueOf(templateListMap.get(0).get("content"));

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

					String muUserId = String.valueOf(reserveListMap.get(i).get("muUserId"));
					String userName = String.valueOf(reserveListMap.get(i).get("userName"));
					String phoneNumber = String.valueOf(reserveListMap.get(i).get("phoneNumber"));
					String reserveDate = String.valueOf(reserveListMap.get(i).get("reserveDate"));
					String reserveTime = String.valueOf(reserveListMap.get(i).get("reserveTime"));
					String treatmentName = String.valueOf(reserveListMap.get(i).get("treatmentName"));
					String treatmentProcedureName = String.valueOf(reserveListMap.get(i).get("treatmentProcedureName"));

					// 자동발송 (알림톡 + 푸시)
					// 고객명, 예약날짜, 예약시간, 시술명
					String kakaoContent = KakaoTemplateContent;
					kakaoContent = kakaoContent.replace("#{고객명}", userName);
					kakaoContent = kakaoContent.replace("#{예약날짜}", reserveDate);
					kakaoContent = kakaoContent.replace("#{예약시간}", reserveTime);
					kakaoContent = kakaoContent.replace("#{시술명}", treatmentName.concat("-").concat(treatmentProcedureName));


					insertKakaoMap.put("muKakaoId", ("K").concat(String.valueOf(System.currentTimeMillis())));
					insertKakaoMap.put("muUserId", muUserId);
					insertKakaoMap.put("userName", userName);
					insertKakaoMap.put("phoneNumber", phoneNumber);
					insertKakaoMap.put("content", kakaoContent);
					insertKakaoMap.put("state", "R");
					insertKakaoMap.put("popbillState", "0");
					insertKakaoMap.put("sender", senderPhoneNumber);
					insertKakaoMap.put("regId", "SYSTEM");
					insertKakaoMap.put("modId", "SYSTEM");
					insertKakaoMap.put("tId", tId);
					insertKakaoMap.put("tDate", tDate);

					autoKakaoService.send("reserve", insertKakaoMap);


					// 사용자별 디바이스 조회
					selectMap = new HashMap<String, Object>();
					selectMap.put("muUserId", muUserId);
					List<Map<String, Object>> deviceListMap = webDeviceSqlMapDAO.selectListDevice2(selectMap);
					int deviceListSize = deviceListMap.size();

					if(0<deviceListSize){
						HashMap<String, Object> insertPushMap = new HashMap<String, Object>();
						for(int j=0; j<deviceListSize; j++){
							insertPushMap.put("muPushId", ("P").concat(String.valueOf(System.currentTimeMillis())));
							insertPushMap.put("muUserId", muUserId);
							insertPushMap.put("userName", userName);
							insertPushMap.put("muDeviceId", deviceListMap.get(j).get("muDeviceId"));
							insertPushMap.put("deviceUuid", deviceListMap.get(j).get("deviceUuid"));
							insertPushMap.put("pushKey", deviceListMap.get(j).get("pushKey"));
							insertPushMap.put("pushType", "A");
							insertPushMap.put("phoneType", deviceListMap.get(j).get("phoneType"));
							insertPushMap.put("title", KakaoTemplateTitle);
							insertPushMap.put("content", kakaoContent);
							insertPushMap.put("state", "R");
							insertPushMap.put("regId", "SYSTEM");
							insertPushMap.put("modId", "SYSTEM");
							insertPushMap.put("tId", tId);
							insertPushMap.put("tDate", tDate);

							autoPushService.send("reserve", insertPushMap);
						}
					}
				}
			}

		}catch(Exception e){
			e.printStackTrace();
			log.error("Exception TodayReservePushAutoSendBatch - run", e);
		}
		log.debug("TodayReservePushAutoSendBatch - run end");

	}

}
