using AyCode.Interfaces.StorageHandlers;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TIAMSharedUI.Resources;
using TIAMWebApp.Shared.Application.Interfaces;
using TIAMWebApp.Shared.Application.Utility;

namespace TIAMSharedUI.Shared.Components
{
    public partial class Navbar : ComponentBase
    {
        [Inject] 
        public ISecureStorageHandler SecureStorageHandler { get; set; }

        [Inject]
        public ISessionService sessionService { get; set; }
        [Inject] 
        public IStringLocalizer<MyResources> localizer { get; set; }

        [Inject] 
        public NavigationManager navigationManager { get; set; }

        [Inject]
        public IComponentUpdateService componentUpdateService { get; set; }

        [Inject]
        public LogToBrowserConsole logToBrowserConsole { get; set; }

        private bool collapseNavMenu = true;
        private bool myUser = false;
        private bool hasProperty = false;        

        //componentUpdateService.RefreshRequested += RefreshMe;


        private void RefreshMe()
        {
            logToBrowserConsole.LogToBC($"Navbar refresh called! {DateTime.Now} ");
            OnInitialized();
            StateHasChanged();
        }

        private void ToggleNavMenu()
        {
            collapseNavMenu = !collapseNavMenu;
        }

        private void NavigateBack()
        {

        }

        private void SignOut()
        {
            SecureStorageHandler.ClearAllSecureStorageAsync();
            sessionService.User = null;
            sessionService.IsAuthenticated = false;
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();
            componentUpdateService.RefreshRequested += RefreshMe;
            logToBrowserConsole.LogToBC($"Navbar OnInit {DateTime.Now} ");
            if (sessionService.User != null)
            {                 
                myUser = true;
            }
            else
            {
                logToBrowserConsole.LogToBC($"Navbar myUser false! {DateTime.Now} ");
                myUser = false;
            }

            var _properties = sessionService.User?.HasProperties;

            if (sessionService.User?.HasProperties != null)
            {
                if(_properties.Count > 0)
                {
                    hasProperty = true;
                }
                else
                {
                    hasProperty = false;
                }
                foreach (var property in _properties)
                {
                    logToBrowserConsole.LogToBC($"First property: {property.Value} ");
                }
            }
            
        }

    }
}
