/**
 * date : 2022. 4. 25.
 * auth : ckr
 * desc : 발신번호관리
 */
package kr.co.uplus.ez.api.sendNumMgt;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import kr.co.uplus.ez.api.sendNumMgt.dto.*;
import kr.co.uplus.ez.common.components.ValidComponents;
import kr.co.uplus.ez.common.data.ApiResponseCode;
import kr.co.uplus.ez.common.utils.FileIoUtils;
import kr.co.uplus.ez.common.utils.FileUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;

@RestController
@RequestMapping(value = "api/v1/bo/sendNumMgt")
public class SendNumMgtController {

	private static final Logger logger = LoggerFactory.getLogger(SendNumMgtController.class);

	@Autowired
	SendNumMgtService sendNumService;

	@Autowired
	ValidComponents validComponents;

	private static int BUFFERSIZE = 1024;

	@Value("${file-resource.info.sendNumber.path:/efs/admin/sendNumber/}")
	private String sendNumUploadPath;

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 발신 프로필 목록 조회
	 * @param profileListReqDto
	 */
	@ApiOperation(value = "/profileList", notes = "발신 프로필 목록 조회")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "profileList", method = { RequestMethod.POST })
	@ResponseBody
	public ProfileListResDto profileList(@RequestBody @Valid ProfileListReqDto profileListReqDto, BindingResult bindingResult) {

		if (validComponents.validParameter(bindingResult)) {
			return new ProfileListResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}

		return sendNumService.profileList(profileListReqDto);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 문자 발신 번호 목록 조회
	 * @param sendNumberListReqDto
	 */
	@ApiOperation(value = "/numberList", notes = "문자 발신 번호 목록 조회")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "numberList", method = { RequestMethod.POST })
	@ResponseBody
	public SendNumberListResDto numberList(@RequestBody @Valid SendNumberListReqDto sendNumberListReqDto, BindingResult bindingResult) {

		if (validComponents.validParameter(bindingResult)) {
			return new SendNumberListResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}
		return sendNumService.numberList(sendNumberListReqDto);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 문자 발신 번호 삭제
	 * @param deleteNumberReqDto
	 */
	@ApiOperation(value = "/deleteNumber", notes = "문자 발신 번호 삭제")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "deleteNumber", method = { RequestMethod.POST })
	@ResponseBody
	public DeleteNumberResDto deleteNumber(@RequestBody @Valid DeleteNumberReqDto deleteNumberReqDto, BindingResult bindingResult) {
		if (validComponents.validParameter(bindingResult)) {
			return new DeleteNumberResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}

		return sendNumService.deleteNumber(deleteNumberReqDto);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 문자 발신 번호 등록
	 * @param insertNumberReqDto
	 */
	@ApiOperation(value = "/insertNumber", notes = "문자 발신 번호 등록")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "insertNumber", method = { RequestMethod.POST })
	@ResponseBody
	public InsertNumberResDto insertNumber(@RequestPart(value = "key") InsertNumberReqDto insertNumberReqDto, MultipartHttpServletRequest multipartRequest) {
		return sendNumService.insertNumber(insertNumberReqDto, multipartRequest);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 문자 발신 번호 정보 상세 조회
	 * @param detailNumberReqDto
	 */
	@ApiOperation(value = "/numberDetail", notes = "문자 발신 번호 정보 상세 조회")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "numberDetail", method = { RequestMethod.POST })
	@ResponseBody
	public DetailNumberResDto numberDetail(@RequestBody @Valid DetailNumberReqDto detailNumberReqDto, BindingResult bindingResult) {

		if (validComponents.validParameter(bindingResult)) {
			return new DetailNumberResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}

		return sendNumService.numberDetail(detailNumberReqDto);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 관리자ID 목록 조회
	 * @param adminListReqDto
	 */
	@ApiOperation(value = "/adminList", notes = "관리자ID 목록 조회")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "adminList", method = { RequestMethod.POST })
	@ResponseBody
	public SendAdminListResDto adminList(@RequestBody @Valid SendAdminListReqDto adminListReqDto, BindingResult bindingResult) {

		if (validComponents.validParameter(bindingResult)) {
			return new SendAdminListResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}

		return sendNumService.adminList(adminListReqDto);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 발신번호 승인 목록 조회
	 * @param sendNumberApprListReqDto
	 */
	@ApiOperation(value = "/apprList", notes = "발신번호 승인 목록 조회")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "/apprList" , method = {RequestMethod.POST})
	@ResponseBody
	public SendNumberApprListResDto apprList(@RequestBody @Valid SendNumberApprListReqDto sendNumberApprListReqDto, BindingResult bindingResult) {

		if (validComponents.validParameter(bindingResult)) {
			return new SendNumberApprListResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}

		return sendNumService.apprList(sendNumberApprListReqDto);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 발신번호 승인 상세 정보 조회
	 * @param sendNumberApprDetailReqDto
	 */
	@ApiOperation(value = "/apprDetail", notes = "발신번호 승인 상세 정보 조회")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "/apprDetail" , method = {RequestMethod.POST})
	@ResponseBody
	public SendNumberApprDetailResDto apprDetail(@RequestBody @Valid SendNumberApprDetailReqDto sendNumberApprDetailReqDto, BindingResult bindingResult) {

		if (validComponents.validParameter(bindingResult)) {
			return new SendNumberApprDetailResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}

		return sendNumService.apprDetail(sendNumberApprDetailReqDto);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 발신번호 승인내역 수정
	 * @param updateApprReqDto
	 */
	@ApiOperation(value = "/updateAppr", notes = "발신번호 승인내역 수정")
	@ApiResponses({ @ApiResponse(code = HttpServletResponse.SC_OK, message = "SUCESS") })
	@RequestMapping(value = "/updateAppr" , method = {RequestMethod.POST})
	@ResponseBody
	public UpdateApprResDto updateAppr(@RequestBody @Valid UpdateApprReqDto updateApprReqDto, BindingResult bindingResult) {

		if (validComponents.validParameter(bindingResult)) {
			return new UpdateApprResDto(ApiResponseCode.CM_PARAMETER_ERROR);
		}

		return sendNumService.updateAppr(updateApprReqDto);
	}

	/**
	 * 파일 다운로드
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/filedownload")
	public void filedownload(@RequestBody LinkedHashMap param, HttpServletRequest request, HttpServletResponse response) throws IOException {
		logger.debug(param.toString());

		File file = new File(param.get("filePath") + File.separator + param.get("fileNm"));
		if (file.exists()) {
			FileIoUtils.fileDownload(file, request, response );
		}
	}

}
