@using RobotNet.Shares
@using RobotNet.Clients
@using RobotNet.WebApp.Scripts.Models

@inject ISnackbar Snackbar
@inject IHttpClientFactory httpFactory

<MudDialog>
    <TitleContent>
        <MudText Typo="Typo.h6">
            @Title
        </MudText>
    </TitleContent>
    <DialogContent>
        <div class="d-flex flex-column" style="width: 500px;">
            <div class="d-flex align-items-center mb-2">
                <div class="d-flex flex-row-reverse me-2" style="width: 200px;">
                    <MudText>@Model.Name : </MudText>
                </div>
                <div style="flex: 1;">
                    @switch (Model.TypeName)
                    {
                        case "System.Boolean":
                            <MudSwitch T="bool" Color="Color.Primary" @bind-Value="@ValueModel.BoolValue" />
                            break;
                        case "System.Byte":
                            <MudNumericField T="byte"
                                             @bind-Value="@ValueModel.ByteValue"
                                             Min="@System.Byte.MinValue"
                                             Max="@System.Byte.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.SByte":
                            <MudNumericField T="sbyte"
                                             @bind-Value="@ValueModel.SByteValue"
                                             Min="@System.SByte.MinValue"
                                             Max="@System.SByte.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.Int16":
                            <MudNumericField T="short"
                                             @bind-Value="@ValueModel.ShortValue"
                                             Min="@System.Int16.MinValue"
                                             Max="@System.Int16.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.UInt16":
                            <MudNumericField T="ushort"
                                             @bind-Value="@ValueModel.UShortValue"
                                             Min="@System.UInt16.MinValue"
                                             Max="@System.UInt16.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.Int32":
                            <MudNumericField T="int"
                                             @bind-Value="@ValueModel.IntValue"
                                             Min="@System.Int32.MinValue"
                                             Max="@System.Int32.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.UInt32":
                            <MudNumericField T="uint"
                                             @bind-Value="@ValueModel.UIntValue"
                                             Min="@System.UInt32.MinValue"
                                             Max="@System.UInt32.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.Int64":
                            <MudNumericField T="long"
                                             @bind-Value="@ValueModel.LongValue"
                                             Min="@System.Int64.MinValue"
                                             Max="@System.Int64.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.UInt64":
                            <MudNumericField T="ulong"
                                             @bind-Value="@ValueModel.ULongValue"
                                             Min="@System.UInt64.MinValue"
                                             Max="@System.UInt64.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.Single":
                            <MudNumericField T="float"
                                             @bind-Value="@ValueModel.FloatValue"
                                             Min="@System.Single.MinValue"
                                             Max="@System.Single.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.Double":
                            <MudNumericField T="double"
                                             @bind-Value="@ValueModel.DoubleValue"
                                             Min="@System.Double.MinValue"
                                             Max="@System.Double.MaxValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.Decimal":
                            <MudNumericField T="decimal"
                                             @bind-Value="@ValueModel.DecimalValue"
                                             Label="@ValueModel.Type"
                                             ShrinkLabel="true"
                                             Variant="Variant.Outlined"
                                             Margin="Margin.Dense" />
                            break;
                        case "System.String":
                            <MudTextField @bind-Value="@ValueModel.StringValue"
                                          Label="@ValueModel.Type"
                                          ShrinkLabel="true"
                                          Variant="Variant.Outlined"
                                          Margin="Margin.Dense" />
                            break;
                        case "System.Char":
                            <MudTextField @bind-Value="@ValueModel.CharValue"
                                          MaxLength="1"
                                          Label="@ValueModel.Type"
                                          ShrinkLabel="true"
                                          Variant="Variant.Outlined"
                                          Margin="Margin.Dense" />
                            break;
                        case "System.Threading.CancellationToken":
                            <span>
                                &lt;CancellationToken&gt;
                            </span>
                            break;
                        default:
                            <MudAlert Severity="Severity.Error" Dense>Unsupport parameter with type @ValueModel.Type</MudAlert>
                            break;
                    }
                </div>
            </div>
        </div>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="OnCancel">Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="RequestSetValue" Disabled="@IsRequesing">Update</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter]
    private IMudDialogInstance Dialog { get; set; } = null!;

    [Parameter, EditorRequired]
    public ScriptVariableModel Model { get; set; } = default!;

    public string Title => $"Set Variable \"{Model.Name}\"";
    private HttpClient http = default!;

    private ScriptValueModel ValueModel = null!;

    private bool IsRequesing = false;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        ValueModel = new ScriptValueModel(Model.Name, Model.TypeName, "");

        switch (Model.TypeName)
        {
            case "System.Boolean":
                ValueModel.BoolValue = default;
                break;
            case "System.Byte":
                ValueModel.ByteValue = default;
                break;
            case "System.SByte":
                ValueModel.SByteValue = default;
                break;
            case "System.Int16":
                ValueModel.ShortValue = default;
                break;
            case "System.UInt16":
                ValueModel.UShortValue = default;
                break;
            case "System.Int32":
                ValueModel.IntValue = default;
                break;
            case "System.UInt32":
                ValueModel.UIntValue = default;
                break;
            case "System.Int64":
                ValueModel.LongValue = default;
                break;
            case "System.UInt64":
                ValueModel.ULongValue = default;
                break;
            case "System.Single":
                ValueModel.FloatValue = default;
                break;
            case "System.Double":
                ValueModel.DoubleValue = default;
                break;
            case "System.Decimal":
                ValueModel.DecimalValue = default;
                break;
        }
        http = httpFactory.CreateClient("ScriptManagerAPI");
    }

    private async Task RequestSetValue()
    {
        if (IsRequesing) return;
        IsRequesing = true;
        StateHasChanged(); // Force UI update to reflect instantiation state

        var result = await http.PutFromJsonAsync<MessageResult>($"/api/ScriptVariables/{ValueModel.Name}", new UpdateVariableModel(ValueModel.Name, ValueModel.ToString()));
        if (result == null)
        {
            Snackbar.Add("Failed to instantiate mission: server response null", Severity.Error);
        }
        else if (result.IsSuccess)
        {
            Snackbar.Add(result.Message, Severity.Success);
            Dialog.Close();
        }
        else
        {
            Snackbar.Add($"Failed to reset variable value: {result.Message}", Severity.Error);
            Dialog.Close();
        }
    }

    private void OnCancel() => Dialog.Cancel();
}
