@inherits MSGComponentBase

<MudDialog>
    <DialogContent>
        @if (this.isInstallationInProgress)
        {
            <MudText Typo="Typo.h4" Class="mb-3">
                @T("Please wait for the installation to complete...")
            </MudText>
            <MudProgressLinear Color="Color.Primary" Indeterminate="@true" Size="Size.Large" Rounded="@true"/>
        }
        else if (this.ShowInstallationPage)
        {
            <div class="mb-4">
                <MudJustifiedText Typo="Typo.body1" Class="mb-2">
                    @T("AI Studio uses the free and open-source third-party app Pandoc to process and retrieve data from local Office files (such as Word files) or to generate Office documents.")
                </MudJustifiedText>
                <MudJustifiedText Typo="Typo.body1">
                    @T("Unfortunately, Pandoc's GPL license isn't compatible with the AI Studios licenses. However, software under the GPL is free to use and free of charge. You'll need to accept the GPL license before we can download and install Pandoc for you automatically (recommended). Alternatively, you might download it yourself using the instructions below or install it otherwise, e.g., by using a package manager of your operating system.")
                </MudJustifiedText>
            </div>

            <MudExpansionPanels Class="mb-3" MultiExpansion="@false" Outlined="false" Elevation="0">
                <ExpansionPanel HeaderIcon="@Icons.Material.Filled.Verified" HeaderText="@T("View the GNU General Public License v2 (GPL)")" MaxHeight="300" ExpandedChanged="this.WhenExpandingLicence">
                    @if (this.isLoadingLicence)
                    {
                        <MudSkeleton/>
                        <MudSkeleton Animation="Animation.Wave"/>
                        <MudSkeleton/>
                    }
                    else if (!string.IsNullOrWhiteSpace(this.licenseText))
                    {
                        <MudMarkdown Value="@this.licenseText" Props="Markdown.DefaultConfig"/>
                    }
                </ExpansionPanel>

                <ExpansionPanel HeaderIcon="@Icons.Material.Filled.AutoFixHigh" HeaderText="@T("Automatic installation")" IsExpanded="true">
                    <MudJustifiedText Typo="Typo.body1">
                        @T("""Pandoc is distributed under the GNU General Public License v2 (GPL). By clicking "Accept GPL and Install," you agree to the terms of the GPL license, and Pandoc will be installed automatically for you. Software under GPL is free of charge and free to use.""")
                    </MudJustifiedText>
                    <MudButton OnClick="this.InstallPandocAsync" Color="Color.Primary" Class="mt-4" Variant="Variant.Filled" Size="Size.Medium" StartIcon="@Icons.Material.Filled.InstallDesktop">
                        @T("Accept the GPL and start the installation")
                    </MudButton>
                </ExpansionPanel>

                <ExpansionPanel HeaderIcon="@Icons.Material.Filled.Build" HeaderText="@T("Manual installation")" ExpandedChanged="this.WhenExpandingManualInstallation">
                    <MudJustifiedText Typo="Typo.body1" Class="mb-2">
                        @T("If you want to install Pandoc yourself, follow one of these two guides. The first guide uses an installer and is available for Windows and macOS.  Alternatively, you might use a package manager of your operating system to install Pandoc.")
                    </MudJustifiedText>

                    <MudExpansionPanels Outlined="false" Elevation="0">
                        <ExpansionPanel HeaderIcon="@Icons.Material.Filled.AppRegistration" HeaderText="@T("Download installer")" IsExpanded="true">
                            <MudList T="string">
                                <MudListItem T="string" Class="mb-2">
                                    <MudJustifiedText Typo="Typo.body1">
                                        @T("Accept the terms of the GPL license and download the latest installer with the download button below. Eventually, you need to allow the download of the installer in the download window.")
                                    </MudJustifiedText>
                                    <CodeTabs @bind-SelectedIndex="this.selectedInstallerIndex">
                                        <CodeBlock Title="Windows">pandoc-@(LATEST_PANDOC_VERSION)-windows-x86_64.msi</CodeBlock>
                                        <CodeBlock Title="macOS ARM">pandoc-@(LATEST_PANDOC_VERSION)-arm64-macOS.pkg</CodeBlock>
                                        <CodeBlock Title="macOS x86">pandoc-@(LATEST_PANDOC_VERSION)-x86_64-macOS.pkg</CodeBlock>
                                    </CodeTabs>
                                </MudListItem>
                                <MudListItem T="string">
                                    <MudJustifiedText Typo="Typo.body1">
                                        @T("Execute the installer and follow the instructions.")
                                    </MudJustifiedText>
                                </MudListItem>
                            </MudList>

                            <MudJustifiedText Class="mb-3" Typo="Typo.body1">
                                @T("""Pandoc is distributed under the GNU General Public License v2 (GPL). By clicking "Accept GPL and download installer," you agree to the terms of the GPL license. Software under GPL is free of charge and free to use.""")
                            </MudJustifiedText>

                            <MudButton Href="@this.downloadUrlInstaller" Target="_blank" Color="Color.Primary" Class="mt-4" Variant="Variant.Filled" Size="Size.Medium" StartIcon="@Icons.Material.Filled.Downloading">
                                @T("Accept the GPL and download the installer")
                            </MudButton>
                        </ExpansionPanel>

                        <ExpansionPanel HeaderIcon="@Icons.Material.Outlined.Archive" HeaderText="Download archive">
                            <MudList T="string">
                                <MudListItem T="string" Class="mb-2">
                                    <MudJustifiedText Typo="Typo.body1">
                                        @T("Accept the terms of the GPL license and download the latest archive with the download button below.")
                                    </MudJustifiedText>
                                </MudListItem>

                                <MudListItem T="string" Class="mb-2">
                                    <MudJustifiedText Typo="Typo.body1">
                                        @T("Extract the archive to a folder of your choice, e.g.:")
                                    </MudJustifiedText>
                                    <CodeTabs @bind-SelectedIndex="this.selectedArchiveIndex">
                                        <CodeBlock Title="Windows">C:\Users\%USERNAME%\pandoc</CodeBlock>
                                        <CodeBlock Title="macOS">~/pandoc</CodeBlock>
                                        <CodeBlock Title="Linux">/usr/local/bin/pandoc</CodeBlock>
                                    </CodeTabs>
                                </MudListItem>

                                <MudListItem T="string" Class="mb-2">
                                    <MudJustifiedText Typo="Typo.body1">
                                        @T("Open the folder and copy the full path to the folder where your Pandoc executable file is into your clipboard:")
                                    </MudJustifiedText>
                                    <CodeTabs @bind-SelectedIndex="this.selectedArchiveIndex">
                                        <CodeBlock Title="Windows">C:\Users\%USERNAME%\pandoc\pandoc-@(LATEST_PANDOC_VERSION)</CodeBlock>
                                        <CodeBlock Title="macOS">~/pandoc/pandoc-@(LATEST_PANDOC_VERSION)</CodeBlock>
                                        <CodeBlock Title="Linux">/usr/local/bin/pandoc/pandoc-@(LATEST_PANDOC_VERSION)</CodeBlock>
                                    </CodeTabs>
                                </MudListItem>

                                <MudListItem T="string">
                                    <MudJustifiedText Typo="Typo.body1">
                                        @T("Add the copied path to your system's or user's PATH environment variable, restart your terminal, and check the installation:")
                                    </MudJustifiedText>
                                    <CodeTabs @bind-SelectedIndex="this.selectedArchiveIndex">
                                        <CodeBlock Title="Windows">> pandoc.exe --version<br/>> pandoc @(LATEST_PANDOC_VERSION)</CodeBlock>
                                        <CodeBlock Title="macOS">> pandoc --version<br/>> pandoc @(LATEST_PANDOC_VERSION)</CodeBlock>
                                        <CodeBlock Title="Linux">> pandoc --version<br/>> pandoc @(LATEST_PANDOC_VERSION)</CodeBlock>
                                    </CodeTabs>
                                </MudListItem>
                            </MudList>

                            <MudText Class="mb-3" Typo="Typo.caption">
                                <MudJustifiedText Typo="Typo.body1">
                                    @T("""Pandoc is distributed under the GNU General Public License v2 (GPL). By clicking "Accept GPL and archive," you agree to the terms of the GPL license. Software under GPL is free of charge and free to use.""")
                                </MudJustifiedText>
                            </MudText>

                            <MudButton Href="@this.downloadUrlArchive" Target="_blank" Color="Color.Primary" Class="mt-4" Variant="Variant.Filled" Size="Size.Medium" StartIcon="@Icons.Material.Filled.Downloading">
                                @T("Accept the GPL and download the archive")
                            </MudButton>
                        </ExpansionPanel>
                    </MudExpansionPanels>
                </ExpansionPanel>
            </MudExpansionPanels>
        }
        else
        {
            <MudItem Class="px-8 py-2">
                @if (this.pandocInstallation == default)
                {
                    <MudStack Row="true" AlignItems="AlignItems.Center">
                        <MudSkeleton SkeletonType="SkeletonType.Circle" Width="4em" Height="4em" Animation="Animation.Pulse"/>
                        <MudStack Row="false" Class="d-flex flex-column flex-grow-1">
                            <MudSkeleton Width="90%" Animation="Animation.Wave"/>
                            <MudSkeleton Width="75%" Animation="Animation.Wave"/>
                            <MudSkeleton Width="55%"/>
                            <MudSkeleton Width="12%"/>
                        </MudStack>
                    </MudStack>
                }
                else
                {
                    switch (this.pandocInstallation)
                    {
                        case { CheckWasSuccessful: true, IsAvailable: true }:
                            <MudStack Row="true" AlignItems="AlignItems.Center">
                                <MudIcon Style="width: 2.5em; height: 2.5em;" Icon="@Icons.Material.Filled.Check" Color="Color.Success"/>
                                <MudJustifiedText Typo="Typo.body1">
                                    @T("Pandoc is installed and the version meets the requirements of MindWork AI Studio.")
                                </MudJustifiedText>
                            </MudStack>
                            break;

                        case { CheckWasSuccessful: true, IsAvailable: false }:
                            <MudStack Row="true" AlignItems="AlignItems.Center">
                                <MudIcon Style="width: 3.5em; height: 3.5em;" Icon="@Icons.Material.Filled.Error" Color="Color.Error"/>
                                <MudJustifiedText Typo="Typo.body1">
                                    @T("Pandoc is installed, but the version is too old and does not meet the requirements of MindWork AI Studio. AI Studio can help you install a newer version.")
                                </MudJustifiedText>
                            </MudStack>
                            break;

                        case { CheckWasSuccessful: false }:
                            <MudStack Row="true" AlignItems="AlignItems.Center">
                                <MudIcon Style="width: 3.5em; height: 3.5em;" Icon="@Icons.Material.Filled.Error" Color="Color.Error"/>
                                <MudJustifiedText Typo="Typo.body1">
                                    @T("Pandoc doesn't seem to be installed on this system. AI Studio can help you install the correct version.")
                                </MudJustifiedText>
                            </MudStack>
                            break;
                    }
                }
            </MudItem>
        }
    </DialogContent>
    <DialogActions>
        @if (this.isInstallationInProgress)
        {
            <MudButton OnClick="@this.Cancel" Variant="Variant.Filled">
                @T("Close")
            </MudButton>
        }
        else if (this.ShowInstallationPage)
        {
            <MudButton OnClick="@this.Cancel" Variant="Variant.Filled">
                @T("Close")
            </MudButton>
            
            <MudButton Color="Color.Warning" OnClick="@this.RejectLicense" Variant="Variant.Filled" Size="Size.Medium">
                @T("Reject the GPL licence")
            </MudButton>
        }
        else
        {
            <MudButton OnClick="@this.Cancel" Variant="Variant.Filled">
                @T("Close")
            </MudButton>

            @if (this.pandocInstallation != default)
            {
                switch (this.pandocInstallation)
                {
                    case { CheckWasSuccessful: true, IsAvailable: false }:
                    case { CheckWasSuccessful: false, IsAvailable: false }:
                        <MudButton Color="Color.Primary" OnClick="@this.ProceedToInstallation" Variant="Variant.Filled" Size="Size.Medium">
                            @T("Proceed to the installation")
                        </MudButton>
                        break;

                    case { CheckWasSuccessful: true, IsAvailable: true }:
                        <MudButton Color="Color.Default" OnClick="@this.ProceedToInstallation" Variant="Variant.Filled" Size="Size.Medium">
                            @T("Re-install Pandoc")
                        </MudButton>
                        break;
                }
            }
        }
    </DialogActions>
</MudDialog>