using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace InABox.Mobile
{
    
    public interface IMobileToolItem
    {
        string Text { get; set; }
        ImageSource Image { get; set; }
        string Alert { get; set; }
        bool IsEnabled { get; set; }
        bool IsVisible { get; set; }
        int ID { get; set; }
        event EventHandler Clicked;
        void DoTap();
        int Row { get; set; }
        int Column { get; set; }
    }
    
    public class MobileToolItem : BindableObject, IMobileToolItem
    {
        
        public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MobileToolItem), "");
        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }

        public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(Image), typeof(ImageSource), typeof(MobileToolItem), null);
        public ImageSource Image
        {
            get => (ImageSource)GetValue(ImageProperty); 
            set =>SetValue(ImageProperty, value);
            
        }
        
        public static readonly BindableProperty AlertProperty = BindableProperty.Create(
            nameof(Alert), 
            typeof(string), 
            typeof(MobileToolItem), 
            null);
        public string Alert
        {
            get => (string)GetValue(AlertProperty);
            set
            {
                SetValue(AlertProperty, value);
                OnPropertyChanged(nameof(AlertVisible));
            }
        }

        public bool AlertVisible => !String.IsNullOrWhiteSpace(Alert);

        public static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
            nameof(BackgroundColor),
            typeof(Color),
            typeof(MobileToolItem),
            Color.White);

        public Color BackgroundColor
        {
            get => (Color)GetValue(BackgroundColorProperty);
            set => SetValue(BackgroundColorProperty, value);
        }

        public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
            nameof(TextColor),
            typeof(Color),
            typeof(MobileToolItem),
            Color.Black);

        public Color TextColor
        {
            get => (Color)GetValue(TextColorProperty);
            set => SetValue(TextColorProperty, value); 
        }
                
        public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(MobileToolItem), true);
        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set
            {
                var old = (bool)GetValue(IsEnabledProperty);
                SetValue(IsEnabledProperty, value);
                if (old != value)
                    DoEnabledChanged();
            }
        }
        
                        
        public static readonly BindableProperty IsVisibleProperty = BindableProperty.Create(nameof(IsVisible), typeof(bool), typeof(MobileToolItem), true);
        public bool IsVisible
        {
            get { return (bool)GetValue(IsVisibleProperty); }
            set
            {
                var old = (bool)GetValue(IsVisibleProperty);
                SetValue(IsVisibleProperty, value);
                if (old != value)
                    DoVisibleChanged();
            }
        }
        
                                
        public static readonly BindableProperty RowProperty = BindableProperty.Create(nameof(Row), typeof(Int32), typeof(MobileToolItem));
        public Int32 Row
        {
            get => (Int32)GetValue(RowProperty);
            set => SetValue(RowProperty, value);
        }
        
        public static readonly BindableProperty ColumnProperty = BindableProperty.Create(nameof(Column), typeof(Int32), typeof(MobileToolItem));

        public Int32 Column
        {
            get => (Int32)GetValue(ColumnProperty);
            set => SetValue(ColumnProperty, value);
        }

        public int ID { get; set; }
        
        public event EventHandler Clicked;
        
        public void DoTap()
        {
	        Clicked?.Invoke(this, EventArgs.Empty);
        }
        
                
        public event EventHandler VisibleChanged;

        public void DoVisibleChanged()
        {
            VisibleChanged?.Invoke(this, EventArgs.Empty);
        }
        
        public event EventHandler EnabledChanged;

        public void DoEnabledChanged()
        {
            EnabledChanged?.Invoke(this, EventArgs.Empty);
        }



    }

}