package cokr.xit.ens.modules.common.ctgy.intgrnnoti.presentation;

import java.util.*;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;

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.code.*;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.repository.*;
import io.swagger.v3.oas.annotations.tags.*;
import lombok.*;
import lombok.extern.slf4j.*;

@Tag(name = "IntgrnNotiMblPageController")
@Slf4j
@RequiredArgsConstructor
@Controller
public class IntgrnNotiMblPageController {

    private final IntgrnSendDetailRepository intgrnSendDetailRepository;

    @Value("${xit.mblpage.postse.kkomd.path}")
    private String KKOMD_MBL_PATH;
    // FIXME: 카카오톡 추가
    @Value("${xit.mblpage.postse.kkotalk.path}")
    private String KKOTALK_MBL_PATH;
    @Value("${xit.mblpage.postse.nvst.path}")
    private String NVST_MBL_PATH;
    @Value("${xit.mblpage.postse.ktst.path}")
    private String KTST_MBL_PATH;
    @Value("${xit.mblpage.postse.ktgbs.path}")
    private String KTGBS_MBL_PATH;


    @GetMapping(value = "/intgrn/noti/page/prnt")
    public String mblPagePrnt(@RequestParam Map<String, String> mParam, ModelMap model) {

        String docUuid = null;
        String linkedUuid = null;
        // 카카오페이
        if (docUuid == null) docUuid = mParam.get("document_binder_uuid");
        if (linkedUuid == null) linkedUuid = mParam.get("external_document_uuid");
        // 네이버페이
        if (docUuid == null) docUuid = mParam.get("docId");
        if (linkedUuid == null) linkedUuid = mParam.get("clientDocId");

        // kt
        if (docUuid == null) {
            docUuid = CmmnUtil.isEmpty(mParam.get("src_key")) ? null : mParam.get("src_key").replaceAll("[\\\"\\,\\[\\]]", "");
            linkedUuid = null;
        }

        if (docUuid == null) {
            docUuid = CmmnUtil.isEmpty(mParam.get("envelopeId")) ? null : mParam.get("envelopeId").replaceAll("[\\\"\\,\\[\\]]", "");
            linkedUuid = null;
        }

        try {
            if (CmmnUtil.isEmpty(docUuid))
                throw new EnsException(EnsErrCd.ERR410, "정상적인 요청이 아닙니다. 재인증 후 시도하시기 바랍니다. ( 내부식별ID는 필수조건 입니다 )");


            final String finalDocUuid = docUuid;
            final String finalLinkedUuid = linkedUuid;
//            IntgrnSendDetail intgrnSendDetail = intgrnSendDetailRepository.findByDocUuidAndLinkedUuid(docUuid, linkedUuid)
//                    .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, String.format("일치하는 자료가 없습니다. [docUuid %s linkedUuid %s]", finalDocUuid, finalLinkedUuid)));
            final PostSeCd postSeCd = intgrnSendDetailRepository.findPostSeByDocUuidAndLinkedUuid(docUuid, linkedUuid);
            if (postSeCd == null)
                throw new EnsException(EnsErrCd.ERR404, String.format("일치하는 자료가 없습니다. [docUuid %s linkedUuid %s]", finalDocUuid, finalLinkedUuid));


            String url = null;
            if (PostSeCd.kkoMydoc.equals(postSeCd)) url = KKOMD_MBL_PATH;
            // FIXME: 카카오톡 추가
            else if (PostSeCd.kkoTalk.equals(postSeCd)) url = KKOTALK_MBL_PATH;
            else if (PostSeCd.nvSigntalk.equals(postSeCd)) url = NVST_MBL_PATH;
            else if (PostSeCd.ktSigntalk.equals(postSeCd)) url = KTST_MBL_PATH;
            else if (PostSeCd.ktGibis.equals(postSeCd)) url = KTGBS_MBL_PATH;
            else
                throw new EnsException(EnsErrCd.ERR500, String.format("정의되지 않은 우편구분 입니다. [linkedUuid %s]", finalLinkedUuid));

            return "forward:" + url;
        } catch (EnsException e) {
            model.put("errCode", e.getErrCd());
            model.put("errMsg", e.getMessage());
            return "modules/mblpage/notiprnt";
        }
    }

}
