using SebastianTabViewRepro.Portable.Converters;
using Xamarin.Forms;

namespace SebastianTabViewRepro.Portable.Controls
{
    public class CustomTabViewHeader : Grid
    {
        public CustomTabViewHeader()
        {
            // ************ Root Grid configuration *************** //
            
            // The tab's Background color
            this.SetBinding(BackgroundColorProperty,
                new TemplateBinding("IsSelected", BindingMode.Default,
                    new BoolToColorConverter
                    {
                        SelectedColor = Color.Transparent,
                        UnselectedColor = Color.Green
                    }));

            // **************** END SECTION ******************** //


            // ************ BoxView configuration *************** //

            // The BoxView that underlines the Header text
            var bv = new BoxView
            {
                BackgroundColor = Color.Orange,
                HeightRequest = 2,
                VerticalOptions = LayoutOptions.End
            };

            bv.SetBinding(IsVisibleProperty, new TemplateBinding("IsSelected"));

            // **************** END SECTION ******************** //


            // ************ Label configuration *************** //

            // the Label that displays the header text
            var label = new Label
            {
                HorizontalTextAlignment = TextAlignment.Center,
                Margin = new Thickness(0, 0, 0, 8)
            };

            label.SetBinding(Label.TextProperty, new TemplateBinding("Text"));

            label.SetBinding(Label.TextColorProperty, 
                new TemplateBinding("IsSelected", BindingMode.Default, 
                new BoolToColorConverter
                {
                    SelectedColor = Color.Green,
                    UnselectedColor = Color.Black
                }));

            // **************** END SECTION ******************** //


            // ************ Add UI elements to Root Grid to complete construction of the header *************** //

            this.Children.Add(bv);
            this.Children.Add(label);
        }
    }
}
