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

public class TypeBase
{
    public string Name { get; set; }
    public int Size { get; set; }//占用的字节数
    public string TypeClass { get; set; }
}
public class TypeSimple:TypeBase
{
    public int SwapSize { get; set; }
    public string IecName { get; set; }
    public int BitOffset { get; set; }
}

public class TypeArray : TypeBase
{
    public int NativeSize { get; set; }
    public string BaseType { get; set; }
    public string IecName { get; set; }
    public int[]MinRange; //数组的行值
    public int[]MaxRange;
    public int Rank { get; set; }//数组的维数
}

public class UserDefElement
{
    public string IecName { get; set; }
    public string Type { get; set; }
    public uint ByteOffset { get; set; }
    public string VarType { get; set; }
    public string Access { get; set; }
}

public class TypeUserDef : TypeBase
{
    private List<UserDefElement> userDefElements;

    public TypeUserDef()
    {
        userDefElements=new List<UserDefElement>();
    }
    public int NativeSize { get; set; }
    public string PouClass { get; set; }
    public string IecName { get; set; }
    public List<UserDefElement> UserDefElements { get => userDefElements; set => userDefElements = value; }
}

public class XmlVariable
{
    public string Name { get; set; }
    public string DisplayName
    {
        get
        {
            string s = Name;
            if (!string.IsNullOrEmpty(Comment))
            {
                s += $" <{Comment}>";
            }
            if (!string.IsNullOrEmpty(Type))
            {
                s += $" ({ParseTypeString(Type)})";
            }
            return s;
        }
    }
    public string Type { get; set; }
    public string Access { get; set; }
    public string Comment { get; set; }
    public bool IsLeaf { get; set; }//是否是叶子节点（没有子项）
    public List<XmlVariable> Children { get; set; } = new List<XmlVariable>();

    private static string ParseTypeString(string input)
    {
        if (!input.StartsWith("T_"))
            return input; // 不符合格式，原样返回

        string typePart = input.Substring(2); // 去掉 T_

        if (!typePart.StartsWith("ARRAY__"))
            return typePart; // 简单类型，如 BOOL、INT 等

        // 分解数组部分和元素类型
        int ofIndex = typePart.IndexOf("__OF_");
        if (ofIndex == -1)
            return typePart; // 格式错误，原样返回

        string arrayPart = typePart.Substring(0, ofIndex);
        string elementType = typePart.Substring(ofIndex + 5); // "OF_".Length == 3, "__OF_" 是 5 个字符

        // 解析数组维度
        string[] ranges = arrayPart.Split(new string[] { "__" },
                                  StringSplitOptions.RemoveEmptyEntries);
        if ((ranges.Length-1) % 2 != 0)
            return typePart; // 维度解析失败，原样返回

        List<string> dimensions = new List<string>();
        for (int i = 1; i < ranges.Length; i += 2)
        {
            string lower = ranges[i];
            string upper = ranges[i + 1];
            dimensions.Add($"{lower}..{upper}");
        }

        string dimensionStr = string.Join(",", dimensions);
        return $"ARRAY[{dimensionStr}] OF {elementType}";
    }
}
