// <auto-generated/>
#pragma warning disable 1591
#pragma warning disable 0414
#pragma warning disable 0649
#pragma warning disable 0169

namespace spanreed.Client.Pages
{
    #line hidden
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "/home/skins/projects/spanreed/Client/_Imports.razor"
using System.Net.Http;

#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "/home/skins/projects/spanreed/Client/_Imports.razor"
using System.Net.Http.Json;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "/home/skins/projects/spanreed/Client/_Imports.razor"
using Microsoft.AspNetCore.Components.Forms;

#line default
#line hidden
#nullable disable
#nullable restore
#line 4 "/home/skins/projects/spanreed/Client/_Imports.razor"
using Microsoft.AspNetCore.Components.Routing;

#line default
#line hidden
#nullable disable
#nullable restore
#line 5 "/home/skins/projects/spanreed/Client/_Imports.razor"
using Microsoft.AspNetCore.Components.Web;

#line default
#line hidden
#nullable disable
#nullable restore
#line 6 "/home/skins/projects/spanreed/Client/_Imports.razor"
using Microsoft.AspNetCore.Components.Web.Virtualization;

#line default
#line hidden
#nullable disable
#nullable restore
#line 7 "/home/skins/projects/spanreed/Client/_Imports.razor"
using Microsoft.AspNetCore.Components.WebAssembly.Http;

#line default
#line hidden
#nullable disable
#nullable restore
#line 8 "/home/skins/projects/spanreed/Client/_Imports.razor"
using Microsoft.JSInterop;

#line default
#line hidden
#nullable disable
#nullable restore
#line 9 "/home/skins/projects/spanreed/Client/_Imports.razor"
using spanreed.Client;

#line default
#line hidden
#nullable disable
#nullable restore
#line 10 "/home/skins/projects/spanreed/Client/_Imports.razor"
using spanreed.Client.Shared;

#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "/home/skins/projects/spanreed/Client/Pages/Room.razor"
using Microsoft.AspNetCore.SignalR.Client;

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "/home/skins/projects/spanreed/Client/Pages/Room.razor"
using spanreed.Shared;

#line default
#line hidden
#nullable disable
    [Microsoft.AspNetCore.Components.RouteAttribute("/{room}")]
    public partial class Room : Microsoft.AspNetCore.Components.ComponentBase, IDisposable
    {
        #pragma warning disable 1998
        protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
        {
        }
        #pragma warning restore 1998
#nullable restore
#line 31 "/home/skins/projects/spanreed/Client/Pages/Room.razor"
       
    private HubConnection hubConnection;
    private List<ChatData> messages = new List<ChatData>();
    private string userInput;
    private string messageInput;
    [Parameter]
    public string room {get; set;}

    protected override async Task OnParametersSetAsync() {
        if (hubConnection != null) {
            try {
                await hubConnection.StopAsync(); 
            }
            finally {
                await hubConnection.DisposeAsync();
            }
        }

        messages.Clear();
        messageInput = "";

        hubConnection = new HubConnectionBuilder().WithUrl(NavigationManager.ToAbsoluteUri("/sendmessage")).Build();
        hubConnection.On<string, string, DateTime>("Received", (author, content, timestamp) => {
            messages.Add(new ChatData{Author = author, Content = content, Timestamp = timestamp});
            StateHasChanged();
        });
        var old_messages = await Http.GetFromJsonAsync<ChatData[]>($"Chat/{room}");

        if (old_messages != null) {
            foreach (var message in old_messages)
            {
                messages.Add(message);
            }
        }

        await hubConnection.StartAsync();

        await hubConnection.SendAsync("Connect", room);
    }

    Task Send() =>
        hubConnection.SendAsync("sendMessage", userInput, messageInput, room, DateTime.Now);

    public bool IsConnected =>
        hubConnection.State == HubConnectionState.Connected;

    public void Dispose() {
        _ = hubConnection.DisposeAsync();
    }

#line default
#line hidden
#nullable disable
        [global::Microsoft.AspNetCore.Components.InjectAttribute] private HttpClient Http { get; set; }
        [global::Microsoft.AspNetCore.Components.InjectAttribute] private NavigationManager NavigationManager { get; set; }
    }
}
#pragma warning restore 1591
