@page "/admin/nodes/new"

@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Repositories
@using Moonlight.App.Services

@inject SmartTranslateService SmartTranslateService
@inject NodeRepository NodeRepository
@inject NavigationManager NavigationManager

@attribute [PermissionRequired(nameof(Permissions.AdminNewNode))]

<div class="d-flex flex-center">
        <div class="card rounded-3 w-md-550px">
            <div class="card-body">
                <div class="d-flex flex-center flex-column-fluid">
                    <div class="form w-100 fv-plugins-bootstrap5 fv-plugins-framework">
                        <div class="fv-row mb-8">
                            <label class="form-label">
                                <TL>Nodename</TL>
                            </label>
                            <input @bind="NewNode.Name" type="text" placeholder="@(SmartTranslateService.Translate("Nodename"))" class="form-control bg-transparent">
                        </div>
                        <div class="fv-row mb-8">
                            <label class="form-label">
                                <TL>FQDN</TL>
                            </label>
                            <input @bind="NewNode.Fqdn" type="text" placeholder="@(SmartTranslateService.Translate("FQDN"))" class="form-control bg-transparent">
                        </div>
                        <div class="fv-row mb-8">
                            <label class="form-label">
                                <TL>Http port</TL>
                            </label>
                            <input @bind="NewNode.HttpPort" type="number" class="form-control bg-transparent">
                        </div>
                        <div class="fv-row mb-8">
                            <label class="form-label">
                                <TL>Sftp port</TL>
                            </label>
                            <input @bind="NewNode.SftpPort" type="number" class="form-control bg-transparent">
                        </div>
                        <div class="fv-row mb-8">
                            <label class="form-label">
                                <TL>Moonlight daemon port</TL>
                            </label>
                            <input @bind="NewNode.MoonlightDaemonPort" type="number" class="form-control bg-transparent">
                        </div>
                        <div class="fv-row mb-8">
                            <div class="input-group">
                                <label class="col-lg-4 col-form-label fw-semibold fs-6">
                                    <TL>SSL</TL>
                                </label>
                                <div class="col-lg-8 d-flex align-items-center">
                                    <div class="form-check form-check-solid form-switch form-check-custom fv-row">
                                        <input @bind="NewNode.Ssl" class="form-check-input w-45px h-30px" type="checkbox" id="ssl">
                                        <label class="form-check-label" for="ssl"></label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="fv-row mb-9">
                            <WButton Text="@(SmartTranslateService.Translate("Create"))"
                                     WorkingText="@(SmartTranslateService.Translate("Creating"))"
                                     CssClasses="btn-success"
                                     OnClick="Create">
                            </WButton>
                            <a href="/admin/nodes" class="btn btn-primary">
                                <TL>Back</TL>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

@code
{
    private Node NewNode = new()
    { // Default values
        HttpPort = 8080,
        SftpPort = 2022,
        MoonlightDaemonPort = 8081,
        Ssl = true
    };

    private Task Create()
    {
        NewNode.Token = StringHelper.GenerateString(65);
        NewNode.TokenId = StringHelper.GenerateString(17);

        NodeRepository.Add(NewNode);
        NavigationManager.NavigateTo("/admin/nodes");

        return Task.CompletedTask;
    }
}