package com.takensoft.cms.bbs.web;

import com.takensoft.cms.bbs.service.BbsCnService;
import com.takensoft.cms.bbs.service.BbsMngService;
import com.takensoft.cms.bbs.service.BbsTypeMngService;
import com.takensoft.cms.bbs.vo.BbsMngVO;
import com.takensoft.cms.bbs.vo.BbsTypeMngVO;
import com.takensoft.cms.codeManage.service.CodeManageService;
import com.takensoft.cms.codeManage.vo.CodeManageVO;
import com.takensoft.common.HierachyVO;
import com.takensoft.common.util.ResponseData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.*;

/**
 * @author  : 박정하
 * @since   : 2024.05.08
 *
 * 게시판 관리 관련 컨트롤러
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping(value="/admin/bbsMng")
public class BbsMngController {

    private final BbsMngService bbsMngService;
    private final BbsTypeMngService bbsTypeMngService;
    private final CodeManageService codeManageService;

    /**
     * @author  하석형
     * @since   2024.05.10
     * @param   bbsMngVO
     * @return
     * @throws  Exception
     *
     * 게시판 관리 등록
     */
    @PostMapping("/saveProc.json")
    public ResponseEntity<?> saveProc(@RequestBody BbsMngVO bbsMngVO) throws Exception {
        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        // 게시판 관리 아이디 중복 검사 (아이디 입력 삭제로 미사용)
        /*boolean isExistence = bbsMngService.bbsMngIdCheck(bbsMngVO);
        if(isExistence) {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("이미 존재하는 게시판 아이디입니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }*/

        // 게시판 관리 등록
        HashMap<String, Object> result = bbsMngService.saveBbsMng(bbsMngVO);
        int insertResult = (int) result.get("result");
        if(insertResult > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 등록 처리되었습니다.");
            responseData.setData(result);
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("등록에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author  하석형
     * @since   2024.05.10
     * @param   params
     * @return
     * @throws  Exception
     *
     * 게시판 관리 목록 조회
     */
    @PostMapping("/findAll.json")
    public ResponseEntity<?> findAll(@RequestBody HashMap<String, String> params) throws Exception {
        // 게시판 관리 목록 조회
        Map<String, Object> result = bbsMngService.findAllBbsMng(params);

        // 응답처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author  박정하
     * @since   2024.05.16
     * @param
     * @return
     * @throws  Exception
     *
     * 게시판 관리 목록 조회 (메뉴 관리용)
     */
    @PostMapping("/findAllByMenuMng.json")
    public ResponseEntity<?> findAllByMenuMng() throws Exception {
        // 게시판 관리 목록 조회
        List<BbsMngVO> result = bbsMngService.findAllByMenuMng();

        // 응답처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 하석형
     * @since  2024.05.08
     * @param  bbsMngVO
     * @return
     * @throws Exception
     *
     * 게시판 관리 상세 조회
     */
    @PostMapping("/findByBbsMng.json")
    public ResponseEntity<?> findByBbsMng(@RequestBody BbsMngVO bbsMngVO) throws Exception {
        // 게시판 관리 상세 조회
        BbsMngVO bbsMng = bbsMngService.findByBbsMngId(bbsMngVO.getBbsMngId());

        // 게시판 유형 목록 조회
        BbsTypeMngVO bbsTypeMap = new BbsTypeMngVO();
        bbsTypeMap.setExpsrYn("Y");
        List<BbsTypeMngVO> bbsType = (List<BbsTypeMngVO>) bbsTypeMngService.findAllBbsTypeMng(bbsTypeMap).get("list");
        // 페이지 유형 목록 조회
        List<CodeManageVO> pageType = codeManageService.findByChildCdCache("pageType");

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("bbsMng", bbsMng);
        result.put("bbsTypeList", bbsType);
        result.put("pageTypeList", pageType);

        // 응답 처리
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();
        responseData.setStatus(HttpStatus.OK);
        responseData.setMessage("정상적으로 조회가 처리되었습니다.");
        responseData.setData(result);
        return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
    }

    /**
     * @author 하석형
     * @since  2024.05.09
     * @param  bbsMngVO
     * @return
     * @throws Exception
     *
     * 게시판 관리 수정
     */
    @PostMapping("/updateProc.json")
    public ResponseEntity<?> updateProc(@RequestBody BbsMngVO bbsMngVO) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        // 게시판 관리 수정
        int result = bbsMngService.updateBbsMng(bbsMngVO);

        // 응답 처리
        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 수정 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("수정에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * @author 하석형
     * @since  2024.05.09
     * @param  bbsMngVO
     * @return
     * @throws Exception
     *
     * 게시판 관리 삭제
     */
    @PostMapping("/deleteProc.json")
    public ResponseEntity<?> deleteProc(@RequestBody BbsMngVO bbsMngVO) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
        ResponseData responseData = new ResponseData();

        // 게시판 관리 수정
        int result = bbsMngService.deleteBbsMng(bbsMngVO);

        // 응답 처리
        if(result > 0) {
            responseData.setStatus(HttpStatus.OK);
            responseData.setMessage("정상적으로 삭제 처리되었습니다.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        } else {
            responseData.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
            responseData.setMessage("삭제에 실패하였습니다.\n담당자에게 문의하세요.");
            return new ResponseEntity<>(responseData, headers, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}