using FFImageLoading.Svg.Forms;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;

namespace PhilExample.UI.Views
{
    internal class PopupTitleView : StackLayout
    {
        public static readonly BindableProperty TitleProperty =
              BindableProperty.Create(nameof(Title),
                                      returnType: typeof(string),
                                      declaringType: typeof(PopupTitleView),
                                      defaultValue: string.Empty,
                                      propertyChanged: TitleChanged);

        public static readonly BindableProperty CloseCommandProperty =
              BindableProperty.Create(nameof(CloseCommand),
                                      returnType: typeof(ICommand),
                                      declaringType: typeof(PopupTitleView),
                                      defaultValue: null,
                                      propertyChanged: CloseCommandChanged);

        private static void TitleChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is PopupTitleView ptv && newValue is string title)
            {
                ptv.Title = title;
                ptv._lbl_Title.Text = title;
            }
        }

        private static void CloseCommandChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is PopupTitleView ptv && newValue is ICommand closeCommand)
            {
                ptv.CloseCommand = closeCommand;
                ptv._btn_CloseTGR.Command = closeCommand;
            }
        }

        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        public ICommand CloseCommand
        {
            get => (ICommand)GetValue(CloseCommandProperty);
            set => SetValue(CloseCommandProperty, value);
        }



        private readonly Label _lbl_Title;
        private readonly TapGestureRecognizer _btn_CloseTGR = new TapGestureRecognizer();

        public PopupTitleView()
        {
            HeightRequest = 100;
            Orientation = StackOrientation.Horizontal;

            _lbl_Title = GetTitleLabel();
            Children.Add(_lbl_Title);
            Children.Add(GetCloseButton());
        }

        private Label GetTitleLabel()
        {
            return new Label()
            {
                FontFamily = "NunitoExtraBold",
                FontSize = 24,
                TextColor = App.GetResource<Color>("Color_TextTitle"),
                HorizontalOptions = LayoutOptions.Start,
                VerticalOptions = LayoutOptions.Center
            };
        }

        private Frame GetCloseButton()
        {
            var f = new Frame()
            {
                BackgroundColor = Color.White,
                Padding = 2,
                CornerRadius = 5,
                HasShadow = false,
                HeightRequest = 32,
                WidthRequest = 32,
                HorizontalOptions = LayoutOptions.EndAndExpand,
                VerticalOptions = LayoutOptions.Center
            };

            var img = new SvgCachedImage()
            {
                Source = App.GetResource<string>("Img_XBaddne"),
                HeightRequest = 22,
                Margin = 0,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center
            };

            f.Content = img;
            f.GestureRecognizers.Add(_btn_CloseTGR);
            return f;
        }
    }
}
