. "$PSScriptRoot/Config.ps1";
. "$PSScriptRoot/Operations.ps1";
. "$PSScriptRoot/System.ps1";
. "$PSScriptRoot/../Types/InstallerAction.ps1";

$null = New-Module {
    . "$PSScriptRoot/BrowserAutomation.ps1";
    . "$PSScriptRoot/SoftwareManagement.ps1";
    . "$PSScriptRoot/../Types/InstallerAction.ps1";
    $userArgument = "name";

    <#
        .SYNOPSIS
        Installs the specified packages using chocolatey.

        .PARAMETER Names
        The names of the packages to install.
    #>
    function Install-ChocoPackage {
        param(
            [switch] $Force,
            [string[]] $ArgumentList,
            [Parameter(Position=0)]
            [string] $Name,
            [Parameter(ValueFromRemainingArguments = $true)]
            [string[]] $AdditionalNames = @()
        )

        [System.Collections.ArrayList] $Names = @();
        $null = $Names.Add($Name);
        $Names.AddRange($AdditionalNames);

        if (-not ($Force.IsPresent)) {
            for ($i = $Names.Count - 1; $i -ge 0; $i--) {
                $name = $Names[$i];

                if (Test-ChocoPackage $name) {
                    Write-Host "Package ``$name`` is already installed"
                    $Names.RemoveAt($i);
                }
            }
        }

        if ($Names.Count -ge 1) {
            choco install -y $ArgumentList $Names;
        }
    }

    <#
        .SYNOPSIS
        Installs the specified packages using `winget`.

        .PARAMETER Names
        The names of the packages to install.
    #>
    function Install-WingetPackage {
        param(
            [switch] $Force,
            [string[]] $ArgumentList,
            [Parameter(Position=0)]
            [string] $Name,
            [Parameter(ValueFromRemainingArguments = $true)]
            [string[]] $AdditionalNames = @()
        )

        [System.Collections.ArrayList] $Names = @();
        $null = $Names.Add($Name);
        $Names.AddRange($AdditionalNames);

        foreach ($name in $Names) {
            if ($Force.IsPresent -or -not (Test-WingetPackage $name)) {
                winget install `
                    --accept-source-agreements --accept-package-agreements `
                    --source winget `
                    --exact --id $name $ArgumentList;
            } else {
                Write-Host "Package ``$name`` is already installed"
            }
        }
    }

    <#
        .SYNOPSIS
        Installs a setup package from the specified source.

        .PARAMETER Source
        The location of the setup package.

        .PARAMETER ArgumentList
        The arguments to pass to the setup package.

        .PARAMETER Local
        A value indicating whether the setup package is stored locally.
    #>
    function Install-SetupPackage {
        param(
            [string] $Source,
            [string[]] $ArgumentList = @("/S"),
            [switch] $Local
        )

        [string] $dir = $null;
        [string] $filePath = $null;

        if (-not ($Local.IsPresent)) {
            $dir = New-TemporaryDirectory;
            Write-Host "Determining the file name of ``$Source``…";
            $fileName = ([uri]$Source).Segments[-1];
            Write-Host "Found name ``$fileName``";
            $filePath = Join-Path $dir $fileName;

            Write-Host "Downloading setup file from ``$Source``";
            Invoke-WebRequest $Source -OutFile $filePath;
        } else {
            $filePath = $Source;
        }

        Write-Host "Starting installation of ``$(Split-Path -Leaf $filePath)``";
        Start-Process -Wait -WorkingDirectory (Split-Path -Parent $filePath) -FilePath $filePath -ArgumentList $ArgumentList;

        if ($dir) {
            Remove-Item -Recurse $dir;
        }
    }

    <#
        .SYNOPSIS
        Installs a package downloaded from ASUS.

        .PARAMETER URL
        The URL to download the package from.
    #>
    function Install-AsusPackage {
        param(
            [string] $URL
        )

        $file = "AsusPackage.zip";
        $dir = New-TemporaryDirectory;
        $unpackDir = New-TemporaryDirectory;

        $null = Push-Location $dir;
        Invoke-WebRequest $URL -OutFile $file;
        Expand-Archive $file $unpackDir;
        $null = Pop-Location;
        Remove-Item -Recurse $dir;

        $null = Start-Process -Wait -WorkingDirectory $unpackDir -FilePath (Join-Path $unpackDir "AsusSetup.exe") -ArgumentList "/S";
        Remove-Item -Recurse $unpackDir;
    }

    <#
        .SYNOPSIS
        Downloads and installs a package from the AMD website.

        .PARAMETER URL
        The URL to download the package from.
    #>
    function Install-AmdPackage {
        param(
            [string] $URL
        )

        $dir = New-TemporaryDirectory;
        $cookieBannerSelector = "#onetrust-consent-sdk";
        $osSelector = "div[id$='oscategory']>div[id$='-0']";
        $downloadSelector = "$osSelector div[id$='panel'] .button a";

        $file = Start-CustomBrowserDownload @PSBoundParameters -OutDir $dir -Action {
            param(
                [OpenQA.Selenium.Firefox.FirefoxDriver] $Browser
            )

            $osContainer = $Browser.FindElement([OpenQA.Selenium.By]::CssSelector($osSelector));

            if (-not ([bool] $osContainer.GetAttribute("cmp-expanded"))) {
                $osContainer.Click();
            }

            $download = {
                $browser.FindElement([OpenQA.Selenium.By]::CssSelector($downloadSelector)).Click();
            };

            try {
                & $download;
            } catch {
                $null = $Browser.ExecuteScript("document.querySelector('$cookieBannerSelector').remove()");
                & $download;
            }
        };

        Start-Process -Wait -WorkingDirectory (Split-Path -Parent "$file") -FilePath "$file" -ArgumentList "/S";
        Remove-Item -Recurse $dir;
    }

    function Start-SoftwareInstaller {
        param(
            [string] $Name,
            [scriptblock] $Installer = $null,
            [scriptblock] $Configurator = $null,
            [scriptblock] $UserConfigurator = $null,
            [Nullable[InstallerAction]] $Action,
            [hashtable] $Arguments
        )

        [InstallerAction] $Action = & {
            if ($Action.HasValue) {
                $Action;
            } else {
                [InstallerAction]::Install;
            }
        };

        if (-not $Name) {
            $Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName));
        }

        Start-Operation {
            if ($null -ne $Name) {
                $Name = "``$Name``";
            } else {
                $Name = "unknown software";
            }

            $installHandler = {
                param(
                    [InstallerAction] $Action,
                    [hashtable] $Arguments
                )

                $Arguments ??= @{ };

                $argumentList = @{
                    installer = $installHandler;
                    arguments = $Arguments;
                };

                if ($action -eq ([InstallerAction]::Install)) {
                    if ($Installer) {
                        Write-Host "Installing $Name…";
                        & $Installer @argumentList;
                    }
                    # ToDo: Automatically configure after installation
                } elseif ($Action -eq ([InstallerAction]::Configure)) {
                    if ($Configurator) {
                        Write-Host "Configuring $Name…";
                        & $Configurator @argumentList;
                    }

                    if (-not (Test-SetupUser)) {
                        $argumentList.Add("action", [InstallerAction]::ConfigureUser);
                        & $installHandler @argumentList;
                    }
                } elseif ($Action -eq ([InstallerAction]::ConfigureUser)) {
                    if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) {
                        $argumentList.Add($userArgument, ($env:UserName));
                    }

                    if ($UserConfigurator) {
                        Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``…";
                        & $UserConfigurator @argumentList;
                    }
                }
            };

            & $installHandler -Action $Action -Arguments $Arguments;
        };
    }
}