using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using HyperCube.Models;

namespace HyperCube.Pages
{
    public partial class Account
    {
        [Inject]
        public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
        [Inject]
        public UserManager<IdentityUser> UserManager { get; set; }

        private AccountModel account = new();
        string myBalance = "";

        protected override async Task OnInitializedAsync()
        {
            account = await GetCurrentAcc();
            if (account.GetSelectedBlockChain() != null)
                myBalance = await account.GetBalance();
        }

        private async Task<AccountModel> GetCurrentAcc()
        {
            AccountModel account = new();

            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            var user = authState.User;

            if (user.Identity.IsAuthenticated)
            {
                var currentUser = await UserManager.GetUserAsync(user);
                var acc = AccountModel.Find(currentUser.Id);
                if (acc != null)
                    account = acc;

                //account.AccRole = Role.User;
                return account;
            }

            return null;
        }
    }
}
