@inject DialogService DialogService
@inject NotificationService NotificationService
<div style="display: flex; justify-content: center; align-items: center; height: 100vh; background-color: transparent;">
    <RadzenCard Style="padding: 2rem; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); width: 50%; max-width: 600px;">
        <RadzenStack Orientation="Orientation.Vertical" Gap="20px" Style="color: white;">
            <RadzenLabel Text="Enter your Discord bot token below:" />
            <RadzenTextBox
                id="token"
                Name="token"
                Placeholder="Enter your token here ..."
                Style="width: 100%;"
                Value="@TokenString"
                ValueChanged="str => TokenString = str"/>
            <RadzenLabel Text="Specify the bot's prefix:" />
            <RadzenTextBox
                id="prefix"
                Name="prefix"
                Placeholder="Enter your prefix here ..."
                Style="width: 100%;"
                Value="@PrefixString"
                ValueChanged="str => PrefixString = str" />
            <RadzenLabel Text="Enter server IDs (separated by semicolons):" />
            <RadzenTextBox
                id="server"
                Name="server"
                Placeholder="Enter server IDs here ..."
                Style="width: 100%;"
                Value="@ServerIdsString"
                ValueChanged="str => ServerIdsString = str" />

            <RadzenButton Text="Save Changes" Click="SaveButtonClick" Style="margin-top: 1rem;" />
        </RadzenStack>
    </RadzenCard>
</div>


@code {
    [Parameter]
    public string TokenString { get; set; }
    [Parameter]
    public string PrefixString { get; set; }
    [Parameter]
    public string ServerIdsString { get; set; }

    [Parameter]
    public required Action<string, string, string> SaveSettings { get; set; }


    private async void SaveButtonClick()
    {
        var response = await DialogService.Confirm("Saving this requires a bot restart.\nRestart now?", "Save Settings", new ConfirmOptions()
        {
            CloseDialogOnEsc = false,
            ShowClose = false,
            CloseDialogOnOverlayClick = false,
            OkButtonText = "Restart Now",
            CancelButtonText = "Don't Save"
        });
        
        if (!response.HasValue)
        {
            NotificationService.Notify(new NotificationMessage()
            {
                Severity = NotificationSeverity.Error,
                Summary = "Error",
                Detail = "An error occurred while trying to save settings."
            });
            return;
        }

        if (!response.Value)
        {
            NotificationService.Notify(new NotificationMessage()
            {
                Severity = NotificationSeverity.Info,
                Summary = "Info",
                Detail = "Settings were not saved."
            });
            return;
        }
        
        if (string.IsNullOrWhiteSpace(TokenString) || string.IsNullOrWhiteSpace(PrefixString) || string.IsNullOrWhiteSpace(ServerIdsString))
        {
            NotificationService.Notify(new NotificationMessage()
            {
                Severity = NotificationSeverity.Error,
                Summary = "Error",
                Detail = "Please fill in all fields."
            });
            return;
        }
        
        if(!ValidateToken())
        {
            NotificationService.Notify(new NotificationMessage()
            {
                Severity = NotificationSeverity.Error,
                Summary = "Error",
                Detail = "Invalid token."
            });
            return;
        }
        
        if(!ValidatePrefix())
        {
            NotificationService.Notify(new NotificationMessage()
            {
                Severity = NotificationSeverity.Error,
                Summary = "Error",
                Detail = "Invalid prefix."
            });
            return;
        }
        
        if(!ValidateServerIds())
        {
            NotificationService.Notify(new NotificationMessage()
            {
                Severity = NotificationSeverity.Error,
                Summary = "Error",
                Detail = "Invalid server IDs."
            });
            return;
        }

        SaveSettings.Invoke(TokenString, PrefixString, ServerIdsString);
    }

    private bool ValidateToken()
    {
        if(TokenString.Length < 59)
        {
            return false;
        }
        
        return true;
    }
    
    private bool ValidatePrefix()
    {
        if(PrefixString.Length < 1)
        {
            return false;
        }
        
        return true;
    }
    
    private bool ValidateServerIds()
    {
        if(ServerIdsString.Length < 1)
        {
            return false;
        }
        
        return true;
    }

}