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


import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;
import com.brilliance.mda.runtime.mda.util.*;
import com.brilliance.isc.common.cbsmod.Cbsget;
import com.brilliance.isc.common.stgmod.service.StgmodService;
import com.brilliance.isc.common.stgmod.vo.StgmodVo;
import com.brilliance.isc.common.util.SettleExceptionUtils;
import com.brilliance.isc.common.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

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

@Service
public class StgmodServiceImpl implements StgmodService {

    private static Logger log = LoggerFactory.getLogger(StgmodServiceImpl.class);

    @Autowired
    Cbsget cbsget;

    @Override
    public void gridCfgInit(StgmodVo stgmod) {
        stgmod.reset();
        Streams.streamSet(stgmod.getAddstm(), "P COLORED TRUE" + CR + "P VERTLINES TRUE" + CR + "P HORZLINES TRUE" + CR + "P MULTISELECT FALSE" + CR + "P COLUMNSIZING TRUE");
    }

    @Override
    public void gridCfgAddJoinIns(StgmodVo stgmod, String argIns, String argTbl, String argCon) {

        if (stgmod.getInscnt() > 0) {
            SettleExceptionUtils.sysDump("STGMOD.GridCfgAddJoinIns: After defining a link instances no more join instances might be added.");
        }
        int idx = Streams.streamSearch(stgmod.getInsstm(), argIns + " ");
        if (idx > 0) {
            SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddxxxxIns: Instance $1 already defined.", argIns));
        }
        String tbl = NULLSTR;
        if (MdaUtils.isEmpty(argTbl)) {
            tbl = argIns;
        } else {
            tbl = argTbl;
        }
        String lin = argIns + " " + tbl;
        if (!MdaUtils.isEmpty(argCon)) {
            lin = lin + TAB + argCon;
        }
        Streams.streamInsert(stgmod.getInsstm(), 0, lin);
    }

    @Override
    public void gridCfgAddFld(StgmodVo stgmod, String argIns, Map<String, String> argFld, double argPos, String argHea, int argSiz, String argCod) {

    }

    public static String sqlLit(String argTxt) {
        String arg = NULLSTR;
        if (MdaUtils.isEmpty(argTxt)) {
            arg = " ";
        } else {
            arg = Strings.trim(argTxt);
        }
        // search for single quotes as they have to be doubled
        int pos = Strings.pos(arg, "'");
        int nxt = pos;
        while (nxt > 0) {
            arg = Strings.mid(arg, 1, pos - 1) + "''" + Strings.mid(arg, pos + 1);
            nxt = Strings.pos(Strings.mid(arg, pos + 2), "'");
            pos = pos + nxt + 1;
        }
        return "'" + arg + "'";

    }

