package com.madeu.service.mobile.mobilepush.impl;

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobilepush.MobilePushSqlMapDAO;
import com.madeu.service.mobile.mobilepush.MobilePushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service("MobilePushService")
public class MobilePushServiceImpl implements MobilePushService {

	@Autowired
	private MobilePushSqlMapDAO mobilePushSqlMapDAO;

	/**
	 * 앱 푸시 리스트 조회
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListPush(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("MobilePushServiceImpl selectListPush START");

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

		log.debug("muUserId       :"+paramMap.get("muUserId"));
		log.debug("deviceUuid     :"+paramMap.get("deviceUuid"));
		log.debug("phoneType      :"+paramMap.get("phoneType"));
		log.debug("modelName      :"+paramMap.get("modelName"));
		log.debug("osVersion      :"+paramMap.get("osVersion"));
		log.debug("pushKey        :"+paramMap.get("pushKey"));
		log.debug("appVersion     :"+paramMap.get("appVersion"));
		log.debug("deviceYn       :"+paramMap.get("deviceYn"));

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

		try{
			boolean check  = true;
			
			if(true == check){

				List<Map<String, Object>> totalCountListMap = mobilePushSqlMapDAO.selectTotalPushCount(paramMap);
				int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

				if(0 < totalCount) {
					// 리스트 조회
					listMap = mobilePushSqlMapDAO.selectListPush(paramMap);
				}

				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("totalCount", totalCount);
				map.put("rows",listMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		log.debug("MobilePushServiceImpl selectListPush END");
		return map;
	}

	/**
	 * 앱 푸시 상세 조회
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectPush(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("MobilePushServiceImpl selectPush START");

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

		log.debug("muPushId       :"+paramMap.get("muPushId"));

		try{
			boolean check  = true;

			if(true == check){

				List<Map<String, Object>> listMap = mobilePushSqlMapDAO.selectPush(paramMap);

				// 사용자 확인 여부 업데이트
				paramMap.put("userCheckYn", "Y");
				mobilePushSqlMapDAO.updatePushUserCheckYn(paramMap);

				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		log.debug("MobilePushServiceImpl selectPush END");
		return map;
	}

	/**
	 * 푸시 알림 갯수 체크
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectPushCountCheck(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("MobilePushServiceImpl selectPushCountCheck START");

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

		log.debug("muPushId       :"+paramMap.get("muPushId"));
		log.debug("deviceUuid     :"+paramMap.get("deviceUuid"));

		try{
			boolean check  = true;

			if(true == check){

				List<Map<String, Object>> listMap = mobilePushSqlMapDAO.selectPushCountCheck(paramMap);

				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		log.debug("MobilePushServiceImpl selectPushCountCheck END");
		return map;
	}
}
