using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using PhilExample.Utils;
using Xamarin.Forms;

namespace PhilExample.UI.Controls
{
    public class PhExEntry : Entry
    {
        public static readonly BindableProperty DefaultTextColorProperty =
           BindableProperty.Create(nameof(DefaultTextColor),
                                   returnType: typeof(Color),
                                   declaringType: typeof(PhExEntry),
                                   defaultValue: (Color)Entry.TextColorProperty.DefaultValue,
                                   propertyChanged: DefaultTextColorChanged);

        public static readonly BindableProperty DefaultPlaceholderProperty =
           BindableProperty.Create(nameof(DefaultPlaceholder),
                                   returnType: typeof(string),
                                   declaringType: typeof(PhExEntry),
                                   defaultValue: string.Empty,
                                   propertyChanged: DefaultPlaceholderChanged);

        public static readonly BindableProperty ErrorPlaceholderProperty =
           BindableProperty.Create(nameof(ErrorPlaceholder),
                                   returnType: typeof(string),
                                   declaringType: typeof(PhExEntry),
                                   defaultValue: string.Empty);//,
                                   //propertyChanged: ErrorPlaceholderChanged);

        public static readonly BindableProperty IsErrorStateProperty = 
            BindableProperty.Create(nameof(IsErrorState),
                                    returnType: typeof(bool),
                                    declaringType: typeof(PhExEntry),
                                    defaultBindingMode: BindingMode.TwoWay,
                                    defaultValue: false,
                                    propertyChanged: IsErrorStateChanged);

        private static void DefaultTextColorChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is PhExEntry phExE && newValue is Color defaultTextColor)
            {
                phExE.TextColor = defaultTextColor;
            }
        }

        private static void DefaultPlaceholderChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is PhExEntry phExE && newValue is string defaultPlaceholder)
            {
                phExE.Placeholder = defaultPlaceholder;
            }
        }

        private static void IsErrorStateChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is PhExEntry phExE && newValue is bool isErrorState)
            {
                if (isErrorState)
                    SetErrorState(phExE);
                else
                    RemoveErrorState(phExE);
            }
        }

        public Color DefaultTextColor
        {
            get => (Color)GetValue(DefaultTextColorProperty);
            set => SetValue(DefaultTextColorProperty, value);
        }

        public string DefaultPlaceholder
        {
            get => (string)GetValue(DefaultPlaceholderProperty);
            set => SetValue(DefaultPlaceholderProperty, value);
        }

        public string ErrorPlaceholder
        {
            get => (string)GetValue(ErrorPlaceholderProperty);
            set => SetValue(ErrorPlaceholderProperty, value);
        }

        public bool IsErrorState
        {
            get => (bool)GetValue(IsErrorStateProperty);
            set => SetValue(IsErrorStateProperty, value);
        }

        public PhExEntry()
        {
            Effects.Add(Effect.Resolve(nameof(EffectConstants.EffectGroup) + ".RemoveUnderlineEntryEffect"));
        }

        private static void SetErrorState(PhExEntry phExE)
        {
            phExE.TextColor = App.GetResource<Color>("Color_Error");
            phExE.Placeholder = phExE.ErrorPlaceholder;
            phExE.PlaceholderColor = App.GetResource<Color>("Color_Error");
        }

        private static void RemoveErrorState(PhExEntry phExE)
        {
            phExE.TextColor = phExE.DefaultTextColor;
            phExE.Placeholder = phExE.DefaultPlaceholder;
            phExE.PlaceholderColor = (Color)Entry.PlaceholderColorProperty.DefaultValue;
        }

    }
}
