using System;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Media.Imaging;
using InABox.Core;
using InABox.WPF;

namespace InABox.DynamicGrid
{
    public class DynamicTickColumn<T, P> : DynamicImageColumn where T : BaseObject, new() where P : struct
    {
        private readonly BitmapImage? Data = Wpf.Resources.tick.AsBitmapImage();

        private readonly BitmapImage? Header;
        private readonly BitmapImage? NoData;
        private readonly Expression<Func<T, P>> Property;

        public DynamicTickColumn(Expression<Func<T, P>> property, BitmapImage? header, BitmapImage? data, BitmapImage? nodata,
            ActionDelegate? action = null) : base(r => null, action)
        {
            Header = header;
            Data = data;
            NoData = nodata;
            Property = property;
            Image = GetImage;
            Filters = new[] { "(Blank)", "(Not Blank)" };

            FilterRecord = TickFilter;
        }

        private bool TickFilter(CoreRow row, string[] filter)
        {
            if (filter.Contains("(Blank)") && !HasData(row))
                return true;
            if (filter.Contains("(Not Blank)") && HasData(row))
                return true;
            return false;
        }

        private BitmapImage? GetImage(CoreRow? row)
        {
            if (row == null)
                return Header;

            return HasData(row) ? Data : NoData;
        }

        private bool HasData(CoreRow row)
        {
            var value = row.Get(Property);
            return !value.Equals(default(P));

            //bool hasdata = false;
            //if (typeof(P) == typeof(bool))
            //    hasdata = value.Equals(true);
            //else if (typeof(P) == typeof(DateTime))
            //    hasdata = !value.Equals(DateTime.MinValue);
            //return hasdata;
        }
    }
}