namespace TeamViewerApi.ViewModels
{
    #region Using
    using System;
    using System.Collections.Generic;
    using System.Linq;
    #endregion

    public class TokenViewModel : BasicViewModel
    {
        #region Properties
        private static string token;
        private static string status;
        #endregion

        #region Constructor
        public TokenViewModel()
        {
            this.Token = string.Empty;
            this.PingCommand = new Command(this.Ping);
        }
        #endregion

        #region Methods
        private void Ping(object o)
        {
            if (string.IsNullOrWhiteSpace(TokenViewModel.token))
                return;

            if (TeamViewer.Ping.Check(TokenViewModel.token))
                this.Status = "OK";
            else
                this.Status = "Error";
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Access token
        /// </summary>
        public string Token
        {
            get { return TokenViewModel.token; }
            set
            {
                if (TokenViewModel.token != value)
                {
                    TokenViewModel.token = value;

                    this.RaisePropertyChanged(() => this.Token);

                    this.Status = "...";
                }
            }
        }

        /// <summary>
        /// Token status
        /// </summary>
        public string Status
        {
            get { return TokenViewModel.status; }
            set
            {
                if (TokenViewModel.status != value)
                {
                    TokenViewModel.status = value;

                    this.RaisePropertyChanged(() => this.Status);
                }
            }
        }
        #endregion

        #region Public methods
        /// <summary>
        /// Get access token
        /// </summary>
        public static string GetToken()
        {
            if (TokenViewModel.status == "OK")
                return TokenViewModel.token;
            else
                return string.Empty;
        }
        #endregion

        #region Commands
        public Command PingCommand { get; private set; }
        #endregion
    }
}
