package com.madeu.crm.smsTemplate.ctrl;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.madeu.constants.Constants;
import com.madeu.crm.smsTemplate.dto.SmsTemplateDTO;
import com.madeu.crm.smsTemplate.dto.SmsTemplateSearchDTO;
import com.madeu.crm.smsTemplate.service.SmsTemplateService;
import com.madeu.util.HttpUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/smsTemplate")
public class SmsTemplateController {

    @Autowired
    private SmsTemplateService smsTemplateService;

    /**
     * 문자 상용구 관리 화면으로 이동
     */
    @RequestMapping("/moveSmsTemplateList.do")
    public String moveSmsTemplateList(HttpSession session, HttpServletRequest request, HttpServletResponse response,
            Model model) {
        log.debug("SmsTemplateController moveSmsTemplateList START");

        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = null;

        try {
            paramMap.put("loginMemberId", String.valueOf(session.getAttribute("loginMemberId")));
            map = smsTemplateService.moveSmsTemplateList(paramMap);
            model.addAttribute("selectUseYn", map.get("selectUseYn"));
            model.addAttribute("insertUseYn", map.get("insertUseYn"));
            model.addAttribute("updateUseYn", map.get("updateUseYn"));
            model.addAttribute("deleteUseYn", map.get("deleteUseYn"));
            model.addAttribute("downloadUseYn", map.get("downloadUseYn"));
        } catch (Exception e) {
            e.printStackTrace();
            return "/web/login/logout";
        }
        log.debug("SmsTemplateController moveSmsTemplateList END");

        return "/web/smsTemplate/smsTemplateSelectList";
    }

    /**
     * 문자 상용구 목록 조회
     */
    @PostMapping("/getSmsTemplateList.do")
    public ModelAndView getSmsTemplateList(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, SmsTemplateSearchDTO searchDTO) {
        log.debug("SmsTemplateController getSmsTemplateList START");

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

        try {
            searchDTO.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
            map = smsTemplateService.getSmsTemplateList(searchDTO);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "서버 오류가 발생했습니다.");
        } finally {
            if (Constants.OK != map.get("msgCode")) {
                map.put("msgCode", Constants.FAIL);
                map.put("success", false);
                if (null == map.get("msgDesc") || ("").equals(map.get("msgDesc"))) {
                    map.put("msgDesc", "정상적으로 수행되지 않았습니다. 관리자에게 문의하시기 바랍니다.");
                }
            }
        }
        log.debug("SmsTemplateController getSmsTemplateList END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 문자 상용구 상세 조회
     */
    @PostMapping("/getSmsTemplate.do")
    public ModelAndView getSmsTemplate(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, SmsTemplateDTO dto) {
        log.debug("SmsTemplateController getSmsTemplate START");

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

        try {
            dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
            map = smsTemplateService.getSmsTemplate(dto);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "조회 중 오류가 발생하였습니다.");
        }
        log.debug("SmsTemplateController getSmsTemplate END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 문자 상용구 등록
     */
    @PostMapping("/putSmsTemplate.do")
    public ModelAndView putSmsTemplate(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, SmsTemplateDTO dto) {
        log.debug("SmsTemplateController putSmsTemplate START");

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

        try {
            dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
            map = smsTemplateService.putSmsTemplate(dto);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "등록 중 오류가 발생하였습니다.");
        }
        log.debug("SmsTemplateController putSmsTemplate END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 문자 상용구 수정
     */
    @PostMapping("/modSmsTemplate.do")
    public ModelAndView modSmsTemplate(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, SmsTemplateDTO dto) {
        log.debug("SmsTemplateController modSmsTemplate START");

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

        try {
            dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
            map = smsTemplateService.modSmsTemplate(dto);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "수정 중 오류가 발생하였습니다.");
        }
        log.debug("SmsTemplateController modSmsTemplate END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }

    /**
     * 문자 상용구 삭제
     */
    @PostMapping("/delSmsTemplate.do")
    public ModelAndView delSmsTemplate(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, SmsTemplateDTO dto) {
        log.debug("SmsTemplateController delSmsTemplate START");

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

        try {
            dto.setLoginMemberId(String.valueOf(session.getAttribute("loginMemberId")));
            map = smsTemplateService.delSmsTemplate(dto);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "삭제 중 오류가 발생하였습니다.");
        }
        log.debug("SmsTemplateController delSmsTemplate END");
        return HttpUtil.makeHashToJsonModelAndView(map);
    }
}
