using Syncfusion.SfDataGrid.XForms;
using Xamarin.Forms;

namespace InABox.Mobile
{
    
    public enum MobileGridImageAspect
    {
        Fill,
        Fit
    }
    
    public class MobileGridImageColumn<TEntity> : MobileGridColumn<TEntity, ImageSource>
    {

        public MobileGridImageAspect Aspect { get; set; } = MobileGridImageAspect.Fit;
        
        public double Margin { get; set; }
        
        public ImageSource Header { get; set; }
        
        public MobileGridImageColumn() : base()
        {
            Width = 40;
            
        }
        
        protected override View HeaderContent()
        {
            if (Header != null)
                return new Image()
                {
                    Source = Header,
                    Margin = Margin
                };
            return base.HeaderContent();
        }
        
        public override GridColumn CreateColumn()
        {
            var result = CreateColumn<GridTemplateColumn>();
            
            result.CellTemplate = new DataTemplate(() =>
            {
                var image = new Image()
                {
                    Aspect = Aspect == MobileGridImageAspect.Fill
                        ? Xamarin.Forms.Aspect.AspectFill
                        : Xamarin.Forms.Aspect.AspectFit,
                    Margin = Margin,
                };
                image.SetBinding(Image.SourceProperty, ColumnName);
                return image;
            });
            
            return result;
        }
    }
}