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

namespace HyperCube.Pages
{
    public partial class Wallet
    {
        [Inject]
        AppData AppData { get; set; }
        private AccountModel account = new();
        string myBalance = "";
        string address = "";

        protected override async Task OnInitializedAsync()
        {
            account = AppData.CurrentAccount;
            var bc = await account.GetSelectedBlockChain();
            address = account.GetActualAddress(bc);
            if (bc != 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.LoadRoles();
                return account;
            }

            return null;
        }

        bool admin
        {
            get { return account.Roles.Contains(Role.Admin); }
            set
            {
                if (!account.Roles.Contains(Role.Admin))
                {
                    account.AddRole(Role.Admin);
                    InsertRole(Role.Admin);                    
                }
                else
                {
                    account.RemoveRole(Role.Admin);
                    DeleteRole(Role.Admin);                    
                }
            }
        }
        bool initiator
        {
            get { return account.Roles.Contains(Role.Initiator); }
            set
            {
                if (!account.Roles.Contains(Role.Initiator))
                {
                    account.AddRole(Role.Initiator);
                    InsertRole(Role.Initiator);                    
                }
                else
                {
                    account.RemoveRole(Role.Initiator);
                    DeleteRole(Role.Initiator);                    
                }
            }
        }
        bool verifier
        {
            get { return account.Roles.Contains(Role.Verifier); }
            set
            {
                if (!account.Roles.Contains(Role.Verifier))
                {
                    account.AddRole(Role.Verifier);
                    InsertRole(Role.Verifier);                    
                }
                else
                {
                    account.RemoveRole(Role.Verifier);
                    DeleteRole(Role.Verifier);                    
                }
            }
        }
        bool requester
        {
            get { return account.Roles.Contains(Role.Requester); }
            set
            {
                if (!account.Roles.Contains(Role.Requester))
                {
                    account.AddRole(Role.Requester);
                    InsertRole(Role.Requester);                    
                }
                else
                {
                    account.RemoveRole(Role.Requester);
                    DeleteRole(Role.Requester);                    
                }
            }
        }

        void InsertRole(Role role)
        {
            MySQLConnector.Instance().SQLInsert($"insert into account_roles (account_uuid, role_id) values ('{account.UUID}', {(int)role})");
        }

        void DeleteRole(Role role)
        {
            MySQLConnector.Instance().SQLInsert($"delete from account_roles where account_uuid = '{account.UUID}' and role_id={(int)role}");
        }
    }
}