    public void gridCfgAddCol(StgmodVo stgmod, String argCol, double argPos, String argHea, int argSiz, int argTyp, String argFmtPar) {
        //取得上下文

        //! routine to add a column to a streamgrid configuration in case the datafield is not available or special/non standard functions are required
        //!
        //! ArgCol database field in form <ins>.<fld>
        //!     or sql-literal where the contents is to be put into the stream
        //!     or empty for separatly added fields
        //! ArgPos <visible column of grid>.<row in grid>
        //! Both values are starting with 1. A zero value defaults to the next column or row.
        //! ArgHea Heading of the column
        //! ArgSiz Column width in pixel (-1 for invisible fields like an INR)
        //! ArgTyp Datatype of column (e.g. tdDataTypeText, tdDataTypeDate)
        //! ArgFmtPar additional formating parameters inserted after the datatype separated by a space
        //!
        //! Corresponding routines: GridCfgAddCbs, GridCfgAddFld, GridCfgAddJoinIns, GridCfgAddLinkIns, GridCfgAddPara, GridCfgAddPts, GridCfgInit, GridCfgLoadTds, GridCfgSetDefaultOrder, GridCfgSetPromptStream, GridCfgStore
        int pos = Strings.pos(argCol, ".");
        String ins = NULLSTR;
        int idx = 0;
        if (pos > 0) {
            ins = Strings.mid(argCol, 1, pos - 1);
            // check for existance of the used instance
            idx = Streams.streamSearch(stgmod.getInsstm(), ins + " ");
            if (idx == 0) {
                SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddCol: The field $1 uses an not yet defined instance $2.\nProbably a sequence error when adding fields of joined instances after having defined link instances.", argCol, ins));
            }
            if (stgmod.getInscnt() > 0 && idx < Streams.streamCount(stgmod.getInsstm())) {
                SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddCol: Column has to be part of last defined link instance.\nPlease reorder the column defining calls for performance reason.\n($1 / $2)", argCol, argHea));
            }
        }
        // analyse position -> $Col + $Row
        int col = 0;
        BigDecimal row = null;
        if (MdaUtils.compareTo(argSiz, 0) < 0) {
            col = 0;
            row = new BigDecimal("0");
        } else {
            if (argPos == 0) {
                col = Numerics.toInt(stgmod.getPoslst()) + 1;
                row = new BigDecimal("0");
            } else {
                if (argPos < 1) {
                    col = Numerics.toInt(stgmod.getPoslst());
                    row = new BigDecimal(Numerics.toInt(argPos * 10));
                } else {
                    if (argPos == Numerics.toInt(argPos)) {
                        if (Numerics.toInt(argPos) == Numerics.toInt(stgmod.getPoslst())) {
                            col = Numerics.toInt(argPos);
                            row = Decimals.add(Decimals.mul((Decimals.sub(stgmod.getPoslst(), Numerics.toInt(stgmod.getPoslst()))), 10), 1);
                        } else {
                            col = Numerics.toInt(argPos);
                            row = new BigDecimal("1");
                        }
                    } else {
                        col = Numerics.toInt(argPos);
                        row = new BigDecimal(Numerics.toInt((argPos - Numerics.toInt(argPos)) * 10));
                    }
                }
            }
            stgmod.setPoslst(Decimals.add(col, Decimals.div(row, 10)));
        }
        // store field in list of fields (SQL)
        IStream stream = stgmod.getFldstm();
        boolean flag = false;
        //TODO order by的字段名重复时,会额外拼接一个别名
        if (argCol.contains(".")) {
            for (int i = 1; i <= stream.countLines(); i++) {
                String line = stream.getLine(i);
                if (line.contains(".")) {
                    String s = line.split("\\.")[1];
                    if (s.equalsIgnoreCase(argCol.split("\\.")[1] + TAB)) {
                        flag = true;
                        break;
                    }
                }
            }
        }
        Streams.streamInsert(stgmod.getFldstm(), 0, flag ? (argCol + TAB + " " + argCol.replaceAll("\\.", "_")) : argCol + TAB);
        if (stgmod.getInscnt() == 0) {
            stgmod.setFldcnt(Streams.streamCount(stgmod.getFldstm()));
        }
        // Fields with a negative size are not included
        int cnt = 0;
        String lin = NULLSTR;
        if (MdaUtils.compareTo(argSiz, 0) >= 0) {
            cnt = Streams.streamCount(stgmod.getFldstm()) - 1;
            lin = MdaUtils.toString(cnt) + " " + MdaUtils.toString(Numerics.toInt(col));
            if (MdaUtils.compareTo(row, 0) > 0) {
                lin = lin + ":" + MdaUtils.toString(Numerics.toInt(row));
            }
            lin = lin + " " + Quote + argHea + Quote + " " + MdaUtils.toString(argSiz);
            if (MdaUtils.compareTo(argTyp, 1) != 0 || !MdaUtils.isEmpty(argFmtPar)) {
                lin = lin + " " + MdaUtils.toString(argTyp);
                if (!MdaUtils.isEmpty(argFmtPar)) {
                    lin = lin + " " + argFmtPar;
                }
            }
            Streams.streamInsert(stgmod.getValstm(), 0, lin);
        }

    }

    @Override
    public void gridCfgAddPts(StgmodVo stgmod, String argIns, String argRol, String argBusIns, String argTyp) {

        //! Routine to add a reference to a pts instance of a contract within an streamgrid definition.
        //! ArgIns new instance (default = role)
        //! ArgRol Role of the PTS instance
        //! ArgBusIns Instance of the referred to contract
        //! ArgTyp holds `link` or `join` depending on the requested connection
        int idx = Streams.streamSearch(stgmod.getInsstm(), argBusIns + " ");
        if (idx == 0) {
            SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddPts: Contract instance $1 unknown.", argBusIns));
        }
        String lin = Strings.getLine(stgmod.getInsstm(), idx);
        int pos = Strings.pos(lin, TAB);
        if (pos > 0) {
            lin = Strings.mid(lin, 1, pos - 1);
        }
        pos = Strings.pos(lin, " ");
        String tbl = Strings.trim(Strings.mid(lin, pos + 1));
        String ins = NULLSTR;
        if (MdaUtils.isEmpty(argIns)) {
            ins = argRol;
        } else {
            ins = argIns;
        }
        String chk = Strings.toLower(argTyp);
        if (MdaUtils.compareTo(chk, "link") != 0 && MdaUtils.compareTo(chk, "join") != 0) {
            SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddPts: in definition of instance $1 type $2 is used.\nOnly link or join are allowed.", argIns, argTyp));
        }
        String sql = Strings.formatText("$1.OBJTYP = $2 and $1.OBJINR = >>$3.INR<< and $1.ROL = $4", ins, sqlLit(tbl), argBusIns, sqlLit(argRol));
        String cmd = Strings.formatText("GridCfgAdd$1Ins( \"$2\", \"PTS\", \"$3\" )", argTyp, ins, sql);
//        Modules.invokeExpress( stgmod, cmd, -1);

    }

