﻿using System;
using System.Diagnostics;
using System.Threading.Tasks;
using AvvmTools;
using AvvmTools.Controls;
using Xamarin.Forms;
using XfpDemo1.AppStart;
using XfpDemo1.Models;
using XfpDemo1.Themes;
using XfpDemo1.ViewModels.PageNavigation;
using ContentPage = AvvmTools.ContentPage;

namespace XfpDemo1.Views.PageNavigation
{
    public class Page1 : ContentPage
    {
        private Request _request;
        private Label _labelTitle;
        private Label _labelSomeText;
        private Entry _entrySomeText;
        private AvvmTappable _buttonNextPage;
        private Label _labelNextPage;
        private AvvmTappable _footer;

        public async Task<Response> Execute()
        {
            return await Execute(new Request());
        }

        public async Task<Response> Execute(Request request)
        {
            var response = new Response();
            _request = request;
            var host = HostIoc.Get<Host>();
            var theme = host.Theme.PageNavDemoPage;

            // Setup scaffolding
            Title = "Page 1";
            BackgroundColor = host.Theme.PageNavDemoPage.BackgroundColor;
            Content = new StackLayout {
                VerticalOptions = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Padding = new Thickness(20),
                Children = {
                    (_labelTitle = new Label {
                        HorizontalTextAlignment = TextAlignment.Start,
                        FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                        Text = $"Hello {this.Title}"
                    }),
                    new Spacer(0, 20, 0, 0),
                    new StackLayout {
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        Orientation = StackOrientation.Horizontal,
                        Children = {
                            (_labelSomeText = new Label {
                                VerticalOptions = LayoutOptions.Center,
                                HorizontalOptions = LayoutOptions.Start,
                                HorizontalTextAlignment = TextAlignment.Start,
                                TextColor = theme.TitleTextColor,
                                Text = $"Some Text:"
                            }),
                            (_entrySomeText = new Entry {
                                VerticalOptions = LayoutOptions.Center,
                                HorizontalOptions = LayoutOptions.FillAndExpand,
                            }),
                        }
                    },
                    new Spacer(0, 20, 0, 0),
                    (_buttonNextPage = new AvvmTappable {
                        Content = _labelNextPage = new Label {
                            HorizontalTextAlignment = TextAlignment.Start,
                            Text = "Next Page",
                        },
                        Command = new Command(OnNextPageTapped)
                    }),
                    new Spacer(0, 50, 0, 0),
                    (_footer = new AvvmTappable()),
                }
            };

            // Bindings
            _entrySomeText.SetBinding(
                Entry.TextProperty,
                new Binding(
                    "DemoData.SomeText",
                    BindingMode.TwoWay, null, null, null,
                    null)
            );

            // BindingContext
            this.BindingContext = _request.Vm ?? null;

            // Event handlers
            _entrySomeText.TextChanged += EntrySomeTextOnTextChanged;

            await this.RefreshPage();

            response.Page = this;
            return response;
        }

        public class Request
        {
            public Page1Vm Vm { get; set; }
            public Action OnRefreshPage { get; set; }
            public Action<TextChangedEventArgs> OnSomeTextChanged { get; set; }
            public Action OnNextPageTapped { get; set; }
            public Action OnDarkThemeTapped { get; set; }
            public Action OnLightThemeTapped { get; set; }
        }

        public class Response
        {
            public Page Page { get; set; }
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            await this.RefreshPage();
        }

        public async Task RefreshPage()
        {
            var host = HostIoc.Get<Host>();
            var theme = host.Theme.PageNavDemoPage;

            this.BackgroundColor = theme.BackgroundColor;
            _labelTitle.TextColor = theme.TitleTextColor;
            _labelSomeText.TextColor = theme.TitleTextColor;
            _labelNextPage.TextColor = theme.NavButtonTextColor;
            if (Device.OS == TargetPlatform.Android)
                _entrySomeText.TextColor = theme.TitleTextColor;

            _footer.Content = (await new Footer()
                .Execute(new Footer.Request {
                    Theme = theme,
                    OnDarkThemeTapped = OnDarkThemeTapped,
                    OnLightThemeTapped = OnLightThemeTapped,
                })).Layout;

            //await host.NavPage.RefreshPage();
            _request.OnRefreshPage?.Invoke();
        }

        private void EntrySomeTextOnTextChanged(object sender, TextChangedEventArgs args)
        {
            _request.OnSomeTextChanged?.Invoke(args);
        }

        private void OnNextPageTapped()
        {
            _request.OnNextPageTapped?.Invoke();
        }

        private async void OnDarkThemeTapped()
        {
            await this.RefreshPage();
            _request.OnDarkThemeTapped?.Invoke();
        }

        private async void OnLightThemeTapped()
        {
            await this.RefreshPage();
            _request.OnLightThemeTapped?.Invoke();
        }
    }
}