using PlantBox.Client.Extensions;
using PlantBox.Shared.Communication.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PlantBox.Client.Forms
{
	[XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class HomeItem : ContentView
	{
        // Colors
        public static readonly Color BadColor = Color.FromHex("#FA3838");
        public static readonly Color GoodColor = Color.FromHex("#8ED959");
        public static readonly Color WarningColor = Color.FromHex("#FFC533");

        // Name
        public static readonly BindableProperty PlantNameProperty = BindableProperty.Create(nameof(PlantName), typeof(string), typeof(HomeItem));
        public string PlantName
        {
            get => (string)GetValue(PlantNameProperty);
            set => SetValue(PlantNameProperty, value);
        }

        // PlantType
        public static readonly BindableProperty PlantTypeProperty = BindableProperty.Create(nameof(PlantType), typeof(PlantType), typeof(HomeItem));
        public PlantType PlantType
        {
            get => (PlantType)GetValue(PlantTypeProperty);
            set => SetValue(PlantTypeProperty, value);
        }

        // PlantState
        public static readonly BindableProperty PlantStateProperty = BindableProperty.Create(nameof(PlantState), typeof(PlantState), typeof(HomeItem));
        public PlantState PlantState
        {
            get => (PlantState)GetValue(PlantStateProperty);
            set => SetValue(PlantStateProperty, value);
        }

		public HomeItem()
		{
			InitializeComponent();
		}

        protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            base.OnPropertyChanged(propertyName);

            if (propertyName == PlantNameProperty.PropertyName)
            {
                labelName.Text = PlantName;
            }
            if (propertyName == PlantTypeProperty.PropertyName && PlantType != PlantType.Default)
            {
                labelType.Text = PlantType.ToString();

                // Icon
                imageType.Source = ImageResourceExtension.GetImage($"Type.{PlantType}.png");
            }
            if (propertyName == PlantStateProperty.PropertyName && PlantState != PlantState.Default)
            {
                Color color;

                switch(PlantState)
                {
                    case PlantState.Bad:
                        color = BadColor;
                        break;
                    case PlantState.Warning:
                        color = WarningColor;
                        break;
                    default:
                        color = GoodColor;
                        break;
                }

                boxState.BackgroundColor = color;
            }
        }
    }
}