    @Override
    public void gridCfgAddPta(StgmodVo stgmod, String argIns, String argrol, String argBusIns, String argTyp) {

        //! Routine to add a reference to a pts instance of a contract within an streamgrid definition.
        //! ArgIns new instance (default = role)
        //! ArgRol Role of the PTS instance
        //! ArgBusIns Instance of the referred to contract
        //! ArgTyp holds `link` or `join` depending on the requested connection
        int idx = Streams.streamSearch(stgmod.getInsstm(), argBusIns + " ");
        idx=2;
        if (idx == 0) {
            SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddPts: Contract instance $1 unknown.", argBusIns));
        }
        String lin = Strings.getLine(stgmod.getInsstm(), idx);
        int pos = Strings.pos(lin, TAB);
        if (pos > 0) {
            lin = Strings.mid(lin, 1, pos - 1);
        }
        pos = Strings.pos(lin, " ");
        String tbl = Strings.trim(Strings.mid(lin, pos + 1));
        String ins = NULLSTR;
        if (MdaUtils.isEmpty(argIns)) {
            ins = argrol;
        } else {
            ins = argIns;
        }
        String chk = Strings.toLower(argTyp);
        if (MdaUtils.compareTo(chk, "link") != 0 && MdaUtils.compareTo(chk, "join") != 0) {
            SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddPts: in definition of instance $1 type $2 is used.\nOnly link or join are allowed.", argIns, argTyp));
        }
        //$Sql = FormatText( "$1.ptyextkey= substring(>>$2.extkey<<,1,10) ", $Ins, ArgBusIns )
        String sql = Strings.formatText("$1.inr= >>$2.ptaINR<< ", ins, argBusIns);
        String cmd = Strings.formatText("GridCfgAdd$1Ins( \"$2\", \"PTA\", \"$3\" )", argTyp, ins, sql);
//        Modules.invokeExpress((IModule) stgmod, cmd, -1);

    }

    @Override
    public void gridCfgAddCbs(StgmodVo stgmod, String argIns, String argCbc, String argExtid, String argBusIns, String argTyp) {
        cbsget.gridCfgAddCbs(stgmod, argIns, argCbc, argExtid, argBusIns, argTyp);
    }

    @Override
    public void gridCfgSetDefaultOrder(StgmodVo stgmod, String argTds, String argOrder) {
        String fld = argOrder;
        int pos = 0;
        String arg = NULLSTR;
        String col = NULLSTR;
        int idx = 0;
        while (!MdaUtils.isEmpty(fld)) {
            pos = Strings.pos(fld, ",") - 1;
            if (pos <= 0) {
                arg = Strings.trim(fld);
                fld = "";
            } else {
                arg = Strings.trim(Strings.mid(fld, 1, pos));
                fld = Strings.trim(Strings.mid(fld, pos + 2));
            }
            pos = Strings.pos(arg, " ") - 1;
            if (pos <= 0) {
                col = arg;
                arg = "ASC";
            } else {
                col = Strings.trim(Strings.mid(arg, 1, pos));
                arg = Strings.trim(Strings.mid(arg, pos + 1));
            }
            idx = Streams.streamSearch(stgmod.getFldstm(), col + TAB);
            if (idx == 0) {
                SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgSetDefaultOrder: Field '$3' in definition of $1 with order $2 is not yet defined.", argTds, argOrder, col));
            }
        }

    }

