using Microsoft.AspNetCore.Components;
using Blazor.Models;
using Blazor.Services;

namespace Blazor.Pages.Players
{
    public partial class AddPlayer
    {
        private PlayerModel playerModel = new();

        [Inject]
        public IDataService DataService { get; set; }

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


        private async void HandleValidSubmit()
        {
            await DataService.Add(playerModel);
            var formData = new List<KeyValuePair<string, string>>();
            formData.Add(new KeyValuePair<string, string>("nickname", playerModel.Nickname));
            formData.Add(new KeyValuePair<string, string>("password", playerModel.HashedPassword));

            var formContent = new FormUrlEncodedContent(formData);

            string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/players/"+playerModel.Id;

            using (var httpClient = new HttpClient())
            {
                var response = await httpClient.PostAsync(apiUri, formContent);

                if (response.IsSuccessStatusCode)
                {
                    var responseBody = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    var errorResponse = await response.Content.ReadAsStringAsync();
                }
            }
            NavigationManager.NavigateTo("players");
        }
    }
}
