using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Operations;

/// <summary>
/// Best-effort map from the MxGateway/Galaxy free-form attribute type name
/// (<c>GalaxyAttribute.data_type_name</c>, surfaced on <c>BrowseNode.DataType</c>)
/// to a ScadaBridge <see cref="DataType"/>. Used by the Secured Writes page to
/// pre-fill the data-type dropdown when a tag is picked. Unknown or blank names
/// return false so the operator's existing selection is left untouched. Galaxy
/// type names are free-form text, not a stable enum — keep this tolerant.
/// </summary>
internal static class SecuredWriteDataTypeMapper
{
    /// <summary>Attempts to map a Galaxy/MxGateway free-form type name to a ScadaBridge <see cref="DataType"/>.</summary>
    /// <param name="galaxyTypeName">The free-form Galaxy attribute type name, or null/blank.</param>
    /// <param name="dataType">The mapped data type when the mapping succeeds; otherwise <c>default</c>.</param>
    /// <returns><c>true</c> when the name was recognized; otherwise <c>false</c>.</returns>
    public static bool TryMap(string? galaxyTypeName, out DataType dataType)
    {
        dataType = default;
        if (string.IsNullOrWhiteSpace(galaxyTypeName)) return false;
        switch (galaxyTypeName.Trim().ToLowerInvariant())
        {
            case "boolean": case "bool":              dataType = DataType.Boolean;  return true;
            case "integer": case "int": case "int32": dataType = DataType.Int32;    return true;
            case "float":                             dataType = DataType.Float;    return true;
            case "double":                            dataType = DataType.Double;   return true;
            case "string":                            dataType = DataType.String;   return true;
            case "time":                              dataType = DataType.DateTime; return true;
            default:                                  return false;
        }
    }
}
