using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Aitex.Core.Account;

namespace MECF.Framework.UI.Client.ClientBase.Dialog
{
    /// <summary>
    /// Interaction logic for LoginRequestConfirmationDialog.xaml
    /// </summary>
    public partial class LoginRequestConfirmationDialog : Window
    {
        #region Variables

        private readonly Credential _requestingCredential;
        private readonly CancellationTokenSource _ctsCountDownTask;
        private readonly CancellationTokenSource _ctsRemoteRequstCanceled;
        private readonly IProgress<double> _progCountDown;

        #endregion

        public LoginRequestConfirmationDialog(Credential requestingCredential, CancellationTokenSource cts)
        {
            Debug.Assert(cts != null);
            _requestingCredential = requestingCredential;
            _ctsRemoteRequstCanceled = cts;
            _ctsCountDownTask = new CancellationTokenSource();
            _progCountDown = new Progress<double>(countDown =>
            {
                if (countDown > 0)
                {
                    if (BtnRejected != null) 
                        BtnRejected.Content = $"Reject ({countDown:F0}s)";
                }
                else
                    Close();
            });

            InitializeComponent();
        }

        #region Properties

        public string Description =>
            $"User [{_requestingCredential}] is requesting to login from somewhere, do you agree to it's request and logout from the system?";

        #endregion

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

            var closeTime = DateTime.Now.Add(TimeSpan.FromSeconds(CredentialManager.REQ_LOGIN_DIALOG_LIFT_TIME_SEC));
            Task.Run(() =>
            {
                while (true)
                {
                    var timeRemained = (closeTime - DateTime.Now).TotalSeconds;
                    if (timeRemained <= 0)
                    {
                        // force to reject
                        _progCountDown.Report(0);
                        break;
                    }

                    _progCountDown.Report(timeRemained);
                    Thread.Sleep(500);

                    // 结束倒计时
                    if (_ctsCountDownTask.Token.IsCancellationRequested)
                        break;

                    // 登录请求已取消
                    if (_ctsRemoteRequstCanceled.Token.IsCancellationRequested)
                    {
                        _progCountDown.Report(0);
                        break;
                    }
                }
            });
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _ctsCountDownTask?.Cancel();
        }

        private void BtnRejected_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void BtnAccept_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }
    }
}
