using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CobXmlSupport
{
    public class qualifieldhlp
    {
        private CobRow qualifiedRow;
        private List<CobRow> qualRows;
        private Dictionary<string, string> locIndexis;
        private string qfhCode, qfhInd, qfhRedInd ;
        private string[] indexStrs;

        public CobRow qualCobRow
        {
            get
            {
                return qualifiedRow;
            }
        }

        public string indexText
        {
            get { return qfhInd; }
        }

        public string indexRedText
        {
            get { return qfhRedInd; }
        }

        public string[] indexStrings
        {
            get { return indexStrs; }
        }

        public qualifieldhlp()
        {
            qualifiedRow = null;
            initQualifieldhlp();
        }

        private void initQualifieldhlp()
        {
            qualRows = null;
            qfhCode = string.Empty;
            locIndexis = null;
        }

        public qualifieldhlp(CobRow actRow, Dictionary<string, string> all_Inds)
        {
            initQualifieldhlp();
            qualifiedRow = actRow;
            initQualRows(ref all_Inds);
            generateFieldCode();
        }

        public void generateFieldCode()
        {
            generateFieldCode(false);
        }

        public void generateFieldCode(bool exp)
        {
            if (qualRows.Count > 0)
            {
                qfhCode = "";


                foreach (CobRow localCobRow in qualRows)
                {
                    if (qfhCode.Length > 0)
                    {
                        qfhCode += "           OF ";
                    }
                    if (exp) qfhCode += localCobRow.FieldName.Replace(S.ettingPrefix, S.ettingExpPrefix);
                    else qfhCode += localCobRow.FieldName;
                    if (localCobRow.isOccurs)
                    {
                        string tmpInd;
                        locIndexis.TryGetValue(localCobRow.FieldName, out tmpInd);
                        qfhCode += " *> ( " + tmpInd + " )";
                    }
                    qfhCode += "\r\n";
                }
                if (this.indexText.Length > 0)
                {
                    qfhCode += this.indexText ;
                }
            }
        }

        private void initLocIndexTxt()
        {
            qfhInd = "";
            qfhRedInd = "";
            indexStrs = null;
            if (locIndexis!=null && locIndexis.Count > 0)
            {
                int indCnt = 0;
                indexStrs = locIndexis.Values.ToArray();
                foreach (KeyValuePair<string, string> tmpInd in locIndexis.Reverse())
                {
                    if (qfhInd.Length == 0)
                    {
                        qfhInd = "           ( " + tmpInd.Value;
                    }
                    else
                    {
                        qfhInd += ",\r\n           " + tmpInd.Value;
                    }
                }
                qfhInd += " )\r\n";
                if (locIndexis.Count > 1)
                {
                    foreach (KeyValuePair<string, string> tmpInd in locIndexis.Reverse())
                    {
                        indCnt++;
                        if (indCnt == locIndexis.Count) break;
                        if (qfhRedInd.Length == 0)
                        {
                            qfhRedInd = "           ( " + tmpInd.Value;
                        }
                        else
                        {
                            qfhRedInd += ",\r\n           " + tmpInd.Value;
                        }
                    }
                    qfhRedInd += " )\r\n";

                }
            }

        }



        private void initQualRows(ref Dictionary<string, string> allInds)
        {
            CobRow tmpRow = qualifiedRow;
            if (qualRows == null)
            {
                qualRows = new List<CobRow>();
            }
            while (true)
            {
                if (tmpRow.isOccurs)
                {
                    if (locIndexis == null) locIndexis = new Dictionary<string, string>();
                    string indVarName = "";
                    if (!allInds.TryGetValue(tmpRow.FieldName, out indVarName))
                    {
                        indVarName = CobRow.checkLongNames(tmpRow.FieldName, 25) + "_ind";
                        allInds.Add(tmpRow.FieldName, indVarName);
                    }
                    locIndexis.Add(tmpRow.FieldName, indVarName);
                }
                qualRows.Add(tmpRow);
                if (tmpRow.LevelParent != null)
                {
                    tmpRow = tmpRow.LevelParent;
                }
                else break;
            }
            initLocIndexTxt();
           
        }

        public override string ToString()
        {
            return qfhCode;
        }

        public string ToExpString()
        {
            generateFieldCode(true);
            return qfhCode;
        }

    }
}
