using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration.UserSecrets;
using WF_WebAdmin.Model;

namespace WF_WebAdmin.Pages
{
    public partial class DeleteUser
    {
        private List<User> users;


        private bool showPopupDelete = false;
        private User userToDelete = null;

        private bool showPopupAdmin = false;
        private User userToAdmin = null;
        [Inject]
        public HttpClient Http { get; set; }

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

        protected override async Task OnInitializedAsync()
        {
            users = await Http.GetFromJsonAsync<List<User>>($"{NavigationManager.BaseUri}fake-dataUsers.json");
        }



        // ------- Popup remove user -------
        private void ShowConfirmation(User user)
        {
            userToDelete = user;
            showPopupDelete = true;
        }



        private async Task RemoveUser()
        {
            if (userToDelete != null)
            {
                users.RemoveAll(u => u.Name == userToDelete.Name);
                ClosePopup();
            }
        }

        private void ClosePopup()
        {
            showPopupDelete = false;
            showPopupAdmin = false;
        }


        // ------- Popup admin -------
        private void ShowConfirmationAdmin(User user)
        {
            userToAdmin = user;
            showPopupAdmin = true;
        }



        private async Task Admin()
        {
            if (!userToAdmin.IsAdmin)
            {
                userToAdmin.IsAdmin = true;
                ClosePopup();
            }
            else
            {
                userToAdmin.IsAdmin = false;
                ClosePopup();
            }
        }

    }

}
