using C4IT.FASD.Cockpit.Communication;
using C4IT.Graphics;
using C4IT.Logging;
using FasdCockpitCommunication;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.SplashScreenView
{
    public partial class SplashScreenView : Window
    {
        private DispatcherTimer timer;
        private readonly IntroView _introView = new IntroView();

        public SplashScreenView()
        {
            InitializeComponent();
        }

        public void SetStatusText(string text)
        {
            try
            {
                Dispatcher.Invoke(() => StatusTextBlock.Text = text);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            timer = new DispatcherTimer(TimeSpan.FromSeconds(0.5), DispatcherPriority.Loaded, new EventHandler((sender, args) => UpdateLoadingDots()), Dispatcher.CurrentDispatcher);
        }

        private void UpdateLoadingDots()
        {
            try
            {
                if (string.IsNullOrEmpty(LoadingDotBlock.Text))
                    LoadingDotBlock.Text = ".";
                else if (LoadingDotBlock.Text == ".")
                    LoadingDotBlock.Text = "..";
                else if (LoadingDotBlock.Text == "..")
                    LoadingDotBlock.Text = "...";
                else
                    LoadingDotBlock.Text = string.Empty;

            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (!(e.NewValue is bool isVisible))
                    return;

                if (isVisible)
                    timer?.Start();
                else
                    timer?.Stop();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        #region MinimizeButton_Click

        private void MinimizeButton_Click()
        {

            try
            {
                (App.Current as App).notifyIcon.Visible = true;
                Close();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void MinimizeButton_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MinimizeButton_Click();
        }

        private void MinimizeButton_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
        {
            MinimizeButton_Click();
        }

        #endregion

        private void F4SDLogo_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                var communication = cFasdCockpitCommunicationBase.Instance;
                var serverUrl = cFasdCockpitMachineConfiguration.Instance?.ServerUrl;

                if (communication?.IsDemo() == true || string.IsNullOrWhiteSpace(serverUrl) || !Uri.TryCreate(serverUrl, UriKind.Absolute, out var baseUri))
                {
                    _introView.Show();
                    Close();
                    return;
                }

                using (HttpClient http = new HttpClient() { BaseAddress = baseUri })
                {
                    if (!http.GetAsync(IntroView.IntroPath).Result.IsSuccessStatusCode)
                        return;
                }

                _introView.Show();

                Close();
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
        }
    }
}
