package com.madeu.batch;

import com.madeu.dao.web.webpush.WebPushSqlMapDAO;
import com.madeu.push.FCMProvider;
import com.madeu.push.FCMProvider2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class PushMarketingSendBatch {

	@Autowired
	private WebPushSqlMapDAO webPushSqlMapDAO;

//	@Scheduled(fixedDelay = 600000) // 10분마다
//	@Scheduled(fixedDelay = 60000) // 1분마다
//	@Scheduled(fixedDelay = 3600000) // 1시간마다
	public void go() {

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

		try {
			LocalTime now = LocalTime.now();
			int hour = now.getHour();

			// 오전 9시 ~ 오후 9시 전송
			if(9 < hour && hour < 21) {
				// 푸시 전송할 대상자 조회 (group)
				HashMap<String, Object> selectMap = new HashMap<String, Object>();
				selectMap.put("state","R");
				selectMap.put("pushType","MKT");
				List<Map<String, Object>> pushList = webPushSqlMapDAO.selectListPushTimeGroup(selectMap);
				int pushListSize = pushList.size();

				List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();

				if(0<pushListSize){
					String title   = String.valueOf(pushList.get(0).get("title"));
					String content = String.valueOf(pushList.get(0).get("content"));
					String linkUrl = String.valueOf(pushList.get(0).get("linkUrl"));
					String tId     = String.valueOf(pushList.get(0).get("tId"));

					selectMap.put("title",title);
					selectMap.put("content",content);
					selectMap.put("linkUrl",linkUrl);
					selectMap.put("tId",tId);

					HashMap<String, Object>  messageParamMap = new HashMap<String, Object>();
					messageParamMap.put("linkUrl",linkUrl);

					List<Map<String, Object>> pushList2 = webPushSqlMapDAO.selectListPushTime(selectMap);
					int pushList2Size = pushList2.size();

					if(0<pushList2Size){
						for(int i=0; i<pushList2Size; i++){
							// R -> G
							String muPushId = String.valueOf(pushList2.get(i).get("muPushId"));

							// 상태를 변경함. pushYn : G(진행중)
							HashMap<String, Object> sendPushParamMap = new HashMap<String, Object>();
							sendPushParamMap.put("muPushId", muPushId);
							sendPushParamMap.put("oldState", "R");
							sendPushParamMap.put("chgState", "G");
							sendPushParamMap.put("modId", "BatchSystem");
							sendPushParamMap.put("sendDateYn", "N");
							webPushSqlMapDAO.updatePushState(sendPushParamMap);

							String phoneType       = String.valueOf(pushList2.get(i).get("phoneType"));
							String pushKey         = String.valueOf(pushList2.get(i).get("pushKey"));

							HashMap<String, Object>  userParamMap = new HashMap<String, Object>();
							userParamMap.put("pushKey", pushKey);
							userParamMap.put("phoneType", phoneType);

							// pushKey, phoneType 리스트에 담음
							userList.add(userParamMap);

							// 푸시 발송
							FCMProvider fcmProvider = new FCMProvider();
							fcmProvider.provider(phoneType, pushKey, title, content, messageParamMap);

							// G -> Y
							// 상태를 변경함. pushYn : Y(전송완료)
							sendPushParamMap = new HashMap<String, Object>();
							sendPushParamMap.put("muPushId", muPushId);
							sendPushParamMap.put("oldState", "G");
							sendPushParamMap.put("chgState", "Y");
							sendPushParamMap.put("modId", "BatchSystem");
							sendPushParamMap.put("sendDateYn", "Y");
							webPushSqlMapDAO.updatePushState(sendPushParamMap);
						}
					}
				}
			}

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

	}

}
