@using AIStudio.Chat
@inherits AssistantLowerBase
@typeparam TSettings

<div class="inner-scrolling-context">

    <MudText Typo="Typo.h3" Class="mb-2 mr-3">
        @(this.Title)
    </MudText>

    <InnerScrolling>
        <ChildContent>
            <MudForm @ref="@(this.form)" @bind-IsValid="@(this.inputIsValid)" @bind-Errors="@(this.inputIssues)" FieldChanged="@this.TriggerFormChange" Class="pr-2">
                <MudGrid Class="mb-2">
                    <MudItem xs="10">
                        <MudText Typo="Typo.body1" Align="Align.Justify">
                            @this.Description
                        </MudText>
                    </MudItem>
                    <MudItem xs="2" Class="d-flex justify-end align-start">
                        <MudIconButton Variant="Variant.Filled" Icon="@Icons.Material.Filled.Settings" OnClick="() => this.OpenSettingsDialog()"/>
                    </MudItem>
                </MudGrid>

                @if (this.Body is not null)
                {
                    <CascadingValue Value="@this">
                        @this.Body
                    </CascadingValue>
                    
                    <MudStack Row="true" AlignItems="AlignItems.Center" StretchItems="StretchItems.Start" Class="mb-3">
                        <MudButton Disabled="@this.SubmitDisabled" Variant="Variant.Filled" OnClick="async () => await this.Start()" Style="@this.SubmitButtonStyle">
                            @this.SubmitText
                        </MudButton>
                        @if (this.isProcessing && this.cancellationTokenSource is not null)
                        {
                            <MudTooltip Text="@TB("Stop generation")">
                                <MudIconButton Variant="Variant.Filled" Icon="@Icons.Material.Filled.Stop" Color="Color.Error" OnClick="() => this.CancelStreaming()"/>
                            </MudTooltip>
                        }
                    </MudStack>
                }
            </MudForm>
            <Issues IssuesData="@(this.inputIssues)"/>

            @if (this.ShowDedicatedProgress && this.isProcessing)
            {
                <MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="mb-6" />
            }
            
            <div id="@RESULT_DIV_ID" class="mr-2 mt-3">
            </div>

            <div id="@BEFORE_RESULT_DIV_ID" class="mt-3">
            </div>

            @if (this.ShowResult && !this.ShowEntireChatThread && this.resultingContentBlock is not null)
            {
                <ContentBlockComponent Role="@(this.resultingContentBlock.Role)" Type="@(this.resultingContentBlock.ContentType)" Time="@(this.resultingContentBlock.Time)" Content="@(this.resultingContentBlock.Content)"/>
            }
            
            @if(this.ShowResult && this.ShowEntireChatThread && this.chatThread is not null)
            {
                foreach (var block in this.chatThread.Blocks.OrderBy(n => n.Time))
                {
                    @if (!block.HideFromUser)
                    {
                        <ContentBlockComponent Role="@block.Role" Type="@block.ContentType" Time="@block.Time" Content="@block.Content"/>
                    }
                }
            }
            
            <div id="@AFTER_RESULT_DIV_ID" class="mt-3">
            </div>
        </ChildContent>
        <FooterContent>
            <MudStack Row="@true" Wrap="Wrap.Wrap" Class="ma-1">
                
                @if (!this.FooterButtons.Any(x => x.Type is ButtonTypes.SEND_TO))
                {
                    @if (this.ShowSendTo)
                    {
                        <MudMenu AnchorOrigin="Origin.TopLeft" TransformOrigin="Origin.BottomLeft" StartIcon="@Icons.Material.Filled.Apps" EndIcon="@Icons.Material.Filled.KeyboardArrowDown" Label="@TB("Send to ...")" Variant="Variant.Filled" Style="@this.GetSendToColor()" Class="rounded">
                            @foreach (var assistant in Enum.GetValues<Components>().Where(n => n.AllowSendTo()).OrderBy(n => n.Name().Length))
                            {
                                <MudMenuItem OnClick="() => this.SendToAssistant(assistant, new())">
                                    @assistant.Name()
                                </MudMenuItem>
                            }
                        </MudMenu>
                    }
                }

                @foreach (var button in this.FooterButtons)
                {
                    switch (button)
                    {
                        case ButtonData buttonData when !string.IsNullOrWhiteSpace(buttonData.Tooltip):
                            <MudTooltip Text="@buttonData.Tooltip">
                                <MudButton Variant="Variant.Filled" Color="@buttonData.Color" Disabled="@buttonData.DisabledAction()" StartIcon="@GetButtonIcon(buttonData.Icon)" OnClick="async () => await buttonData.AsyncAction()">
                                    @buttonData.Text
                                </MudButton>
                            </MudTooltip>
                            break;

                        case ButtonData buttonData:
                            <MudButton Variant="Variant.Filled" Color="@buttonData.Color" Disabled="@buttonData.DisabledAction()" StartIcon="@GetButtonIcon(buttonData.Icon)" OnClick="async () => await buttonData.AsyncAction()">
                                @buttonData.Text
                            </MudButton>
                            break;

                        case SendToButton sendToButton:
                            <MudMenu AnchorOrigin="Origin.TopLeft" TransformOrigin="Origin.BottomLeft" StartIcon="@Icons.Material.Filled.Apps" EndIcon="@Icons.Material.Filled.KeyboardArrowDown" Label="@TB("Send to ...")" Variant="Variant.Filled" Style="@this.GetSendToColor()" Class="rounded">
                                @foreach (var assistant in Enum.GetValues<Components>().Where(n => n.AllowSendTo()).OrderBy(n => n.Name().Length))
                                {
                                    <MudMenuItem OnClick="() => this.SendToAssistant(assistant, sendToButton)">
                                        @assistant.Name()
                                    </MudMenuItem>
                                }
                            </MudMenu>
                            break;
                    }
                }
                
                @if (this.ShowCopyResult)
                {
                    <MudButton Variant="Variant.Filled" StartIcon="@Icons.Material.Filled.ContentCopy" OnClick="() => this.CopyToClipboard()">
                        @TB("Copy result")
                    </MudButton>
                }

                @if (this.ShowReset)
                {
                    <MudButton Variant="Variant.Filled" Style="@this.GetResetColor()" StartIcon="@Icons.Material.Filled.Refresh" OnClick="() => this.InnerResetForm()">
                        @TB("Reset")
                    </MudButton>
                }

                @if (this.SettingsManager.ConfigurationData.LLMProviders.ShowProviderConfidence)
                {
                    <ConfidenceInfo Mode="PopoverTriggerMode.BUTTON" LLMProvider="@this.providerSettings.UsedLLMProvider"/>
                }

                @if (this.AllowProfiles && this.ShowProfileSelection)
                {
                    <ProfileSelection MarginLeft="" @bind-CurrentProfile="@this.currentProfile"/>
                }
            </MudStack>
        </FooterContent>
    </InnerScrolling>
</div>