using namespace System.Management.Automation.Host;
using namespace System.Security.Principal;

$null = New-Module {
    . "$PSScriptRoot/../../Common/Scripts/Config.ps1";
    . "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
    . "$PSScriptRoot/../../Common/Types/OneShotTask.ps1";
    $loggedInUserOption = "LoggedInUser";

    <#
        .SYNOPSIS
        Creates a new user for the PortValhalla setup.

        .PARAMETER Name
        The name of the user to create.
    #>
    function New-ValhallaUser {
        param(
            [string] $Name
        )

        function Add-MicrosoftAccount {
            param(
                [string] $Name
            )

            $newUser = & {
                while ($true) {
                    $currentUsers = Get-LocalUser | ForEach-Object { $_.Name };

                    Write-Host (
                        @(
                            "So… Windows is too dumb to create users which are bound to a Microsoft Account.",
                            "Thus, you have to do it by yourself.",
                            "So sorry…") -join "`n");

                    Write-Host "Create a user for ``$Name`` manually… (because Windows is too stupid)";
                    $null = Read-Host "Hit enter once you're done";

                    $newUsers = @(Get-LocalUser | Where-Object { -not ($currentUsers -contains $_.Name) });

                    if ($newUsers.Count) {
                        if ($newUsers.Count -eq 1) {
                            $newUser = $newUsers[0];

                            Write-Host "Found new user ``$newUser``";

                            if (
                                $Host.UI.PromptForChoice(
                                    "Confirm",
                                    "Is ``$newUser`` your user?",
                                    [ChoiceDescription[]]@(
                                        [ChoiceDescription]::new("&No", "``$newUser`` is not your user"),
                                        [ChoiceDescription]::new("&Yes", "``$newUser`` is your user")),
                                    0) -eq 1) {
                                return $newUser;
                            }
                        } else {
                            $result = $Host.UI.PromptForChoice(
                                "Select your User",
                                "Which one is your user?",
                                [ChoiceDescription[]](
                                    & {
                                        [ChoiceDescription]::new("&None", "None of these users is yours");

                                        for ($i = 0; $i -lt $newUsers.Count; $i++) {
                                            $name = "$($newUsers[$i])";
                                            [ChoiceDescription]::new("&$($i + 1) - ``$name``", "Your user is ``$name``");
                                        }
                                    }), 0);

                            if ($result -gt 0) {
                                return $newUsers[$result - 1];
                            }
                        }
                    } else {
                        Write-Host "";
                        Write-Host "Unable to determine the new user";
                        Write-Host "Retrying…";
                    }
                }
            };

            Set-MSAccountName ([string]$newUser);
        }

        $msAccount = Get-UserConfig -UserName $Name "microsoftAccount";

        if ($msAccount) {
            if (Test-Admin) {
                Write-Host "Preparing environment for creating MS Account";
                Enable-OneShotListener;
                Enable-UAC;
                Restart-Intermediate -CurrentUser;
                exit;
            }
        }

        Write-Host "Creating personal user ``$Name``…";

        if ($msAccount) {
            Add-MicrosoftAccount $Name;
            Set-SetupOption $loggedInUserOption $env:UserName;
            Invoke-OneShot ([OneShotTask]::InitializeMSAccount);
        } else {
            New-LocalUser -NoPassword $Name;
            Set-LocalUser $Name -PasswordNeverExpires $true;
            Set-LocalUser $Name -PasswordNeverExpires $false;
            Initialize-UserCreation;
        }
    }

    <#
        .SYNOPSIS
        Prepares the first login for initializing the current user under configuration.
    #>
    function Initialize-UserCreation {
        $name = (@(Get-Users))[(Get-CurrentUser)];
        $msAccount = Get-UserConfig -UserName $name "microsoftAccount";
        $displayName = Get-UserConfig -UserName $Name "displayName";

        Write-Host "Initializing user ``$name``…";

        $userArguments = @{
            name = $name;
        };

        if ($displayName) {
            $userArguments.fullName = $displayName;
        }

        $adminGroup = @{
            SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null);
        };

        if ($msAccount) {
            $accountName = Get-MSAccountName;
            Write-Host "Renaming ``$accountName`` to ``$name``…"
            Rename-LocalUser $accountName $name;
        }

        Set-LocalUser @userArguments;

        if ($msAccount) {
            Disable-LocalUser (Get-SetupOption $loggedInUserOption);
        } else {
            Disable-LocalUser $env:UserName;
        }

        Add-LocalGroupMember `
            @adminGroup `
            $name `
            -ErrorAction SilentlyContinue;

        if ($msAccount) {
            Disable-Autologin;
            Set-BootMessage -Caption "Please Log In" -Message "Please log in using your new Microsoft Account ``$name``.";
        } else {
            Set-AutologinUser "$name";
        }
    }
};