using System.Dynamic;
using System.Text;
using System.Text.Json;

namespace File_Watcher.Helpers.EDA;

public class DCP
{

    internal static string GetEdaObjectToHtml(string edaObjectFile, Common common)
    {
        StringBuilder result = new();
        string title = string.Concat(Path.GetFileName(edaObjectFile), " - ", common.Source);
        _ = result.AppendLine("<!DOCTYPE html>");
        _ = result.AppendLine("<html lang=\"en\">");
        _ = result.AppendLine("<head>");
        _ = result.AppendLine("<style>table, th, td{border:1px solid black;} td{padding:2px}</style>");
        _ = result.Append("<title>").Append(title).AppendLine("</title>");
        _ = result.AppendLine("</head>");
        _ = result.AppendLine("<body>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("Unit Name").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Container Name").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Configuration State").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Configuration Productive State").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("LogisticsEquipmentAlias").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Source").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("StoragePath(s)").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("StartTimeFormat").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Filename").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<td nowrap>").Append(common.UnitName).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(common.ContainerName).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(common.ConfigurationState).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(common.ConfigurationProductiveState).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(common.LogisticsEquipmentAlias).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(common.Source).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(string.Join("</br >", common.StoragePaths)).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(common.StartTimeFormat).AppendLine("</td>");
        _ = result.Append("<td nowrap>").Append(common.Filename).AppendLine("</td>");
        _ = result.AppendLine("</tr>");
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("Use").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Order").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Key").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Placeholder").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        foreach (string[] item in common.LogisticsAttributes)
        {
            if (item.Length > 2 && !item[2].StartsWith("Z_"))
            {
                _ = result.AppendLine("<tr>");
                foreach (string value in item)
                    _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
                _ = result.AppendLine("</tr>");
            }
        }
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("ID").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Prefix").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        foreach (string[] item in common.LogisticsColumns)
        {
            _ = result.AppendLine("<tr>");
            foreach (string value in item)
                _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
            _ = result.AppendLine("</tr>");
        }
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("Use").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Order").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("FullName").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Alias").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("HardWareId").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Description").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Formula").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Virtual").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Column#").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        foreach (string?[] item in common.Parameters)
        {
            _ = result.AppendLine("<tr>");
            foreach (string? value in item)
                _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
            _ = result.AppendLine("</tr>");
        }
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("Parent Name").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Name").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("ParameterName").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Formula").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        foreach (string[] item in common.GeneralTriggers)
        {
            _ = result.AppendLine("<tr>");
            foreach (string value in item)
                _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
            _ = result.AppendLine("</tr>");
        }
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("Name").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Rule").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("ResolveGlobalVariableBeforeTrigger").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        foreach (string[] item in common.StartTriggersDCP)
        {
            _ = result.AppendLine("<tr>");
            foreach (string value in item)
                _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
            _ = result.AppendLine("</tr>");
        }
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("Name").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Fixed").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Rule").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Scenario").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("DefaultJobIndex").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("DefaultCarrierIndex").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("DefaultSlotIndex").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("DataPool").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        foreach (string?[] item in common.LogisticsTriggers)
        {
            if (item is null)
                continue;
            _ = result.AppendLine("<tr>");
            foreach (string? value in item)
                _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
            _ = result.AppendLine("<td>");
            _ = result.AppendLine("<table>");
            _ = result.AppendLine("<tr>");
            _ = result.Append("<th nowrap>").Append("KeyName").AppendLine("</th>");
            _ = result.Append("<th nowrap>").Append("ParameterName").AppendLine("</th>");
            _ = result.Append("<th nowrap>").Append("Formula").AppendLine("</th>");
            _ = result.AppendLine("</tr>");
            foreach (KeyValuePair<int, string[]> keyValuePair in common.LogisticsTriggersKeysKeyMapping)
            {
                _ = result.AppendLine("<tr>");
                foreach (string value in keyValuePair.Value)
                    _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
                _ = result.AppendLine("</tr>");
            }
            _ = result.AppendLine("</table>");
            _ = result.AppendLine("</td>");
            _ = result.AppendLine("<td>");
            _ = result.AppendLine("<table>");
            _ = result.AppendLine("<tr>");
            _ = result.Append("<th nowrap>").Append("LogisticsKey").AppendLine("</th>");
            _ = result.Append("<th nowrap>").Append("Source").AppendLine("</th>");
            _ = result.Append("<th nowrap>").Append("MappedParameterName").AppendLine("</th>");
            _ = result.Append("<th nowrap>").Append("Formula").AppendLine("</th>");
            _ = result.AppendLine("</tr>");
            foreach (KeyValuePair<int, List<string[]>> keyValuePair in common.LogisticsTriggersCallDefinitionAttributes)
            {
                foreach (string[] values in keyValuePair.Value)
                {
                    if (values.Length > 0 && !values[0].StartsWith("Z_"))
                    {
                        _ = result.AppendLine("<tr>");
                        foreach (string value in values)
                            _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
                        _ = result.AppendLine("</tr>");
                    }
                }
            }
            _ = result.AppendLine("</table>");
            _ = result.AppendLine("</td>");
            _ = result.AppendLine("</tr>");
        }
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("<table>");
        _ = result.AppendLine("<tr>");
        _ = result.Append("<th nowrap>").Append("Name").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("Rule").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("ResolveGlobalVariableBeforeTrigger").AppendLine("</th>");
        _ = result.Append("<th nowrap>").Append("ResetGlobalVariablesAfterTrigger").AppendLine("</th>");
        _ = result.AppendLine("</tr>");
        foreach (string[] item in common.StopTriggersDCP)
        {
            _ = result.AppendLine("<tr>");
            foreach (string value in item)
                _ = result.Append("<td nowrap>").Append(value).AppendLine("</td>");
            _ = result.AppendLine("</tr>");
        }
        _ = result.AppendLine("</table>");
        _ = result.AppendLine("<hr>");
        _ = result.AppendLine("</body>");
        _ = result.AppendLine("</html>");
        return result.ToString();
    }

    private static string? ReplaceNull(object @object)
    {
        if (@object is null)
            return string.Empty;
        else
            return @object.ToString();
    }

    private static void TextResolveEntry(StringBuilder result, dynamic expandoObject, int level, string super, int? i, bool group)
    {
        level += 1;
        foreach (dynamic entry in expandoObject)
        {
#pragma warning disable IL2026, IL3050
            if (entry.Value is ExpandoObject)
                TextResolveEntry(result, entry.Value, level, string.Concat(super, " : ", entry.Key), i, group);
            else
            {
                if (entry.Value is not ICollection<object>)
                {
                    if (i is null)
                        result.Append(level).Append(") ").Append(super).Append(" : ").Append(entry.Key).Append("--->").AppendLine(ReplaceNull(entry.Value));
                    else
                        result.Append(level).Append(") ").Append(super).Append(" : ").Append(entry.Key).Append("[").Append(i.Value).Append("]--->").AppendLine(ReplaceNull(entry.Value));
                }
                else
                {
                    if (!group)
                    {
                        for (i = 0; i.Value < entry.Value.Count; i++)
                        {
                            if (entry.Value[i.Value] is ExpandoObject)
                                TextResolveEntry(result, entry.Value[i.Value], level, string.Concat(super, " : ", entry.Key), i.Value, group);
                            else
                                result.Append(level).Append(") ").Append(super).Append(" : ").Append(entry.Key).Append("[").Append(i.Value).Append("]--->").AppendLine(ReplaceNull(entry.Value[i.Value]));
                        }
                    }
                    else
                    {
                        for (i = 0; i.Value < entry.Value.Count; i++)
                        {
                            if (entry.Value[i.Value] is not ExpandoObject)
                                result.Append(level).Append(") ").Append(super).Append(" : ").Append(entry.Key).Append("[").Append(i.Value).Append("]--->").AppendLine(ReplaceNull(entry.Value[i.Value]));
                        }
                        for (i = 0; i.Value < entry.Value.Count; i++)
                        {
                            if (entry.Value[i.Value] is ExpandoObject)
                                TextResolveEntry(result, entry.Value[i.Value], level, string.Concat(super, " : ", entry.Key), i.Value, group);
                        }
                    }
                    i = null;
                }
            }
#pragma warning restore IL2026, IL3050
        }
        level -= 1;
        if (level == 0)
        { }
    }

    internal static string GetText(string edaObjectFile, Common common, string json)
    {
        StringBuilder result = new();
        if (result.Length > 0) //Skipping because System.Text.Json changed the way Expando works 
        {
#pragma warning disable IL2026, IL3050
            string title = string.Concat(Path.GetFileName(edaObjectFile), " - ", common.Source);
            dynamic? expandoObject = JsonSerializer.Deserialize<ExpandoObject>(json);
            _ = result.AppendLine(title);
            _ = result.AppendLine("Loop -> \"Normal\"");
            _ = result.AppendLine(edaObjectFile);
            _ = result.AppendLine();
            TextResolveEntry(result, expandoObject, 0, string.Empty, null, group: false);
            _ = result.AppendLine();
            _ = result.AppendLine();
            _ = result.AppendLine();
            _ = result.AppendLine(title);
            _ = result.AppendLine("Loop -> \"Grouped\"");
            _ = result.AppendLine(edaObjectFile);
            _ = result.AppendLine();
            TextResolveEntry(result, expandoObject, 0, string.Empty, null, group: true);
#pragma warning restore IL2026, IL3050
        }
        return result.ToString();
    }

    internal static string GetEdaObjectToDMSGridFormat(string edaObjectFile, Common common, bool useAlias)
    {
        StringBuilder result = new();
        if (string.IsNullOrEmpty(edaObjectFile))
        { }
        string? value;
        string[] segments;
        int? recordStart = null;
        bool hasRecordStart = false;
        string name = string.Concat(common.LogisticsEquipmentAlias, "_", common.ContainerName);
        _ = result.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        _ = result.Append("<Format Name=\"").Append(name).Append("\">").AppendLine();
        _ = result.AppendLine("  <General RecordStart=\"\" RecordLength=\"0\" ReadCommand=\"\" DecimalSeparator=\".\">");
        _ = result.AppendLine("    <RecordTerminator>&lt;13&gt;&lt;10&gt;</RecordTerminator>");
        _ = result.AppendLine("    <ColumnSeparator>&lt;9&gt;</ColumnSeparator>");
        _ = result.AppendLine("  </General>");
        _ = result.AppendLine("  <Fields>");
        for (int i = 0; i < common.Parameters.Count; i++)
        {
            if (common.Parameters[i][3] == "RECORD_START")
            {
                hasRecordStart = true;
                break;
            }
        }
        for (int i = 0; i < common.Parameters.Count; i++)
        {
            if (recordStart is null && common.Parameters[i][3] == "RECORD_START")
                recordStart = i;
            if ((!hasRecordStart || (hasRecordStart && recordStart.HasValue)) && common.Parameters[i][0] == "True")
            {
                if (useAlias && !string.IsNullOrEmpty(common.Parameters[i][3]))
                {
                    value = common.Parameters[i][3];
                    value ??= string.Empty;
                    name = value.Replace("'", string.Empty);
                }
                else
                {
                    value = common.Parameters[i][2];
                    value ??= string.Empty;
                    segments = value.Split('/');
                    name = segments[^1];
                }
                _ = result.Append("    <Field Name=\"").Append(name).Append("\" ColumnNumber=\"").Append(i + common.LogisticsColumns.Count + 2).Append("\" StartPosition=\"\" Length=\"\" DataType=\"Text\" NullReplacement=\"\" />").AppendLine();
            }
        }
        _ = result.AppendLine("  </Fields>");
        _ = result.AppendLine("  <Conditions>");
        _ = result.AppendLine("    <Column />");
        _ = result.AppendLine("    <Row>");
        _ = result.AppendLine("      <Condition>");
        _ = result.AppendLine("        <SkipHeaderCount>7</SkipHeaderCount>");
        _ = result.AppendLine("        <SkipRowFilter>0</SkipRowFilter>");
        _ = result.AppendLine("        <SkipRowValue>");
        _ = result.AppendLine("        </SkipRowValue>");
        _ = result.AppendLine("      </Condition>");
        _ = result.AppendLine("    </Row>");
        _ = result.AppendLine("  </Conditions>");
        _ = result.AppendLine("</Format>");
        return result.ToString();
    }

    internal static string GetEdaObjectToAPCParameter(string edaObjectFile, Common common)
    {
        StringBuilder result = new();
        if (string.IsNullOrEmpty(edaObjectFile))
        { }
        string? value;
        string parameter;
        string[] segments;
        string parameterSub35;
        _ = result.AppendLine("ExportFileVersion=1.0.6");
        _ = result.AppendLine("ExportFromTabsheet=Para");
        _ = result.AppendLine("FieldName\tLongName\tMatchMode\tEquipment\tName\tPdsfNameConvCol\tPdsfNameDataType\tType\tChamberInfo\tUnifiedPara\tDateFormat\tUsername\tId\tWorkcenterId\tSite\tArea\tWorkcenter\tValidFrom\tValidTo");
        _ = result.AppendLine("TIME_PREV_DIFF\tTIME_PREV_DIFF\tEXACT\t*\tTIME_PREV_DIFF\t\tNUMERIC\tRUN\t\tTIME_PREV_DIFF\t\tPHARES\t95069\t4571\tMesa\t\t\t4/15/2020 6:10 PM");
        _ = result.AppendLine("TIME\tTIME\tEXACT\t*\tTIME\t\tNUMERIC\tRUN\t\tTIME\t\tPHARES\t95070\t4571\tMesa\t\t\t4/15/2020 6:10 PM");
        for (int i = 0; i < common.Parameters.Count; i++)
        {
            if (common.Parameters[i][0] == "True")
            {
                value = common.Parameters[i][2];
                value ??= string.Empty;
                segments = value.Split('/');
                parameter = segments[^1];
                if (parameter.Length < 35)
                    parameterSub35 = parameter;
                else
                    parameterSub35 = parameter[..35];
                _ = result.Append(parameterSub35).Append("\tDIVERSE\tEXACT\t*\t").Append(parameterSub35).AppendLine("\t\tNUMERIC\tRUN\t\t\t\tPHARES\t9000000012\t4571\tMesa\t\t\t4/15/2020 6:10 PM");
            }
        }
        _ = result.AppendLine();
        _ = result.AppendLine();
        _ = result.AppendLine();
        for (int i = 0; i < common.Parameters.Count; i++)
        {
            if (common.Parameters[i][0] == "True")
            {
                value = common.Parameters[i][2];
                value ??= string.Empty;
                segments = value.Split('/');
                parameter = segments[^1];
                _ = result.Append(parameter).Append("\tDIVERSE\tEXACT\t*\t").Append(parameter).AppendLine("\t\tNUMERIC\tRUN\t\t\t\tPHARES\t9000000012\t4571\tMesa\t\t\t4/15/2020 6:10 PM");
            }
        }
        return result.ToString();
    }

    internal static string GetEdaObjectToAPCRunKeyNumber(string edaObjectFile, Common common)
    {
        StringBuilder result = new();
        if (string.IsNullOrEmpty(edaObjectFile))
        { }
        string? value;
        string parameter;
        string[] segments;
        string parameterSub21;
        string parameterSub35;
        _ = result.AppendLine("ExportFileVersion=1.0.6");
        _ = result.AppendLine("ExportFromTabsheet=Run-Keynumbers");
        _ = result.AppendLine("FeatureName\tShortName\tChamber\tComments\tVarMode\tPara\tExclude0\tTrigOn1\tTrigOn2\tTrigOn3\tTrigOff1\tTrigOff2\tTrigOff3\tTrigDelay\tTrigNorm\tTrigNvl\tTrigTrg\tAddCondOn\tActive\tFilter1\tFilter2\tFilter3\tFilter4\tUnit\tId\tWorkcenterId\tSite\tArea\tWorkcenter\tUsername\tValidFrom\tValidTo");
        for (int i = 0; i < common.Parameters.Count; i++)
        {
            if (common.Parameters[i][0] == "True")
            {
                value = common.Parameters[i][2];
                value ??= string.Empty;
                segments = value.Split('/');
                parameter = segments[^1];
                if (parameter.Length < 35)
                    parameterSub35 = parameter;
                else
                    parameterSub35 = parameter[..35];
                if (parameter.Length < 21)
                    parameterSub21 = parameter;
                else
                    parameterSub21 = parameter[..21];
                _ = result.Append(parameterSub21).Append("_MIN\t").Append(parameterSub21).Append("_MIN\t\t\tMIN\t").Append(parameterSub35).AppendLine("\t0\tTIME\t=\tRUNSTART\tTIME\t=\tRUNEND\t\t\t\t\t\t1\t\t\t\t\t\t-1\t-1\t\t\t\tECPHARES\t5/2/2017 2:44 PM");
            }
        }
        _ = result.AppendLine();
        _ = result.AppendLine();
        _ = result.AppendLine();
        for (int i = 0; i < common.Parameters.Count; i++)
        {
            if (common.Parameters[i][0] == "True")
            {
                value = common.Parameters[i][2];
                value ??= string.Empty;
                segments = value.Split('/');
                parameter = segments[^1];
                _ = result.Append(parameter).Append("_MIN\t").Append(parameter).Append("_MIN\t\t\tMIN\t").Append(parameter).AppendLine("\t0\tTIME\t=\tRUNSTART\tTIME\t=\tRUNEND\t\t\t\t\t\t1\t\t\t\t\t\t-1\t-1\t\t\t\tECPHARES\t5/2/2017 2:44 PM");
            }
        }
        return result.ToString();
    }

}