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

namespace Sonex.Client.Controls.DataGridView
{
    public sealed partial class DataGridView
    {
        public string? GetValue(int rowIndex, Column column)
        {
            if (_ItemsSourceFiltred == null) return null;
            if (rowIndex < 0 || rowIndex >= _ItemsSourceFiltred.Count) return null;

            if (column == null) return null;
            if (column.PropertyInfo == null) return null;
            var item = _ItemsSourceFiltred[rowIndex];
            if(item == null) return null;
            var value = column.PropertyInfo.GetValue(item);
            var t = column.PropertyInfo.PropertyType.UnderlyingSystemType;



            if (t == typeof(string)) return ((string?)value)?.ToString() ?? null;

            if (t == typeof(int)) 
                return ((int?)value)?.ToString("n0") ?? null;

            if (t == typeof(uint)) 
                return ((uint?)value)?.ToString("n0") ?? null;

            if (t == typeof(long)) 
                return ((long?)value)?.ToString("n0") ?? null;

            if (t == typeof(ulong)) 
                return ((ulong?)value)?.ToString("n0") ?? null;

            if (t == typeof(short)) 
                return ((short?)value)?.ToString("n0") ?? null;

            if (t == typeof(ushort)) 
                return ((short?)value)?.ToString("n0") ?? null;



            if (t == typeof(float)) 
                return ((float?)value)?.ToString("n2") ?? null;

            if (t == typeof(double)) 
                return ((double?)value)?.ToString("n2") ?? null;

            if (t == typeof(decimal)) 
                return ((decimal?)value)?.ToString("n2") ?? null;



            if (t == typeof(bool))
                return value == null ? null : ((bool)value ? "Yes" : "No");

            if (t == typeof(char))
                return ((char?)value)?.ToString() ?? null;

            if (t == typeof(DateTime))
                return ((DateTime?)value)?.ToString("dd-MM-yyyy HH:mm:ss") ?? null;

            if (t == typeof(DateOnly))
                return ((DateOnly?)value)?.ToString("dd-MM-yyyy") ?? null;

            if (t == typeof(TimeOnly))
                return ((TimeOnly?)value)?.ToString("HH:mm:ss") ?? null;

            if (t == typeof(TimeSpan))
                return value is TimeSpan ts ? ts.ToString(@"d\.hh\:mm\:ss").Replace(".","d ") : null;

            if (t == typeof(Guid))
                return ((Guid?)value)?.ToString("N") ?? null;

            if (t == typeof(byte[]))
                return value is byte[] bytes
                    ? Convert.ToHexString(bytes)
                    : null;

            return value?.ToString();
        }
    }
}
