﻿using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace XamJam.Nav.Tab
{
    public class SexyTabbedView : ContentView
    {
        private static readonly Color TabBackgroundColor = Color.White;
        private const double TabVerticalSpacing = 3;
        private readonly ContentView currentTabView = new ContentView();

        public SexyTabbedView(bool areTabsOnTop, IReadOnlyList<TabDestination> children)
        {
            var grid = new Grid
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                ColumnDefinitions = { new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) } }
            };
            var tabGrid = new Grid
            {
                RowDefinitions = { new RowDefinition { Height = new GridLength(1, GridUnitType.Star) } },
                BackgroundColor = TabBackgroundColor
            };

            // place the tab row on top or bottom
            int contentRowIndex;
            if (areTabsOnTop)
            {
                contentRowIndex = 1;
                grid.RowDefinitions = new RowDefinitionCollection
                {
                    new RowDefinition {Height = GridLength.Auto},
                    new RowDefinition {Height = new GridLength(1, GridUnitType.Star)}
                };
                grid.Children.Add(tabGrid, 0, 0);
                //put a little padding above the tab grid, so the buttons have a little spacing above them
                tabGrid.Padding = new Thickness(0, TabVerticalSpacing, 0, 0);
            }
            else
            {
                contentRowIndex = 0;
                grid.RowDefinitions = new RowDefinitionCollection
                {
                    new RowDefinition {Height = new GridLength(1, GridUnitType.Star)},
                    new RowDefinition {Height = GridLength.Auto}
                };
                grid.Children.Add(tabGrid, 0, 1);
                //put a little padding below the tab grid, so the buttons have a little spacing beneath them
                tabGrid.Padding = new Thickness(0, 0, 0, TabVerticalSpacing);
            }

            // create all the tabs. Each tab goes in a 'tabGrid' and visually has a XamSvg & Label. So create the grid with as many columns as needed. Forget scrolling for now, we can
            // wrap this in a horizontal ScrollView or allow a autogenerated Tile/More button later.

            var tabGridColumns = new ColumnDefinitionCollection();
            for (var i = 0; i < children.Count; i++)
            {
                tabGridColumns.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            }
            tabGrid.ColumnDefinitions = tabGridColumns;

            // now create the tabs
            var column = 0;
            foreach (var itemViewModel in children)
            {
                tabGrid.Children.Add(new SexyTabbedItemView(itemViewModel), column++, 0);
            }

            // show the initial tab (View of actual data)
            grid.Children.Add(currentTabView, 0, contentRowIndex);
            Show(children[0]);

            SizeChanged += OnSizeChanged;
            Content = grid;
        }

        public void Show(TabDestination showMe)
        {
            currentTabView.Content = showMe.View;
        }

        private void OnSizeChanged(object sender, EventArgs eventArgs)
        {
            // LODO: Calculate Svg Size and maybe even Font Size for Labels here & update viewModel
        }
    }
}