    @Override
    public void gridCfgStore(StgmodVo stgmod, String argTds) {
        String stgVal = MdaUtils.toString(stgmod.getValstm()) + CR + MdaUtils.toString(stgmod.getAddstm());
        // build the list of fields
        String sqlFld = "";
        String delFld = "";
        String lin = NULLSTR;
        int pos = 0;
        for (int idx = 1; idx <= stgmod.getFldcnt(); idx++) {
            lin = Strings.getLine(stgmod.getFldstm(), idx);
            if (!MdaUtils.isEmpty(lin)) {
                pos = Strings.pos(lin, TAB);
                sqlFld = sqlFld + delFld + Strings.mid(lin, 1, pos - 1);
                delFld = ", ";
            }
        }
        String sqlTbl = "";
        String tblDel = "";
        String conSql = "";
        String conDel = "";
        if (stgmod.getInscnt() == 0) {
            stgmod.setInscnt(Streams.streamCount(stgmod.getInsstm()));
        }
        String tblIns = NULLSTR;
        String con = NULLSTR;
        for (int idx = 1; idx <= stgmod.getInscnt(); idx++) {
            lin = Strings.getLine(stgmod.getInsstm(), idx);
            pos = Strings.pos(lin, TAB);
            if (pos == 0) {
                tblIns = lin;
                con = "";
            } else {
                tblIns = Strings.mid(lin, 1, pos - 1);
                con = Strings.mid(lin, pos + 1);
            }
            pos = Strings.pos(tblIns, " ");
            sqlTbl = sqlTbl + tblDel + Strings.mid(tblIns, pos + 1) + " " + Strings.mid(tblIns, 1, pos - 1);
            tblDel = ", ";
            if (!MdaUtils.isEmpty(con)) {
                conSql = conSql + conDel + con;
                conDel = " and ";
            }
        }
//        Caches.cacheWrite(sqlFld, "stgmod", argTds, "SqlSelectFields");
//        Caches.cacheWrite(sqlTbl, "stgmod", argTds, "SqlSelectTables");
//        Caches.cacheWrite(conSql, "stgmod", argTds, "SqlSelectCondition");
        // check whether a default order is defined
        String defSrt = "";
        Argument<String> defSrtBox = new Argument<String>("", "");
//        Caches.cacheRead(defSrtBox, "stgmod", argTds, "SqlSelectOrder");
        defSrt = defSrtBox.value;
        int max = 0;
        int idx = 0;
        int fldIdx = 0;
        String fldLin = NULLSTR;
        int posTab = 0;
        /*
        if (ctx.getErrorCode() > 0) {
            // Default sort is the columndefinition of the column 1 of the streamgrid
            max = Streams.streamCount(stgmod.getValstm());
            idx = 1;
            while (idx <= max) {
                lin = Strings.getLine(stgmod.getValstm(), idx);
                pos = Strings.pos(lin, " ");
                if (MdaUtils.compareTo(Strings.mid(lin, pos, 3), " 1 ") == 0 || MdaUtils.compareTo(Strings.mid(lin, pos, 5), " 1:1 ") == 0) {
                    fldIdx = Strings.parseInt(Strings.mid(lin, 1, pos - 1));
                    fldLin = Strings.getLine(stgmod.getFldstm(), fldIdx + 1);
                    posTab = Strings.pos(fldLin, TAB);
                    Caches.cacheWrite(Strings.mid(fldLin, 1, posTab - 1), "stgmod", argTds, "SqlSelectOrder");
                    break;
                }
                idx = idx + 1;
            }
        }
         */
        // Determine all required links
        int fldMax = Streams.streamCount(stgmod.getFldstm());
        int sqlCnt = 0;
        String sqlIns = NULLSTR;
        String sqlCon = NULLSTR;
        IStream lnkStm = new StreamImpl();
        int posDot = 0;
        String fldIns = NULLSTR;
        String lnkTxt = NULLSTR;
        if (fldMax > stgmod.getFldcnt()) {
            sqlFld = "";
            sqlCnt = 0;
            sqlIns = "";
            sqlCon = "";
            Streams.streamClear(lnkStm);
            // collect all fields for one instance into one sql
            for (fldIdx = stgmod.getFldcnt() + 1; fldIdx <= fldMax; fldIdx++) {
                fldLin = Strings.getLine(stgmod.getFldstm(), fldIdx);
                posTab = Strings.pos(fldLin, TAB);
                posDot = Strings.pos(fldLin, ".");
                fldIns = Strings.mid(fldLin, 1, posDot - 1);
                if (MdaUtils.compareTo(fldIns, sqlIns) != 0) {
                    internalAddLink(stgmod, lnkStm, sqlIns, sqlFld, sqlCnt);
                    sqlFld = "";
                    sqlCnt = 0;
                    sqlCon = "";
                    sqlIns = fldIns;
                }
                sqlFld = sqlFld + sqlCon + Strings.mid(fldLin, 1, posTab - 1);
                sqlCon = ", ";
                sqlCnt = sqlCnt + 1;
            }
            internalAddLink(stgmod, lnkStm, sqlIns, sqlFld, sqlCnt);
            if (!MdaUtils.isEmpty(lnkStm)) {
                lnkTxt = MdaUtils.toString(lnkStm);
//                Caches.cacheWrite(lnkTxt, "stgmod", argTds, "SqlLinks");
            }
        }
        // Cleanup all internaly used cfg data
        //MdaUtils.clear(stgmod);

    }

