using C4IT.MultiLanguage;
using FasdDesktopUi.Basics;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.DetailsPage.UserControls
{
    public partial class DetailsPageRefreshControl : UserControl
    {
        #region Properties

        private DispatcherTimer timer;

        public cSupportCaseDataProvider DataProvider { get; set; }

        #region IsDataIncomplete DependencyProperty

        public static readonly DependencyProperty IsDataIncompleteProperty =
            DependencyProperty.Register("IsDataIncomplete", typeof(bool), typeof(DetailsPageRefreshControl), new PropertyMetadata(true));

        public bool IsDataIncomplete
        {
            get { return (bool)GetValue(IsDataIncompleteProperty); }
            set { SetValue(IsDataIncompleteProperty, value); }
        }

        #endregion

        #endregion

        public event EventHandler RefreshButtonClicked;

        public DetailsPageRefreshControl()
        {
            InitializeComponent();
        }

        ~DetailsPageRefreshControl()
        {
            timer.Stop();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            timer = new DispatcherTimer(TimeSpan.FromSeconds(15), DispatcherPriority.Loaded, new EventHandler((s, args) =>
            {
                try { UpdateLastDataRequestTime(); } catch { }
            }), Dispatcher.CurrentDispatcher);

            timer.Start();
        }

        public void UpdateLastDataRequestTime()
        {
            try
            {
                string newText = string.Empty;
                if (DataProvider?.HealthCardDataHelper?.LoadingHelper?.LastDataRequest is null)
                {
                    LastDataRequestTextBlock.Text = newText;
                    return;
                }

                string dateAddtionFormat = cMultiLanguageSupport.GetItem("DetailsPage.LastRefresh");
                string timeText = string.Empty;

                var from = DataProvider?.HealthCardDataHelper?.LoadingHelper?.LastDataRequest;
                if (from == null)
                    return;

                TimeSpan timeSpan = DateTime.Now - from.Value;

                if (timeSpan.TotalHours < 1)
                    timeText = $"< {Math.Ceiling(timeSpan.TotalMinutes)} min";
                else
                    timeText = $"< {Math.Ceiling(timeSpan.TotalHours)} h";

                newText = string.Format(dateAddtionFormat, timeText);

                LastDataRequestTextBlock.Text = newText;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        #region RefreshDetails

        private void RefreshDetails_Click()
        {
            LoadingDataIndicatorUc.LoadingText = cMultiLanguageSupport.GetItem("DetailsPage.Refresh");
            RefreshButtonClicked?.Invoke(this, EventArgs.Empty);
        }

        private void RefreshDetailsButton_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                RefreshDetails_Click();
        }

        private void RefreshDetailsButton_TouchDown(object sender, TouchEventArgs e)
        {
            RefreshDetails_Click();
        }

        #endregion
    }
}
