using System;
using System.Collections.Generic;
using System.Text;
using PhilExample.UI.Views;
using Xamarin.Forms;

namespace PhilExample.UI.Controls
{
    public class PhExFrame : Frame, ILifecycledView
    {
        bool _isFocused = false;
        bool _isError = false;

        public PhExFrame()
        {
            BackgroundColor = App.GetResource<Color>("Color_ControlBackground");
            Padding = 4;
            CornerRadius = 5;
            HasShadow = false;
        }

        public void OnAppearing()
        {
            RecursiveCall(Content, Subscribe);
        }

        public void OnDisappearing()
        {
            RecursiveCall(Content, Unsubscribe);
        }

        private void OnChildViewFocusChanged(object sender, FocusEventArgs e)
        {
            if (e.IsFocused)
                SetFocusedState();
            else
                SetUnfocusedState();
        }

        private void Child_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == PhExEntry.IsErrorStateProperty.PropertyName)
            {
                bool isErrorState = ((PhExEntry)sender).IsErrorState;
                if (isErrorState)
                    SetIsErrorState();
                else
                    RemoveErrorState();
            }    
        }

        private void SetFocusedState()
        {
            _isFocused = true;
            BorderColor = App.GetResource<Color>("Color_Primary");
        }

        private void SetUnfocusedState()
        {
            _isFocused = false;

            if (!_isError)
                BorderColor = App.GetResource<Color>("Color_ControlBackground");
        }

        private void SetIsErrorState()
        {
            _isError = true;
            BorderColor = App.GetResource<Color>("Color_Error");
        }

        private void RemoveErrorState()
        {
            _isError = false;
            if (_isFocused)
                SetFocusedState();
            else
                SetUnfocusedState();
        }



        private void RecursiveCall(View view, Action<PhExEntry> onPhExEntryFound)
        {
            if (view is PhExEntry e)
            {
                onPhExEntryFound(e);
            }
            else if (view is Layout<View> layoutT)
            {
                foreach (View child in layoutT.Children)
                    RecursiveCall(child, onPhExEntryFound);
            }
            else if (view is Layout layout)
            {
                foreach (Element child in layout.Children)
                    if (child is View v)
                        RecursiveCall(v, onPhExEntryFound);
            }
        }

        private void Subscribe(PhExEntry e)
        {
            e.PropertyChanged += Child_PropertyChanged; //used for IsErrorState
            e.Focused += OnChildViewFocusChanged;
            e.Unfocused += OnChildViewFocusChanged;
        }

        private void Unsubscribe(PhExEntry e)
        {
            e.PropertyChanged -= Child_PropertyChanged; //used for IsErrorState
            e.Focused -= OnChildViewFocusChanged;
            e.Unfocused -= OnChildViewFocusChanged;
        }
    }
}
