using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PhilExample.VMs;
using Xamarin.Forms;

namespace PhilExample.UI.Pages
{
    public class StartupPage : ContentPage
    {
        private const string Name = "PhilExample";
        private readonly Label[] _nameLabels = new Label[Name.Length];

        private bool _isLoading = true;

        public StartupPage()
        {
            Shell.SetNavBarIsVisible(this, false);
            AddLayout();
            AddLabels();

            BindingContext = new StartupVM();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            RunLoadingAnimationAsync();
            await InitAsync();
        }

        private async Task InitAsync()
        {
            await ((BaseVM)BindingContext).OnAppearingAsync();
            _isLoading = false;
        }

        private async void RunLoadingAnimationAsync()
        {
            var initialY = _nameLabels[0].Y;
            Task[][] anims = new Task[3][];
            anims[0] = new Task[_nameLabels.Length];
            anims[1] = new Task[_nameLabels.Length];
            anims[2] = new Task[_nameLabels.Length];

            int a = 0;
            while (_isLoading)
            {
                for (int i = 0; i < _nameLabels.Length; i++)
                    anims[0][i] = _nameLabels[i].TranslateTo(0, initialY - 200, (uint)(1000 - (40 * (_nameLabels.Length - i))), Easing.SinInOut);
                await Task.WhenAll(anims[0]);

                for (int i = 0; i < _nameLabels.Length; i++)
                    anims[1][i] = _nameLabels[i].TranslateTo(0, initialY + 200, (uint)(500 + (20 * i)), Easing.SinInOut);
                await Task.WhenAll(anims[1]);

                for (int i = 0; i < _nameLabels.Length; i++)
                    anims[2][i] = _nameLabels[i].TranslateTo(0, initialY, (uint)(1000 - (40 * (_nameLabels.Length - i))), Easing.SinInOut);
                await Task.WhenAll(anims[2]);

                a = a == anims.Length - 1
                  ? 0
                  : a + 1;
            }
        }

        #region UI
        private void AddLayout()
        {
            Content = new StackLayout()
            {
                Padding = new Thickness(20, 0, 20, 0),
                Orientation = StackOrientation.Horizontal,
                BackgroundColor = App.GetResource<Color>("Color_Background"),
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.Center
            };
        }

        private void AddLabels()
        {
            for (int i = 0; i < Name.Length; i++)
            {
                _nameLabels[i] = new Label()
                {
                    Text = "" + Name[i],
                    TextColor = App.GetResource<Color>("Color_Primary"),
                    FontSize = 42,
                    FontFamily = "NunitoExtraBold",
                    HorizontalTextAlignment = TextAlignment.Center,
                    VerticalTextAlignment = TextAlignment.Center,
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                };

                ((StackLayout)Content).Children.Add(_nameLabels[i]);
            }
        }
        #endregion
    }
}