using System;
using Pixiview.UI.Theme;
using Xamarin.Forms;

namespace Pixiview.UI
{
    public class NavigationTitle : ContentView
    {
        public static readonly BindableProperty TitleProperty = BindableProperty.Create(
            nameof(Title), typeof(string), typeof(NavigationTitle));
        public static readonly BindableProperty IsLeftButtonVisibleProperty = BindableProperty.Create(
            nameof(IsLeftButtonVisible), typeof(bool), typeof(NavigationTitle));
        public static readonly BindableProperty IsRightButtonVisibleProperty = BindableProperty.Create(
            nameof(IsRightButtonVisible), typeof(bool), typeof(NavigationTitle));
        public static readonly BindableProperty RightButtonTextProperty = BindableProperty.Create(
            nameof(RightButtonText), typeof(string), typeof(NavigationTitle));

        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }
        public bool IsLeftButtonVisible
        {
            get => (bool)GetValue(IsLeftButtonVisibleProperty);
            set => SetValue(IsLeftButtonVisibleProperty, value);
        }
        public bool IsRightButtonVisible
        {
            get => (bool)GetValue(IsRightButtonVisibleProperty);
            set => SetValue(IsRightButtonVisibleProperty, value);
        }
        public string RightButtonText
        {
            get => (string)GetValue(RightButtonTextProperty);
            set => SetValue(RightButtonTextProperty, value);
        }

        public event EventHandler LeftButtonClicked;
        public event EventHandler RightButtonClicked;

        public NavigationTitle()
        {
            BindingContext = this;

            var left = GetLinkButton(nameof(IsLeftButtonVisible),
                padding: StyleDefinition.HorizonRight10);
            left.SetDynamicResource(Button.FontFamilyProperty, ThemeBase.IconRegularFontFamily);
            left.SetDynamicResource(Button.TextProperty, ThemeBase.IconLeft);
            left.Clicked += Left_Clicked;

            var title = new Label();
            title.SetDynamicResource(StyleProperty, ThemeBase.TitleLabel);
            title.SetBinding(Label.TextProperty, nameof(Title));
            Grid.SetColumnSpan(title, 3);

            var right = GetLinkButton(nameof(IsRightButtonVisible),
                text: nameof(RightButtonText),
                horizon: LayoutOptions.End,
                padding: StyleDefinition.HorizonLeft10);
            right.Clicked += Right_Clicked;
            Grid.SetColumn(right, 2);

            Content = new Grid
            {
                ColumnDefinitions =
                {
                    new ColumnDefinition { Width = GridLength.Auto },
                    new ColumnDefinition(),
                    new ColumnDefinition { Width = GridLength.Auto }
                },
                ColumnSpacing = 0,
                Children = { title, left, right }
            };
        }

        private void Left_Clicked(object sender, EventArgs e)
        {
            LeftButtonClicked?.Invoke(sender, e);
        }

        private void Right_Clicked(object sender, EventArgs e)
        {
            RightButtonClicked?.Invoke(sender, e);
        }

        private Button GetLinkButton(string visibility, string text = null, LayoutOptions horizon = default, Thickness padding = default)
        {
            var button = new Button
            {
                VerticalOptions = LayoutOptions.Center,
                HorizontalOptions = horizon,
                Padding = padding
            };
            button.SetBinding(IsVisibleProperty, visibility);
            if (text != null)
            {
                button.SetBinding(Button.TextProperty, text);
            }
            button.SetDynamicResource(StyleProperty, ThemeBase.TitleButton);
            return button;
        }
    }
}

