@page "/serverstatus"

@using ServerManagement.StateStore

@inject NavigationManager NavigationManager
@inject ContainerStorage containerStorage
@inject IServersEFCoreRepository ServersEFCoreRepository

<h3>Server Status</h3>
<br/>

@if (server != null)
{
  <FieldComponent Label="Server status online">
    <Control>
      @if (server.IsOnline)
      {
        <input type="checkbox" @bind-value="server.IsOnline" class="form-check-input"></input>
      }
      else
      {
        <input type="checkbox" @bind-value="server.IsOnline" class="form-check-input" checked></input>
      }
    </Control>
  </FieldComponent>
  <br/>
  <button type="button" class="btn btn-primary" @onclick="Save">Save</button>
}

@code {
  private Server? server;

  protected override void OnInitialized()
  {
    base.OnInitialized();
  }

  protected override void OnAfterRender(bool firstRender)
  {
    if (firstRender)
    {
      this.server = containerStorage.GetServer();
      StateHasChanged();
    }
  }

  private void Save()
  {
    if (server != null)
    {
      containerStorage.SetServer(null);
      ServersEFCoreRepository.AddServer(server);
      NavigationManager.NavigateTo($"/servers/back_from/{server?.City}");
    }
  }
}