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

$null = New-Module {
    . "$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,
            [Parameter(ValueFromRemainingArguments = $true)]
            [string[]] $Names
        )

        [System.Collections.ArrayList] $Names = $Names;

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

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

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

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

        foreach ($name in $Names) {
            if ($Force.IsPresent -and -not (Test-WingetPackage $name)) {
                winget install --accept-source-agreements --accept-package-agreements -e --id $name;
            }
        }
    }

    <#
        .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;
    }

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

        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)) {
                    Write-Host "Installing $Name…";
                    & $Installer @argumentList;
                } elseif ($Action -eq ([InstallerAction]::Configure)) {
                    Write-Host "Configuring $Name…";
                    & $Configurator @argumentList;

                    foreach ($user in Get-Users) {
                        $Arguments.Add($userArgument, $user);
                        $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));
                    }

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

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