package com.madeu.service.common.autopush.impl;

import com.madeu.constants.Constants;
import com.madeu.dao.web.webpush.WebPushSqlMapDAO;
import com.madeu.push.FCMProvider;
import com.madeu.service.common.autopush.AutoPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Slf4j
@Service("PushService")
public class AutoPushServiceImpl implements AutoPushService {
	
	@Autowired
	private WebPushSqlMapDAO webPushSqlMapDAO;

	/**
	 * 푸시 전송
	 * 
	 * @param insertPushMap
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
	public HashMap<String, Object> send(
			String type, HashMap<String, Object> insertPushMap) throws Exception{
		log.debug("PushServiceImpl send START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		try{
			// R 등록
			webPushSqlMapDAO.insertPush(insertPushMap);
			insertPushMap.put("muPushId", insertPushMap.get("id"));
			insertPushMap.put("regId", "SYSTEM");
			insertPushMap.put("modId", "SYSTEM");


			// R -> G 변경
			HashMap<String, Object> sendPushParamMap = new HashMap<String, Object>();
			sendPushParamMap.put("muPushId", insertPushMap.get("muPushId"));
			sendPushParamMap.put("oldState", "R");
			sendPushParamMap.put("chgState", "G");
			sendPushParamMap.put("modId", "SYSTEM");
			sendPushParamMap.put("sendDateYn", "N");
			webPushSqlMapDAO.updatePushState(sendPushParamMap);


			// 전송
			HashMap<String, Object>  messageParamMap = new HashMap<String, Object>();
			if(("reserve").equals(type)){
				messageParamMap.put("linkUrl", "http://nrm.madeu.co.kr/reservation");
			}else if(("membershipUse").equals(type)){
				messageParamMap.put("linkUrl", "http://nrm.madeu.co.kr/mobilemembership/selectListMembershipUseIntro.do");
			}else if(("membershipCharge").equals(type)){
				messageParamMap.put("linkUrl", "http://nrm.madeu.co.kr/mobilemembership/selectListMembershipChargeIntro.do");
			}else{
				messageParamMap.put("linkUrl", null);
			}


			String phoneType = String.valueOf(insertPushMap.get("phoneType"));
			String pushKey   = String.valueOf(insertPushMap.get("pushKey"));
			String title     = String.valueOf(insertPushMap.get("title"));
			String content   = String.valueOf(insertPushMap.get("content"));

			FCMProvider fcmProvider = new FCMProvider();
			fcmProvider.provider(phoneType, pushKey, title, content, messageParamMap);


			// G -> Y 변경
			sendPushParamMap = new HashMap<String, Object>();
			sendPushParamMap.put("muPushId", insertPushMap.get("muPushId"));
			sendPushParamMap.put("oldState", "G");
			sendPushParamMap.put("chgState", "Y");
			sendPushParamMap.put("modId", "SYSTEM");
			sendPushParamMap.put("sendDateYn", "Y");
			webPushSqlMapDAO.updatePushState(sendPushParamMap);

		}catch(Exception e){
			map.put("msgCode", Constants.FAIL);
			map.put("msgDesc", map.get("msgDesc"));
			throw new Exception(e.getMessage());
		}
		
		log.debug("PushServiceImpl send END");
		return map;
	}
}
