using Microsoft.AspNetCore.Http;

namespace RegistroPersonalAN.Models
{
    public class UserState
    {
        private readonly object _lock = new object();
        private string _token = string.Empty;
        private string _NombreUsu = string.Empty;
        private bool _Mostrar;
        private int _currentPage = 1;

        public UserState(IHttpContextAccessor httpContextAccessor, UserStateCookieStore cookieStore)
        {
            var snapshot = cookieStore.Read(httpContextAccessor.HttpContext);
            if (snapshot is null)
            {
                return;
            }

            _token = snapshot.Token;
            _NombreUsu = snapshot.NombreUsu ?? string.Empty;
        }

        public string Token
        {
            get
            {
                lock (_lock)
                {
                    return _token;
                }
            }
            set
            {
                lock (_lock)
                {
                    _token = value ?? string.Empty;
                }
            }
        }

        public string NombreUsu
        {
            get
            {
                lock (_lock)
                {
                    return _NombreUsu;
                }
            }
            set
            {
                lock (_lock)
                {
                    _NombreUsu = value ?? string.Empty;
                }
            }
        }

        public bool Mostrar
        {
            get
            {
                lock (_lock)
                {
                    return _Mostrar;
                }
            }
            set
            {
                lock (_lock)
                {
                    _Mostrar = value;
                }
            }
        }

        public int CurrentPage
        {
            get
            {
                lock (_lock)
                {
                    return _currentPage;
                }
            }
            set
            {
                lock (_lock)
                {
                    _currentPage = value;
                }
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                lock (_lock)
                {
                    return !string.IsNullOrWhiteSpace(_token);
                }
            }
        }

        public void Clear()
        {
            lock (_lock)
            {
                _token = string.Empty;
                _NombreUsu = string.Empty;
                _Mostrar = false;
                _currentPage = 1;
            }
        }
    }
}
