package cokr.xit.ens.modules.kkomydoc.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.kkomydoc.domain.SendDetailKkoMydoc;
import cokr.xit.ens.modules.kkomydoc.domain.repository.SendDetailKkoMydocRepository;
import cokr.xit.ens.modules.kkomydoc.service.KkoMydocService;
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 = "KkoMydocMblPageController")
@Slf4j
@RequiredArgsConstructor
@Controller
public class KkoMydocMblPageController extends MblPageControllerSupport {

    private final KkoMydocService kkoMydocService;
    private final SendDetailKkoMydocRepository sendDetailKkoMydocRepository;
    private final SendDetailMblPageRepository sendDetailMblPageRepository;



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


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


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


            SendDetailKkoMydoc sendDetailKkoMydoc = sendDetailKkoMydocRepository.findByPropExternalDocumentUuidAndDocumentBinderUuid(external_document_uuid, document_binder_uuid)
                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 자료가 없습니다. [ document_binder_uuid %s external_document_uuid %s ]", document_binder_uuid, external_document_uuid)));

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

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

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


            kkoMydocService.readCmplt(sendDetail.getSendMast().getOrgCd(), document_binder_uuid);
        } catch (EnsException e) {
            model.put("errCode", e.getErrCd());
            model.put("errMsg", e.getMessage());
            model.put("details", "{}");
        }

        return "modules/mblpage/notiprnt";
    }


//    @GetMapping(value = "/kko/mydoc/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, "정상적인 요청이 아닙니다. 인증 후 시도하시기 바랍니다.");
//
//            SendDetailKkoMydoc sendDetailKkoMydoc = sendDetailKkoMydocRepository.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(sendDetailKkoMydoc.getSendMast().getPostSe().getCode())
//                            .sendDetailId(sendDetailKkoMydoc.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(sendDetailKkoMydoc.getMkBillUseYn()) && !CmmnUtil.isEmpty(sendDetailKkoMydoc.getKkoBillMast()))
//                model.put("paybutton_link", sendDetailKkoMydoc.getKkoBillMast().getUrl());
//
//
//        } catch (EnsException e) {
//            model.put("errCode", e.getErrCd());
//            model.put("errMsg", e.getMessage());
//        }
//
//        return "modules/mblpage/notiprnt";
//    }
//
//
//    @GetMapping(value = "/kko/mydoc/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";
//    }


}
