// Copyright © Selmo Technology GmbH. All Rights Reserved

using System.Text;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Shared.FormatConverters
{
    internal class CsvConversion
    {
        private const string Delimiter = ";";

        internal static string Convert<T>(List<T> content)
        {
            var sb = new StringBuilder();

            var properties = typeof(T).GetProperties();

            sb.AppendLine(string.Join(Delimiter, properties.Select(p => p.Name)));

            foreach (var item in content)
            {
                var propertyValues = properties.Select(p => p.GetValue(item)?.ToString() ?? "");
                sb.AppendLine(string.Join(Delimiter, propertyValues));
            }

            return sb.ToString();
        }
    }
}