package com.madeu.crm.smsTemplate.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.madeu.common.service.MenuAuthService;
import com.madeu.constants.Constants;
import com.madeu.crm.smsTemplate.dto.SmsTemplateDTO;
import com.madeu.crm.smsTemplate.dto.SmsTemplateSearchDTO;
import com.madeu.crm.smsTemplate.map.SmsTemplateMAP;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SmsTemplateService {

    @Autowired
    private SmsTemplateMAP smsTemplateMAP;

    @Autowired
    private MenuAuthService menuAuthService;

    /**
     * 상용구 관리 화면 이동
     */
    public HashMap<String, Object> moveSmsTemplateList(HashMap<String, Object> paramMap) throws Exception {
        log.debug("moveSmsTemplateList START");

        HashMap<String, Object> map = menuAuthService.getMenuAuthority(paramMap);

        log.debug("loginMemberId : " + paramMap.get("loginMemberId"));
        log.debug("menuClass     : " + paramMap.get("menuClass"));
        log.debug("moveSmsTemplateList END");
        return map;
    }

    /**
     * 상용구 목록 조회
     */
    public HashMap<String, Object> getSmsTemplateList(SmsTemplateSearchDTO searchDTO) throws Exception {
        log.debug("getSmsTemplateList START");

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

        List<SmsTemplateDTO> listMap = new ArrayList<>();

        int totalCount = smsTemplateMAP.getSmsTemplateCnt(searchDTO);

        if (0 < totalCount) {
            listMap = smsTemplateMAP.getSmsTemplateList(searchDTO);
        }

        map.put("msgCode", Constants.OK);
        map.put("success", "true");
        map.put("totalCount", totalCount);
        map.put("rows", listMap);

        log.debug("getSmsTemplateList END");
        return map;
    }

    /**
     * 상용구 상세 조회
     */
    public HashMap<String, Object> getSmsTemplate(SmsTemplateDTO dto) throws Exception {
        log.debug("getSmsTemplate START");

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

        SmsTemplateDTO detail = smsTemplateMAP.getSmsTemplate(dto);

        if (detail != null) {
            map.put("msgCode", Constants.OK);
            map.put("success", "true");
            map.put("data", detail);
        } else {
            map.put("msgCode", Constants.FAIL);
            map.put("success", "false");
            map.put("msgDesc", "상용구 정보를 찾을 수 없습니다.");
        }

        log.debug("getSmsTemplate END");
        return map;
    }

    /**
     * 상용구 등록
     */
    public HashMap<String, Object> putSmsTemplate(SmsTemplateDTO dto) throws Exception {
        log.debug("putSmsTemplate START");

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

        int result = smsTemplateMAP.putSmsTemplate(dto);

        if (0 < result) {
            map.put("msgCode", Constants.OK);
            map.put("success", "true");
            map.put("msgDesc", "상용구가 등록되었습니다.");
        } else {
            map.put("msgCode", Constants.FAIL);
            map.put("success", "false");
            map.put("msgDesc", "상용구 등록에 실패하였습니다.");
        }

        log.debug("putSmsTemplate END");
        return map;
    }

    /**
     * 상용구 수정
     */
    public HashMap<String, Object> modSmsTemplate(SmsTemplateDTO dto) throws Exception {
        log.debug("modSmsTemplate START");

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

        int result = smsTemplateMAP.modSmsTemplate(dto);

        if (0 < result) {
            map.put("msgCode", Constants.OK);
            map.put("success", "true");
            map.put("msgDesc", "상용구가 수정되었습니다.");
        } else {
            map.put("msgCode", Constants.FAIL);
            map.put("success", "false");
            map.put("msgDesc", "상용구 수정에 실패하였습니다.");
        }

        log.debug("modSmsTemplate END");
        return map;
    }

    /**
     * 상용구 삭제 (논리 삭제)
     */
    public HashMap<String, Object> delSmsTemplate(SmsTemplateDTO dto) throws Exception {
        log.debug("delSmsTemplate START");

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

        int result = smsTemplateMAP.delSmsTemplate(dto);

        if (0 < result) {
            map.put("msgCode", Constants.OK);
            map.put("success", "true");
            map.put("msgDesc", "상용구가 삭제되었습니다.");
        } else {
            map.put("msgCode", Constants.FAIL);
            map.put("success", "false");
            map.put("msgDesc", "상용구 삭제에 실패하였습니다.");
        }

        log.debug("delSmsTemplate END");
        return map;
    }
}
