package kr.xit.ens.ktgbs.web;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.core.type.*;
import com.fasterxml.jackson.databind.*;

import io.swagger.v3.oas.annotations.*;
import io.swagger.v3.oas.annotations.media.*;
import io.swagger.v3.oas.annotations.tags.*;
import kr.xit.biz.ens.model.kt.*;
import kr.xit.biz.ens.model.ktgbs.*;
import kr.xit.biz.ktgbs.service.*;
import kr.xit.core.exception.*;
import kr.xit.core.support.utils.*;
import lombok.*;

/**
 * <pre>
 * description : KT GBIS 에서 사용 되는 API
 * packageName : kr.xit.ens.ktgbs.web
 * fileName    : KtGbsMmsInboundController
 * author      : limju
 * date        : 2024-08-19
 * ======================================================================
 * 변경일         변경자        변경 내용
 * ----------------------------------------------------------------------
 * 2024-08-19   limju       최초 생성
 *
 * </pre>
 */
@Tag(name = "KtGbsInboundController", description = "KT GIBIS Inbound API - KT GIBIS 에서 사용하는 API- 천안 can")
@RequiredArgsConstructor
@RestController
public class KtGbsInboundController {
    private final IBizKtGbsService bizService;

    /**
     * <pre>
     * 메세지 발송/수신 결과 전송 -> KT GIBIS에서 호출
     * - KT GBIS 에서 호출 되는 서비스
     * - url 고정 : http://{각대행사(IP:Port)}/api/msg/result
     * @param reqDTO KtMsgRsltRequest
     * @return KtCommonResponse
     * </pre>
     */
    /*
    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
        @Content(
            mediaType = "application/json",
            examples = {
                @ExampleObject(
                    value = """
                        {
                            "reqs":[
                                {
                                "src_key":"44133110005571110KT",
                                "mms_sndg_rslt_sqno":2,
                                "prcs_dt":"20241213",
                                "mms_bsns_dvcd":"01001",
                                "mbl_bzowr_dvcd":"02",
                                "mms_rslt_dvcd":"40",
                                "mms_sndg_tmst":"20241213091014",
                                "msg_type":2,
                                "rl_mms_sndg_telno":"6017",
                                "mms_rcv_tmst":"20241213091016",
                                "mms_rdg_tmst":"20241213105733",
                                "prev_approve_yn":"Y",
                                "ffnlgCode":"11"
                                }
                            ],
                            "service_cd":"CSO01"
                        }
                        """
                ),
            })
    })
     */
    @Operation(summary = "메세지 발송/수신 결과 전송 -> KT GIBIS에서 호출", description = "메세지 발송/수신 결과 전송 -> KT GIBIS에서 호출")
    @PostMapping(value = "/goji/api/msg/result", produces = MediaType.APPLICATION_JSON_VALUE)
    public KtCommonDTO.KtCommonResponse messageResult(@RequestBody final KtGbsDTO.MsgRsltRequest reqDTO) {
        return bizService.messageResult(reqDTO);
    }

    @Operation(summary = "링크이미지 출력")
    @GetMapping(value = "/goji/sys/util/lnk/img")
    public void printImg(HttpServletRequest request, HttpServletResponse response, @RequestParam String link, @RequestParam String type) throws IOException {

        String mime = null;
        if ("jpg".equals(type) || "jpeg".equals(type))
            mime = "image/jpeg";
        else if ("png".equals(type))
            mime = "image/png";
        else if ("gif".equals(type))
            mime = "image/gif";
        else
            mime = "image/jpeg";


        try (InputStream inputStream = new URL(link).openStream();
             ServletOutputStream outputStream = response.getOutputStream();) {

            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType(mime);


            int length;
            byte[] buffer = new byte[1024];
            while ((length = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }


            outputStream.flush();

        } catch (FileNotFoundException e) {
            response.getWriter().println("File is Not Found." + e.getMessage());
        } catch (IOException e) {
            response.getWriter().println("I/O Error Occurred!!. " + e.getMessage());
        }
    }
}