
using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'heatman' -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $commandElements = $commandAst.CommandElements
    $command = @(
        'heatman'
        for ($i = 1; $i -lt $commandElements.Count; $i++) {
            $element = $commandElements[$i]
            if ($element -isnot [StringConstantExpressionAst] -or
                $element.StringConstantType -ne [StringConstantType]::BareWord -or
                $element.Value.StartsWith('-') -or
                $element.Value -eq $wordToComplete) {
                break
        }
        $element.Value
    }) -join ';'

    $completions = @(switch ($command) {
        'heatman' {
            [CompletionResult]::new('-d', '-d', [CompletionResultType]::ParameterName, 'Destination path for the output image')
            [CompletionResult]::new('--dest', '--dest', [CompletionResultType]::ParameterName, 'Destination path for the output image')
            [CompletionResult]::new('-a', '-a', [CompletionResultType]::ParameterName, 'Assistant line gap in cells. If 0, no assistant line will be drawn.')
            [CompletionResult]::new('--assistant-line-gap', '--assistant-line-gap', [CompletionResultType]::ParameterName, 'Assistant line gap in cells. If 0, no assistant line will be drawn.')
            [CompletionResult]::new('-p', '-p', [CompletionResultType]::ParameterName, 'Pixel size of of cells')
            [CompletionResult]::new('--pixel', '--pixel', [CompletionResultType]::ParameterName, 'Pixel size of of cells')
            [CompletionResult]::new('-m', '-m', [CompletionResultType]::ParameterName, 'Output mode')
            [CompletionResult]::new('--mode', '--mode', [CompletionResultType]::ParameterName, 'Output mode')
            [CompletionResult]::new('-l', '-l', [CompletionResultType]::ParameterName, 'Logging level (trace, debug, info, warn, error)')
            [CompletionResult]::new('--level', '--level', [CompletionResultType]::ParameterName, 'Logging level (trace, debug, info, warn, error)')
            [CompletionResult]::new('-r', '-r', [CompletionResultType]::ParameterName, 'Specify the value range for the input data.')
            [CompletionResult]::new('--range', '--range', [CompletionResultType]::ParameterName, 'Specify the value range for the input data.')
            [CompletionResult]::new('--order', '--order', [CompletionResultType]::ParameterName, 'The file describing the order of the data to be plotted. Each line should contain a single string that matches the name of the data to be plotted. The triple dash line (---) means the assistant line is drawin here. The # is the comment line. escape the # with \# if you want to use it as a name. If not provided, the order will be determined by the order of the data in the input files.')
            [CompletionResult]::new('--row-order', '--row-order', [CompletionResultType]::ParameterName, 'The file describing the order of the rows to be plotted.')
            [CompletionResult]::new('--column-order', '--column-order', [CompletionResultType]::ParameterName, 'The file describing the order of the columns to be plotted.')
            [CompletionResult]::new('-h', '-h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
            [CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
            [CompletionResult]::new('-V', '-V ', [CompletionResultType]::ParameterName, 'Print version')
            [CompletionResult]::new('--version', '--version', [CompletionResultType]::ParameterName, 'Print version')
            break
        }
    })

    $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
        Sort-Object -Property ListItemText
}
