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

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.code.PostSeCd;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.repository.IntgrnSendDetailRepository;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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 = "IntgrnNotiMblPageController")
@Slf4j
@RequiredArgsConstructor
@Controller
public class IntgrnNotiMblPageController {

    private final IntgrnSendDetailRepository intgrnSendDetailRepository;

    @Value("${xit.mblpage.postse.kkomd.path}")
    private String KKOMD_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");
        if (docUuid == null) {
            docUuid = CmmnUtil.isEmpty(mParam.get("src_key")) ? null : mParam.get("src_key").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;
            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";
        }
    }

}
