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.Dates;
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("bpd.dftcreRegister")
public class BpdDftcreRegister 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(), "FIA")) {
            dftcreVo.getDfdgrp().setApl(MdaUtils.deepClone(ptsptaToolComponent.getPtsFromListByRol(dftcreVo.getPtsptaList(), "FIA")));
        }
        dftcreVo.getDfdgrp().getApl().setRol("APL");
        dftcreVo.getDfdgrp().getApl().getPts().setRol("APL");
        if (StringUtils.isNotEmpty(ref)) {
            dftcreVo.getDfdgrp().getApl().getPts().setRef(ref);
        }
        if (!dftcreVo.getDfdgrp().getRec().isModified("stttendat")) {
            if ("X".equals(dftcreVo.getDfflag())) {
                dftcreVo.getDfdgrp().getRec().setStttendat(dftcreVo.getRec().getOpndat());
            } else {
                dftcreVo.getDfdgrp().getRec().setStttendat(Dates.today());
            }
        }
        if (!dftcreVo.getDfdgrp().getRec().isModified("matdat")) {
            if ("X".equals(dftcreVo.getDfflag())) {
                dftcreVo.getDfdgrp().getRec().setMatdat(dftcreVo.getRec().getMatdat());
                int tenday = Dates.diff(dftcreVo.getDfdgrp().getRec().getMatdat(), dftcreVo.getDfdgrp().getRec().getStttendat());
                dftcreVo.getDfdgrp().getRec().setTenday(tenday);
            }
        }
    }

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