using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Dfs.WayneChina.CardTrxManager.TrxSubmitter
{
    public class AuthToken
    {
        #region Standard OAuth token format

        /// <summary>
        /// The access token string issued by the authorization server.
        /// </summary>
        [JsonProperty("access_token")]
        public string AccessToken { get; set; }

        /// <summary>
        /// Token type, simply `bearer`.
        /// </summary>
        [JsonProperty("token_type")]
        public string TokenType { get; set; }

        /// <summary>
        /// Duration of the time the access token is granted for, in total seconds.
        /// </summary>
        [JsonProperty("expires_in")]
        public int ExpiresIn { get; set; }

        /// <summary>
        /// Access token to be used to get a new token, optional.
        /// </summary>
        [JsonProperty("refresh_token")]
        public string RefreshToken { get; set; }

        /// <summary>
        /// The error information
        /// </summary>
        [JsonProperty("error")]
        public string Error { get; set; }

        [JsonProperty("userName")]
        public string UserName { get; set; }

        [JsonProperty("alias")]
        public string UserAlias { get; set; }

        [JsonProperty("roleNames")]
        public string RoleNames { get; set; }

        [JsonProperty("BusinessUnits")]
        public string BusinessUnitsJsonString { get; set; }

        public IList<BusinessUnit> BusinessUnitList { get; set; }

        #endregion

        #region Custom extension

        /// <summary>
        /// When the token is retrieve from the auth server, to simplify, set the local received time.
        /// </summary>
        public DateTime TokenRetrievedTime { get; set; }

        /// <summary>
        /// Check if the token has expired or not, if not, then it's valid.
        /// </summary>
        /// <returns>valid or not</returns>
        public bool IsTokenValid()
        {
            //Current Time = Now + 5 minutes, Expiry Time = token retrieved time + expiry time span
            //If current time is not later than expiry time, then the token is valid
            if (DateTime.Now + new TimeSpan(0, 5, 0) < TokenRetrievedTime + new TimeSpan(0, 0, ExpiresIn))
                return true;

            return false;
        }

        #endregion
    }
}