package cokr.xit.ens.modules.kkotalk.presentation;

import java.util.*;

import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;

import cokr.xit.ens.core.aop.*;
import cokr.xit.ens.core.exception.*;
import cokr.xit.ens.core.exception.code.*;
import cokr.xit.ens.core.utils.*;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.*;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.*;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.ids.*;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.repository.*;
import cokr.xit.ens.modules.common.ctgy.mblpage.presentation.*;
import cokr.xit.ens.modules.common.domain.*;
import cokr.xit.ens.modules.kkotalk.mapper.*;
import cokr.xit.ens.modules.kkotalk.model.*;
import cokr.xit.ens.modules.kkotalk.service.*;
import io.swagger.v3.oas.annotations.tags.*;
import lombok.*;
import lombok.extern.slf4j.*;

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

    private final KkoTalkService kkoTalkService;
    private final IKkoTalkMapper kkoTalkMapper;
    private final SendDetailMblPageRepository sendDetailMblPageRepository;

    @GetMapping(value = "/kko/talk/page/prnt")
    public String mblPagePrnt(@RequestParam Map<String, String> mParam, ModelMap model) {
        final String token = mParam.get("token");
        final String envelopeId = mParam.get("envelopeId");
        final String externalId = mParam.get("externalId");

        try {
            if (CmmnUtil.isEmpty(token) && CmmnUtil.isEmpty(envelopeId) && CmmnUtil.isEmpty(externalId))
                throw new EnsException(EnsErrCd.ERR410, "정상적인 요청이 아닙니다. 재인증 후 시도하시기 바랍니다.");

            Map<String, String> param = new HashMap<>();
            param.put("envelopeId", envelopeId);
            param.put("externalId", externalId);
            SendMast sendMast = kkoTalkMapper.selectSendMastByExternalIdAndEnvelopeId(param)
                .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, "문서를 찾을 수 없습니다."));

            SendDetailKkoTalkDTO sendDetailKkoTalkDTO = kkoTalkMapper.findFetchByExternalIdAndEnvelopeId(param)
                .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 자료가 없습니다. [ envelopeId %s externalId %s ]", envelopeId, externalId)));

            EnsResponseVO<?> responseVO = kkoTalkService.tokenVerify(sendMast.getOrgCd(), envelopeId, token, externalId);
            if (!EnsErrCd.OK.equals(responseVO.getErrCode()))
                throw new EnsException(responseVO.getErrCode(), responseVO.getErrMsg());

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

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

            model.put("details", sendDetailMblPage.getDetails());

            if ("Y".equals(sendDetailKkoTalkDTO.getMkBillUseYn())) {
                Bill bill = kkoTalkMapper.selectBillByBillUid(sendDetailKkoTalkDTO.getBillUid())
                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 결재 정보가 없습니다. [ bill_uid %s ]", sendDetailKkoTalkDTO.getBillUid())));
                model.put("payBtnInf", getJsonPayBtnInf(bill, sendMast));
            }
            kkoTalkService.readCmplt(sendMast.getOrgCd(), envelopeId);

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

        return "modules/mblpage/notiprnt";
    }
}
