package cokr.xit.ens.modules.ktsigntalk.direct.presentation;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.EnsException;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.core.utils.CmmnUtil;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.SendDetailMblPage;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.ids.SendDetailMblPageIds;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.repository.SendDetailMblPageRepository;
import cokr.xit.ens.modules.common.ctgy.mblpage.presentation.MblPageControllerSupport;
import cokr.xit.ens.modules.ktsigntalk.direct.domain.SendDetailKtSigntalk;
import cokr.xit.ens.modules.ktsigntalk.direct.domain.repository.SendDetailKtSigntalkRepository;
import cokr.xit.ens.modules.ktsigntalk.direct.service.KtSigntalkService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Tag(name = "KtSigntalkMblPageController")
@Slf4j
@RequiredArgsConstructor
@Controller
public class KtSigntalkMblPageController extends MblPageControllerSupport {

    private final KtSigntalkService ktSigntalkService;
    private final SendDetailKtSigntalkRepository sendDetailKtSigntalkRepository;
    private final SendDetailMblPageRepository sendDetailMblPageRepository;



//    @Parameters({
//            @Parameter(in = ParameterIn.QUERY, name = "token"),
//            @Parameter(in = ParameterIn.QUERY, name = "src_key"),
//            @Parameter(in = ParameterIn.QUERY, name = "external_document_uuid"),
//    })
    @GetMapping(value = "/kt/signtalk/page/prnt")
    public String mblPagePrnt(@RequestParam Map<String, String> mParam, ModelMap model) {
        final String token = mParam.get("token");
        final String srcKey = mParam.get("src_key");


        try {
            if (CmmnUtil.isEmpty(token) && CmmnUtil.isEmpty(srcKey))
                throw new EnsException(EnsErrCd.ERR410, "정상적인 요청이 아닙니다. 재인증 후 시도하시기 바랍니다.");
            SendDetailKtSigntalk sendDetail = sendDetailKtSigntalkRepository.findFetchByClientSrcKeyAndSrcKey(null, srcKey)
                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, "문서를 찾을 수 없습니다."));


            EnsResponseVO responseVO = ktSigntalkService.tokenVerify(sendDetail.getSendMast().getOrgCd(), srcKey, token);
            if (!EnsErrCd.OK.equals(responseVO.getErrCode()))
                throw new EnsException(responseVO.getErrCode(), responseVO.getErrMsg());


            SendDetailKtSigntalk sendDetailKtSigntalk = sendDetailKtSigntalkRepository.findBySrcKey(srcKey)
                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 자료가 없습니다. [ src_key %s ]", srcKey)));

            SendDetailMblPage sendDetailMblPage = sendDetailMblPageRepository.findById(SendDetailMblPageIds.builder()
                            .postSe(sendDetailKtSigntalk.getSendMast().getPostSe().getCode())
                            .sendDetailId(sendDetailKtSigntalk.getSendDetailId())
                            .build())
                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 페이지데이터가 없습니다. [ src_key %s ]", srcKey)));

            if (CmmnUtil.isEmpty(sendDetailMblPage.getDetails()))
                throw new EnsException(EnsErrCd.ERR401, String.format("details 필드 값이 비어있습니다. [ src_key %s ]", srcKey));

            model.put("details", sendDetailMblPage.getDetails());
            
            if ("Y".equals(sendDetail.getMkBillUseYn())) {
                model.put("payBtnInf", getJsonPayBtnInf(sendDetail.getBill(), sendDetail.getSendMast()));
            }


        } catch (EnsException e) {
            model.put("errCode", e.getErrCd());
            model.put("errMsg", e.getMessage());
            model.put("details", "{}");
        }

        return "modules/mblpage/notiprnt";
    }

//    @GetMapping(value = "/kt/signtalk/page/prnt/test")
//    public String mblPagePrntTest(@RequestParam Map<String, String> mParam, ModelMap model) {
//        final String external_document_uuid = mParam.get("external_document_uuid");
//
//
//
//        try {
//            if (CmmnUtil.isEmpty(external_document_uuid))
//                throw new EnsException(EnsErrCd.ERR410, "정상적인 요청이 아닙니다. 인증 후 시도하시기 바랍니다.");
//
//            SendDetailKtSigntalk sendDetailKtSigntalk = sendDetailKtSigntalkRepository.findByPropExternalDocumentUuid(external_document_uuid)
//                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 자료가 없습니다. [ external_document_uuid %s ]", external_document_uuid)));
//
//            SendDetailMblPage sendDetailMblPage = sendDetailMblPageRepository.findById(SendDetailMblPageIds.builder()
//                            .postSe(sendDetailKtSigntalk.getSendMast().getPostSe().getCode())
//                            .sendDetailId(sendDetailKtSigntalk.getSendDetailId())
//                            .build())
//                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 페이지데이터가 없습니다. [ external_document_uuid %s ]", external_document_uuid)));
//
//            if (CmmnUtil.isEmpty(sendDetailMblPage.getDetails()))
//                throw new EnsException(EnsErrCd.ERR401, String.format("details 필드 값이 비어있습니다. [ external_document_uuid %s ]", external_document_uuid));
//
//            model.put("details", sendDetailMblPage.getDetails());
//            if ("Y".equals(sendDetailKtSigntalk.getMkBillUseYn()) && !CmmnUtil.isEmpty(sendDetailKtSigntalk.getKkoBillMast()))
//                model.put("paybutton_link", sendDetailKtSigntalk.getKkoBillMast().getUrl());
//
//
//        } catch (EnsException e) {
//            model.put("errCode", e.getErrCd());
//            model.put("errMsg", e.getMessage());
//        }
//
//        return "modules/mblpage/notiprnt";
//    }
//
//
//    @GetMapping(value = "/kt/signtalk/page/prnt/test/proxy")
//    public String mblPagePrntTemp(@RequestParam Map<String, String> mParam, ModelMap model) {
//        final String external_document_uuid = mParam.get("external_document_uuid");
//
//
//        return "modules/mblpage/notiprntproxy";
//    }


}
