<#
.SYNOPSIS
    Retrive password from ITD Passwordstate
.DESCRIPTION
    A longer description of the function, its purpose, common use cases, etc.
.NOTES
    Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
    Specify a URI to a help page, this will show when Get-Help -Online is used.
.EXAMPLE
    Test-MyTestFunction -VerboseZM
    Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>

function Get-ITDPassword {
    [CmdletBinding()]
    param (
        [string]
        $Title,
        
        [string]
        $UserName,

        [PSCredential]
        $Credential,

        [Parameter(ParameterSetName = "ToClipboard")]
        [switch]
        $ToClipboard,
        
        [switch]
        $FullRecord
    )
    
    begin {
        
    }
    
    process {
        $Uri = 'https://itdpv.nd.gov/winapi/searchpasswords/?'
        
        If ($PSBoundParameters.ContainsKey('Title')) {
            $Uri += 'title=' + $Title + '&'
        }
        If ($PSBoundParameters.ContainsKey('Username')) {
            $Uri += 'username=' + "$UserName" + '&'
        }

        $Uri = $Uri.TrimEnd('&')

        $InvokeRestMethodParams = @{
            Method             = 'Get';
            Uri                = $Uri;
            SkipHttpErrorCheck = $true;
        }
        If ($PSBoundParameters.ContainsKey('Credential')) {
            $InvokeRestMethodParams += @{Credential = $Credential }
        }
        Else {
            $InvokeRestMethodParams += @{UseDefaultCredentials = $true }
        }
        $InvokeResult = Invoke-RestMethod @InvokeRestMethodParams

        switch ($InvokeResult) {
            { $_ -eq $null } {
                Write-Error -Message "No password found"
                Break
            }
            { $_ | Get-Member -MemberType Properties | Where-Object { $_.Name -eq 'errors' } } {
                If ($InvokeResult.errors.message -eq 'Not found') {
                    Write-Warning -Message "Search for Password records return zero results"
                }
            }
            Default {
                $OutResult = $InvokeResult | Select-Object PasswordListID, PasswordList, PasswordID, Title, Description, UserName, @{n = 'SecurePassword'; e = { $_.Password | ConvertTo-SecureString -AsPlainText -Force } }, AccountTypeId, AccountType

                If (@($OutResult).count -eq 1) {
                    If ($PSCmdlet.ParameterSetName -eq "ToClipboard") {
                        $InvokeResult.Password | Set-Clipboard
                    }
                    If ($FullRecord) {
                        Write-Output $OutResult
                    }
                    Else {
                        $OutCred = New-Object System.Management.Automation.PSCredential($OutResult.UserName, $OutResult.SecurePassword)
                        Write-Output $OutCred
                    }
                }
                Else {
                    Write-Output $OutResult
                }
            }
        }
    }
    
    end {
        
    }
}