package com.brilliance.isc.common.register.dftcre.impl;

import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Trn;
import com.brilliance.isc.bo.model.Dftcre;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.register.dftcre.IDftcreRegister;
import com.brilliance.isc.vo.model.DftcreBizInfoVo;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("trd.dftcreRegister")
public class TrdDftcreRegister implements IDftcreRegister {

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Override
    public void setDFDfieldsfromXXX(Dftcre dftcreVo) {
        DftcreBizInfoVo rec = dftcreVo.getRec();
        dftcreVo.getDfdgrp().getRec().setPntinr(rec.getObjinr());
        dftcreVo.getDfdgrp().getRec().setPnttyp(rec.getObjtyp());
        dftcreVo.getDfdgrp().getRec().setPntref(rec.getOwnref());
        dftcreVo.getDfdgrp().getRec().setPntnam(rec.getNam());
        Cbb maxcbb = dftcreVo.getCbsMap().getOrDefault("MAX", new Cbb());
        if(!MdaUtils.isEmpty(maxcbb.getCur()) && !dftcreVo.getDfdgrp().getCbs().getMax().isModified("cur")) {
            dftcreVo.setOricur(maxcbb.getCur());
            if(!dftcreVo.getDfdgrp().getCbs().getMax().isModified("cur")) {
                dftcreVo.getDfdgrp().getCbs().getMax().setCur(maxcbb.getCur());
            }
        }
        if(!MdaUtils.isEmpty(maxcbb.getAmt())) {
            dftcreVo.setOriamt(maxcbb.getAmt());
            if(!dftcreVo.getDfdgrp().getCbs().getMax().isModified("amt")) {
                dftcreVo.getDfdgrp().getCbs().getMax().setAmt(maxcbb.getAmt());
            }
        }
        String ref = dftcreVo.getDfdgrp().getApl().getRef();
        if (ptsptaToolComponent.ptsIsRolSet(dftcreVo.getPtsptaList(), "FIP")) {
            dftcreVo.getDfdgrp().setApl(MdaUtils.deepClone(ptsptaToolComponent.getPtsFromListByRol(dftcreVo.getPtsptaList(), "FIP")));
        }
        dftcreVo.getDfdgrp().getApl().setRol("APL");
        dftcreVo.getDfdgrp().getApl().getPts().setRol("APL");
        if (StringUtils.isNotEmpty(ref)) {
            dftcreVo.getDfdgrp().getApl().getPts().setRef(ref);
        }
    }

    @Override
    public boolean isNeedRepair(Trn trn) {
        if ("TRTOPN".equals(trn.getInifrm())) {
            return true;
        }
        return false;
    }
}
