@namespace ServerManagement.Components.Controls

@inject NavigationManager NavigationManager
@inject EindhovenOnlineServersStore EindhovenOnlineServersStore
@inject IServersEFCoreRepository ServersEFCoreRepository

@if (Server != null)
{
  <tr @key="Server.Id" style="background-color: @GetBackgroundColor();">
    <td>
      @Server.Name
    </td>
    <td>
      @Server.City
    </td>
    <td>
      <span style="color:@(Server.IsOnline ? "green" : "red")">
        @(Server.IsOnline ? "online" : "offline")
      </span>
    </td>
    <td>
      @if (Server.IsOnline)
      {
        Random random = new Random();
        int randomNumber = random.Next(0, 500);
        <text>@randomNumber users online</text>
      }
      else
      {
        <text>N/A</text>
      }
    </td>
    <td>
      @if (Server.IsOnline)
      {
        <button type="button" class="btn btn-outline-danger"
          @onclick="@(() => { SetServerStatus(false); })">
        Turn off
        </button>
      }
      else
      {
        <button type="button" class="btn btn-outline-success"
          @onclick="@(() => { SetServerStatus(true); })">
        Turn on
        </button>
      }
      &nbsp;
      <a href="@($"/server/{Server.Id}")" class="btn btn-primary">Edit</a>
    </td>
    <td>
      <EditForm
        Model="Server"
        FormName="@($"formDeleteServer{Server.Id}")"
        OnValidSubmit="@(() => { DeleteServer(Server.Id); })">
        <button type="submit" class="btn btn-danger">Delete</button>
      </EditForm>
    </td>
  </tr>
}

@code {
  [Parameter]
  public Server? Server { get; set; }

  [CascadingParameter(Name="SelectedCity")]
  public string? SelectedCity { get; set; }

  private void DeleteServer(int serverId)
  {
    if (serverId > 0)
    {
      ServersEFCoreRepository.DeleteServer(serverId);
      NavigationManager.Refresh(forceReload: true);
    }
  }

  private string GetBackgroundColor()
  {
    if (SelectedCity != null) {
      switch (this.SelectedCity)
      {
        case "Eindhoven": return "lightskyblue";
        case "Helmond": return "lightcoral";
        case "Oosterhout": return "lightgreen";
        case "Roosendaal": return "lightsalmon";
        case "Deurne": return "lightpink";
        default:
          return "white";
      }
    }
    else
    {
      return "white";
    }
  }

  private void SetServerStatus(bool status)
  {
    if (this.Server != null)
    {
      if (this.Server.IsOnline != status)
      {
        if (this.Server.City.Equals("Eindhoven", StringComparison.OrdinalIgnoreCase))
        {
          var numberOnline = EindhovenOnlineServersStore.GetNumberServersOnline();
          if (status)
          {
            EindhovenOnlineServersStore.SetNumberServersOnline(numberOnline + 1);
          }
          else if (numberOnline > 1)
          {
            EindhovenOnlineServersStore.SetNumberServersOnline(numberOnline - 1);
          }
        }
        this.Server.IsOnline = status;

        ServersEFCoreRepository.UpdateServer(this.Server.Id, Server);
      }
    }
  }
}