@using AIStudio.Tools.PluginSystem
@using AIStudio.Tools.PluginSystem.Assistants
@using AIStudio.Tools.Services
@inherits MSGComponentBase
@attribute [Route(Routes.PLUGINS)]

<div class="inner-scrolling-context">
    <MudText Typo="Typo.h3" Class="mb-2">
        @T("Plugins")
    </MudText>
    
    <InnerScrolling>
        
        <MudTable Items="@PluginFactory.AvailablePlugins" Hover="@true" GroupBy="@this.groupConfig" Class="border-dashed border rounded-lg">
            <ColGroup>
                <col style="width: 2em;" />
                <col style="width: 2.1em; "/>
                <col/>
                <col style="width: 12em;"/>
            </ColGroup>
            <HeaderContent>
                <MudTh colspan="2">@T("Plugins")</MudTh>
                <MudTh>@T("Actions")</MudTh>
            </HeaderContent>
            <GroupHeaderTemplate>
                <MudTh Class="mud-table-cell-custom-group" colspan="3">
                    @switch (context.Key)
                    {
                        case GROUP_ENABLED:
                            <MudText Typo="Typo.h6" Class="mb-2">
                                @T("Enabled Plugins")
                            </MudText>
                            break;
                            
                        case GROUP_DISABLED:
                            <MudText Typo="Typo.h6" Class="mb-2">
                                @T("Disabled Plugins")
                            </MudText>
                            break;
                            
                        case GROUP_INTERNAL:
                            <MudText Typo="Typo.h6" Class="mb-2">
                                @T("Internal Plugins")
                            </MudText>
                            break;
                    }
                </MudTh>
            </GroupHeaderTemplate>
            <RowTemplate>
                <MudTd>
                    <MudAvatar Size="Size.Medium" Style="width: 2em; height: 2em;" Class="align-content-stretch">
                        <div class="plugin-icon-container">
                            @((MarkupString)context.IconSVG)
                        </div>
                    </MudAvatar>
                </MudTd>
                <MudTd>
                    <MudStack Spacing="0">
                        <MudText Typo="Typo.h6" Class="mb-0 pb-0 gap-0">
                            @context.Name
                        </MudText>
                        <MudText Typo="Typo.body1">
                            @context.Description
                        </MudText>
                    </MudStack>
                </MudTd>
                <MudTd>
                    <MudStack Row="true" Spacing="0" AlignItems="AlignItems.Center">
                        @if (context.Type is PluginType.ASSISTANT)
                        {
                            var assistantPlugin = PluginFactory.RunningPlugins.OfType<PluginAssistants>().FirstOrDefault(x => x.Id == context.Id);
                            <AssistantPluginSecurityCard Plugin="@assistantPlugin" Compact="@true"/>
                        }
                        @if (context is { IsInternal: false, Type: not PluginType.CONFIGURATION })
                        {
                            var isEnabled = this.SettingsManager.IsPluginEnabled(context);
                            var activationSwitchDisabled = this.IsActivationSwitchDisabled(context, isEnabled);
                            <MudTooltip Text="@this.GetActivationTooltip(context, isEnabled)">
                                <MudSwitch T="bool" Value="@isEnabled" ValueChanged="@(_ => this.PluginActivationStateChanged(context))" Disabled="@activationSwitchDisabled"/>
                            </MudTooltip>
                        }

                        <MudButtonGroup Class="ms-3">
                            @if (context is { IsInternal: false } && !string.IsNullOrWhiteSpace(context.SourceURL))
                            {
                                var sourceUrl = context.SourceURL;
                                var isSendingMail = IsSendingMail(sourceUrl);
                                if (isSendingMail)
                                {
                                    var isDefaultSupportContact = string.Equals(sourceUrl, AssistantPluginGenerationService.DEFAULT_SUPPORT_CONTACT, StringComparison.Ordinal);
                                    <MudTooltip Text="@(isDefaultSupportContact ? string.Empty : T("Send a mail"))">
                                                                                                                 <MudIconButton Icon="@Icons.Material.Filled.Email" Href="@sourceUrl" Target="_blank" Size="Size.Medium" Disabled="@isDefaultSupportContact"/>
                                    </MudTooltip>
                                }
                                else
                                {
                                    var isDefaultSourceUrl = string.Equals(sourceUrl, AssistantPluginGenerationService.DEFAULT_SOURCE_URL, StringComparison.Ordinal);
                                    <MudTooltip Text="@(isDefaultSourceUrl ? T("No source url available") : T("Open website"))">
                                        <MudIconButton Icon="@Icons.Material.Filled.OpenInBrowser" Href="@sourceUrl" Target="_blank" Size="Size.Medium" Disabled="@isDefaultSourceUrl"/>
                                    </MudTooltip>
                                }
                            }
                            
                            @if (context is IAvailablePlugin editablePlugin && CanEditAssistantPlugin(editablePlugin))
                            {
                                <MudTooltip Text="@T("Edit assistant plugin")">
                                    <MudIconButton Icon="@Icons.Material.Filled.Code" Size="Size.Medium" OnClick="@(() => this.OpenAssistantPluginEditorDialogAsync(editablePlugin))"/>
                                </MudTooltip>
                            }

                            @if (context is IAvailablePlugin revisionPlugin && CanReviseAssistantPlugin(revisionPlugin))
                            {
                                <MudTooltip Text="@T("Revise assistant plugin with AI")">
                                    <MudIconButton Icon="@Icons.Material.Filled.AutoMode" Size="Size.Medium" OnClick="@(() => this.OpenAssistantPluginRevisionDialogAsync(revisionPlugin))"/>
                                </MudTooltip>
                            }

                            @if (context is IAvailablePlugin availablePlugin)
                            {
                                <AssistantPluginDeleteAction Plugin="@availablePlugin" />
                            }
                        </MudButtonGroup>
                    </MudStack>
                </MudTd>
            </RowTemplate>
        </MudTable>
    </InnerScrolling>
</div>
