@inherits ComponentBase

<BaseContainer Title="@Title" Breadcrumbs="@_breadcrumbs" LoadData="LoadDataAsync" DisableReload>
    <Content>
        @if (_model is not null)
        {
            <MudToolBar Dense DisableGutters>
                 <MudButton OnClick="()=>_activeWindow = 0" Variant="Variant.Filled" StartIcon="@Icons.Material.Filled.Build" Color="_activeWindow == 0 ? Color.Info : Color.Surface" Size="Size.Small" DisableElevation>Script</MudButton>
                <MudButton OnClick="()=>_activeWindow = 1" Variant="Variant.Filled" StartIcon="@Icons.Material.Filled.Api" Color="_activeWindow == 1 ? Color.Info : Color.Surface" Size="Size.Small" DisableElevation Class="ml-2">Result</MudButton>
                 <MudSpacer />
                 <MudIconButton OnClick="()=>_optionDialog?.ToggleAsync(null)" Icon="@Icons.Material.Filled.Settings" Variant="Variant.Text" DisableElevation Class="mr-2"></MudIconButton>
                 <MudButton Disabled="@_running" OnClick="()=>RunAsync(default)" Variant="Variant.Filled" Color="Color.Success" Size="Size.Small" IconSize="Size.Small" DisableElevation EndIcon="@(_running ? "" : @Icons.Material.Filled.Send)">
                    @if (_running)
                    {
                        <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" Style="height:16px; width:16px;" />
                        <MudText>Processing</MudText>
                    }
                    else
                    {
                        <MudText>Execute</MudText>
                    }
                </MudButton>
                @if (_running)
                {
                    <MudButton Variant="Variant.Filled" Color="Color.Error" Size="Size.Small" DisableElevation Class="ml-2">Abort</MudButton>
                }
            </MudToolBar>

            <MudPaper Outlined Height="calc(100vh - 200px)">
                 @if (_activeWindow == 0)
                {
                    <MudTextField T="string" @bind-Text="_model.Request.RequestData" spellcheck="false"
                        Placeholder="Request" 
                        Variant="Variant.Text" 
                        AutoGrow 
                        Disabled="@(_running ? true : false)"
                        DisableUnderLine 
                        Class="px-3" 
                        Style="height:calc(100vh - 210px); overflow:scroll; align-items:normal; margin-top:0px;" />
                }
                else
                {
                    @if (_model.Response.ResponseError ?? false)
                    {
                        <MudText>Response contains Errors</MudText>
                    }
                    <MudTextField T="string" @bind-Text="_model.Response.ResponseData" spellcheck="false" 
                    Placeholder="Response" 
                    Variant="Variant.Text" 
                    AutoGrow 
                    ReadOnly 
                    DisableUnderLine 
                    Class="px-3" 
                    Style="height:calc(100vh - 210px); overflow:scroll; align-items:normal; margin-top:0px;" />
                }
            </MudPaper>
        }
    </Content>
</BaseContainer>

<ConsoleOptionDialog @ref="_optionDialog" OnChanges="(x) => OnRefreshAsync(x)" />

@code{
    private ConsoleOptionDialog? _optionDialog;

    private int _activeWindow = 0;
    private bool _running = false;
}