    public void internalAddLink(StgmodVo stgmod, IStream argStm, String argIns, String argFld, int argCnt) {
        //取得上下文

        int idx = 0;
        String lin = NULLSTR;
        int posTab = 0;
        int posSpc = 0;
        String sql = NULLSTR;
        if (!MdaUtils.isEmpty(argFld)) {
            idx = Streams.streamSearch(stgmod.getInsstm(), argIns + " ");
            if (idx == 0) {
                SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.InternalAddLink: used instance $1 is not defined.", argIns));
            }
            lin = Strings.getLine(stgmod.getInsstm(), idx);
            posTab = Strings.pos(lin, TAB);
            posSpc = Strings.pos(lin, " ");
            sql = Strings.formatText("select $1 from $2 $3 where $4", argFld, Strings.mid(lin, posSpc + 1, posTab - posSpc - 1), Strings.mid(lin, 1, posSpc - 1), Strings.mid(lin, posTab + 1));
            Streams.streamInsert(argStm, 0, MdaUtils.toString(argCnt) + TAB + sql);
        }

    }

//    @Override
//    public void gridSetValues(IDatafield argStg, String argTds) {
//        String stgVal = "";
//        Argument<String> stgValBox = new Argument<String>("", "");
//        Caches.cacheRead(stgValBox, "stgmod", argTds, "StreamGridValues");
//        stgVal = stgValBox.value;
//        Systems.setValues(argStg, stgVal);
//
//    }


//    public void gridCfgAddFld(StgmodVo stgmod, String argIns, IDatafield argFld, double argPos, String argHea, int argSiz, String argCod) {
//        //! Add a Streamgrid column based on the information available for a real datafield. The routine internally determines default values and calls GridCfgAddCol with those values.
//        //! For non default settings use GridCfgAddCol directly.
//        //! ArgIns instance the column is associated to (defaultvalue = table of owning module)
//        //! ArgFld real field to be included into the streamgrid
//        //! ArgPos Position of the column (0 -> first row of next column, Integerpart = Column number, Decimal part Row number)
//        //! ArgHea Heading of the column
//        //! ArgSiz Size of the column (-1 to exclude field from Streamgrid, intended used for INR or other internal keys, 0 to use a default width depending of fieldattributes)
//        //! ArgCod Name of Codetable to decode or formating parameterline (starting with blanc)
//        //!
//        //! The table name of the module holding the passed field is used as instancename of the field.
//        //!
//        //! Corresponding routines: GridCfgAddCbs, GridCfgAddCol, GridCfgAddFld, GridCfgAddJoinIns, GridCfgAddLinkIns, GridCfgAddPara, GridCfgAddPts, GridCfgInit, GridCfgLoadTds, GridCfgSetDefaultOrder, GridCfgSetPromptStream, GridCfgStore
//        BigDecimal siz = new BigDecimal(argSiz);
//        // get field name
//        String fldNam = Strings.toUpper(Systems.getAttributeText(argFld, tdAttrName));
//        String fldPth = Utils.getPathofName(Systems.getAttributeText(argFld, tdAttrFullName));
//        // get table name   to  do  stgmod强转IModule有问题
//        String tbl = Strings.toUpper(Strings.trim(Databases.getTableName(MdaDriver.getModule((IModule) stgmod, fldPth))));
//        // get instance name
//        String ins = NULLSTR;
//        if (MdaUtils.isEmpty(argIns)) {
//            ins = tbl;
//        } else {
//            ins = argIns;
//        }
//        // check for an instance with the name of the table
//        int idx = Streams.streamSearch(stgmod.getInsstm(), ins + " ");
//        if (idx == 0) {
//            SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridAddFld: Instance $1 used in definition of field $2 in module $3 not previously defined with GridAddIns", ins, fldNam, fldPth));
//        }
//        // determine the sql fieldreference
//        String col = ins + "." + fldNam;
//        // determine datatype of field
//        int fldTyp = Systems.getAttribute(argFld, tdAttrDatatype);
//        // Determine a default column width
//        int fldLen = Systems.getAttribute(argFld, tdAttrLength);
//        String fmt = "";
//        String namPfx = NULLSTR;
//        String curNam = NULLSTR;
//        int curIdx = 0;
//        switch (fldTyp) {
//            case tdDataTypeNumeric:
//                namPfx = Strings.toUpper(Strings.mid(fldNam, Strings.len(fldNam) - 2));
//                switch (namPfx) {
//                    case "AMT":
//                        if (MdaUtils.compareTo(fldNam, "BMHAMT") == 0) {
//                        } else {
//                            curNam = Strings.mid(col, 1, Strings.len(col) - 3) + "CUR";
//                            curIdx = Streams.streamSearch(stgmod.getFldstm(), curNam + TAB);
//                            if (curIdx == 0) {
//                                SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridCfgAddFld: Currency not found. Either add $1 previously to $2 or define column via GridCfgAddCol and add the formating manually.", curNam, fldNam));
//                            }
//                            fmt = MdaUtils.toString(tdViewTypeAmount) + " 1 " + MdaUtils.toString(curIdx - 1);
//                            if (MdaUtils.compareTo(siz, 0) == 0) {
//                                siz = new BigDecimal(8 + fldLen * 5);
//                            }
//                        }
//                        break;
//                    case "TIM":
//                        fmt = MdaUtils.toString(tdViewTypeTime) + " 1";
//                        if (MdaUtils.compareTo(siz, 0) == 0) {
//                            siz = new BigDecimal(fldLen * 8);
//                        }
//                        break;
//                }
//                break;
//            case tdDataTypeDate:
//                fmt = MdaUtils.toString(tdViewTypeDate);
//                if (MdaUtils.compareTo(siz, 0) == 0) {
//                    siz = new BigDecimal(fldLen * 5);
//                }
//                break;
//            case tdDataTypeDateTime:
//                fmt = MdaUtils.toString(tdViewTypeDateTime);
//                if (MdaUtils.compareTo(siz, 0) == 0) {
//                    siz = new BigDecimal(fldLen * 5);
//                }
//                break;
//            case tdDataTypeText:
//                if (!MdaUtils.isEmpty(argCod) && MdaUtils.compareTo(Strings.mid(argCod, 1, 1), " ") != 0) {
//                    fmt = MdaUtils.toString(tdViewTypeEdit) + " 1 " + argCod;
//                }
//                break;
//        }
//        // Use argcod as formatextension in case it starts with a blanc
//        if (!MdaUtils.isEmpty(argCod) && MdaUtils.compareTo(Strings.mid(argCod, 1, 1), " ") == 0) {
//            fmt = Strings.trim(argCod);
//        }
//        // Determine a default column width
//        if (MdaUtils.compareTo(siz, 0) == 0) {
//            siz = new BigDecimal(fldLen * 5 + 10);
//            if (fldLen > 20) {
//                siz = Decimals.sub(siz, fldLen);
//            }
//        }
//        gridCfgAddCol(stgmod, col, argPos, argHea, siz.intValue(), fldTyp, fmt);
//
//    }


    public String gridCfgGetTbl4Ins(StgmodVo stgmod, String argIns) {
        //! function to return the table a grid instance is associated to.
        //! ArgIns holds the instance the table is searched for
        //! The routine is intended to be used during the configuration of a streamgrid for internal routines.
        //!
        //! Corresponding routines: GridCfgAddCbs, GridCfgAddCol, GridCfgAddFld, GridCfgAddJoinIns, GridCfgAddLinkIns, GridCfgAddPara, GridCfgAddPts, GridCfgInit, GridCfgLoadTds, GridCfgSetDefaultOrder, GridCfgSetPromptStream, GridCfgStore
        int idx = Streams.streamSearch(stgmod.getInsstm(), argIns + " ");
        if (idx == 0) {
            SettleExceptionUtils.sysDump(Strings.formatText("STGMOD.GridGetTbl4Ins: Instance $1 unknown in currently processed definition.", argIns));
        }
        String lin = Strings.getLine(stgmod.getInsstm(), idx);
        int pos = Strings.pos(lin, TAB);
        if (pos > 0) {
            lin = Strings.mid(lin, 1, pos - 1);
        }
        pos = Strings.pos(lin, " ");
        return Strings.trim(Strings.mid(lin, pos + 1));

    }
}
