using namespace System.Management.Automation

Class ServiceStarted : System.Management.Automation.IValidateSetValuesGenerator {
    [string[]] GetValidValues() {
        $ServicesListStarted = Get-Service | Where-Object Status -eq 'Running'

        return [string[]]$ServicesListStarted.Name
    }
}

function Restart-ServiceRunning {
    [CmdletBinding()]
    Param (
        [ValidateSet([ServiceStarted])]
        [string[]]$Name
    )

    Restart-Service @PSBoundParameters
}

Class ServiceStartedCompleter : System.Management.Automation.IArgumentCompleter {
    [System.Collections.IEnumerable[CompletionResult]] CompleteArgument(
        [string]$CommandName,
        [string]$ParameterName,
        [string]$WordToComplete,
        [Language.CommandAst] $CommandAst,
        [System.Collections.IDictionary] $FakeBoundParameters
    ) {
        $wildcardName = ("*" + $WordToComplete + "*")
        $CompletionResults = [System.Collections.Generic.List[CompletionResult]]::new()
        Get-Service -Name $wildcardName | 
            Where-Object Status -eq 'Running' |
            ForEach-Object { $CompletionResults.Add([CompletionResult]::new("'"+ $_.Name + "'"))}
        return $CompletionResults
    }
}
Class IDArgumentCompleter : IArgumentCompleter {
    [System.Collections.IEnumerable[CompletionResult]] CompleteArgument(
        [string]$CommandName,
        [string]$ParameterName,
        [string]$WordToComplete,
        [Language.CommandAst] $CommandAst,
        [System.Collections.IDictionary] $FakeBoundParameters
    ) {
        $wildcardName = ("*" + $WordToComplete + "*")
        $CompletionResults = [System.Collections.Generic.List[CompletionResult]]::new()
        $allID = Get-AdUser -Filter * | Where-Object SamAccountName -match '^a[0-9]{6}$' | ForEach-Object {
            [int]($_.SamAccountName -replace 'a')
        } | Sort-Object
    
        $lastId = $allID[-1]

        # les 10 prochains ID :
        1..10 | ForEach-Object {$CompletionResults.Add([CompletionResult]::new($lastId + $_))}

        return $CompletionResults
    }
}

[ArgumentCompleter( {
    param ( $command,
            $parameterName,
            $wordToComplete,
            $commandAst,
            $fakeBoundParameters )
    $allID = Get-AdUser -Filter * | Where-Object SamAccountName -match '^a[0-9]{6}$' | ForEach-Object {
        [int]($_.SamAccountName -replace 'a')
    } | Sort-Object

    $lastId = $allID[-1]

    # les 10 prochains ID :
    1..10 | ForEach-Object {$lastId + $_}
} )]
[int]$ID


function New-MyPSADUser {
    param (
        [parameter(
            Mandatory = $true
        )]
        [String]$Name,

        [parameter(
            Mandatory = $true
        )]
        [securestring]$AccountPassword,

        [parameter(
            Mandatory = $true,
            ParameterSetName = 'Administrative'
        )]
        [ArgumentCompleter( {
            param ( $command,
                    $parameterName,
                    $wordToComplete,
                    $commandAst,
                    $fakeBoundParameters )
            $allID = Get-AdUser -Filter * | Where-Object SamAccountName -match '^a[0-9]{6}$' | ForEach-Object {
                [int]($_.SamAccountName -replace 'a')
            } | Sort-Object

            $lastId = $allID[-1]

            # les 10 prochains ID :
            1..10 | ForEach-Object {$lastId + $_}
        } )]
        [int]$ID
    )
    # Instruction de création d'utilisateur
}
