package com.brilliance.isc.common.fremsg.service.impl;

import com.brilliance.isc.bo.Pts;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.fremsg.service.FremsgService;
import com.brilliance.isc.common.trndia.service.TrndiaService;
import com.brilliance.isc.common.trndia.vo.TrndiaVo;
import com.brilliance.isc.common.trndoc.service.TrndocService;
import com.brilliance.isc.common.trndoc.vo.TrndocVo;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.mda.dao.PtsMapper;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.brilliance.mda.runtime.mda.Constants.DiaToWrite;

@Service
public class FremsgServiceImpl implements FremsgService {

    @Resource
    private PtsptaToolComponent ptsptaToolComponent;
    @Resource
    private TrndocService trndocService;
    @Resource
    private PtsMapper ptsMapper;
    @Resource
    private TrndiaService trndiaService;
    @Resource
    private XrtmodService xrtmodService;
    @Override
    public void regDocFRE(BizInfoVo bizInfoVo, TrndocVo trndoc) {
        try {
            Class<?> clazz = bizInfoVo.getClass();
            Field sndmtField = clazz.getDeclaredField("sndmt");
            Field msgrolField = clazz.getDeclaredField("msgrol");
            Field ccrolField = clazz.getDeclaredField("ccrol");
            sndmtField.setAccessible(true);
            msgrolField.setAccessible(true);
            ccrolField.setAccessible(true);
            if("99|95|96|92|30|SUS|59".contains((String)sndmtField.get(bizInfoVo))){
                String bustyp = bizInfoVo.getObjtyp().substring(0,2);
                String mod = "\\" + bustyp + "DGRP" + "\\" + "rec";
                String namFre1 = "\\" + bustyp + "TFREL1";
                String namFre2 = "\\" + bustyp + "TFREL2";
                if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), "OTHMSG") && (!MdaUtils.isEmpty(ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHMSG").getAdrblk()))) {
                    String othmsgPtaInr = ptsptaToolComponent.getPtaInrByRol(trndoc.getPtsptas(), "OTHMSG");
                    trndocService.defDocEot(trndoc, "TRN", "FRE1", namFre1, "LET001", 0, othmsgPtaInr, mod, "Original", ((String)msgrolField.get(bizInfoVo)), ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHMSG").getAdrblk());
                    if (ptsptaToolComponent.ptsIsRolSet(trndoc.getPtsptas(), "OTHCC") && !MdaUtils.isEmpty(ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHCC").getAdrblk())) {
                        String othccPtaInr = ptsptaToolComponent.getPtaInrByRol(trndoc.getPtsptas(), "OTHCC");
                        trndocService.defDocEot(trndoc, "TRN", "FRE2", namFre2, "LET001", 0, othccPtaInr, "\\LIDGRP\\REC", "Copy", ((String)ccrolField.get(bizInfoVo)), ptsptaToolComponent.getPtsFromListByRol(trndoc.getPtsptas(), "OTHCC").getAdrblk());
                    }
                }
            }
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public Set<String> buildMsgrolList(String objinr, String objtyp) {
        Set<String> msgrolSet = new HashSet<>();
        List<Pts> ptsList = ptsMapper.getPtsByObjInrTyp(objinr, objtyp);
        if (ptsList != null) {
            ptsList.forEach(pts -> msgrolSet.add(pts.getRol()));
        }
        msgrolSet.add("OTH");
        return msgrolSet;
    }

    @Override
    public void freDiaSet(BizInfoVo bizInfoVo,TrndiaVo trndiaVo,String inifrm) {
        Class clazz = bizInfoVo.getClass();
        try {
            Field ownrefField = clazz.getDeclaredField("ownref");
            Field reldirField = clazz.getDeclaredField("reldir");
            Field sndmtField = clazz.getDeclaredField("sndmt");
            ownrefField.setAccessible(true);
            reldirField.setAccessible(true);
            sndmtField.setAccessible(true);
            String ownref = (String)ownrefField.get(bizInfoVo);
            String reldir = (String)reldirField.get(bizInfoVo);
            String sndmt = (String)sndmtField.get(bizInfoVo);

            if (MdaUtils.compareTo(reldir, "S") == 0) {
                if (MdaUtils.compareTo(sndmt, "92") == 0 || MdaUtils.compareTo(sndmt, "95") == 0) {
                    trndiaService.trnDiaSet(trndiaVo, "FOLQUE", Dates.dateAddW(Dates.today(), 5, xrtmodService.calendar()), "FOL", Strings.getText("Reply to Query pending", ownref), inifrm, DiaToWrite);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }
}
