@using ImageBoardServerApp.Data.Repository
@inject NavigationManager navigationManager

<div class="pd centered">
    <div class="centered formContent">
        <div>
            <div class="pd centered marg"> 
                <RadzenTextBox Placeholder="Tag" MaxLength="3" @bind-Value="@Tag" Class="w-100"/>
            </div>
    
            <div class="pd centered marg"> 
                <RadzenTextBox Placeholder="Topic" MaxLength="15" @bind-Value="@Topic" Class="w-100"/>
            </div>
            
            <div class="pd centered marg"> 
                <RadzenTextBox Placeholder="MaxThreads (Number)" MaxLength="3" @bind-Value="@maxThread" Class="w-100"/>
            </div>
            
            <div class="pd centered marg"> 
                <RadzenCheckBox @bind-Value=@lockBoard />
                <span>Lock Board</span>
            </div>
    
        </div>
    </div>
    <div class="pd centered marg">
        <RadzenButton class="pd" Click="@onPostClick" Text="Set!"></RadzenButton>
    </div>
</div>

@code {
    [Parameter]
    public string Tag { get; set; }

    [Parameter]
    public string Topic { get; set; }

    [Parameter]
    public string maxThread { get; set; }

    [Parameter]
    public bool lockBoard { get; set; }


    private async Task onPostClick()
    {
        if (!int.TryParse(maxThread, out _))
            return;
        var newB = new BoardData()
        {
            Tag = Tag.ToLower(),
            Topic = Topic,
            maxThreads = int.Parse(maxThread),
            isLocked = lockBoard,
            NumberOfGETs = 0
        };

        var bCheck = await BoardsRepository.getBoardByTagAsync(newB.Tag);
        if (bCheck == null)
        {
            await BoardsRepository.createBoardAsync(newB);
            return;
        }
        bCheck.Tag = newB.Tag;
        bCheck.Topic = newB.Topic;
        bCheck.maxThreads = newB.maxThreads;
        bCheck.isLocked = newB.isLocked;

        await BoardsRepository.updateBoardAsync(bCheck);
        navigationManager.NavigateTo("/sys/modmenu/boards", true, true);
    }
}