<#
.SYNOPSIS
    A short one-line action-based description, e.g. 'Tests if a function is valid'
.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 -Verbose
    Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>

function Remove-ITDPassword {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = "Title")]
        [ValidateNotNullOrEmpty()]
        [String]
        $Title,

        [switch]
        $Force,

        [PSCredential]
        $Credential
    )
    
    begin {
        
    }
    
    process {
        # Find the passwords
        $GetITDPasswordParams = @{
            Title = $Title;
        }
        If ($PSBoundParameters.ContainsKey('Credential')) {
            $InvokeRestMethodParams += @{Credential = $Credential }
        }
        Else {
            $InvokeRestMethodParams += @{UseDefaultCredentials = $true }
        }

        $ExistingRecords = Get-ITDPassword @GetITDPasswordParams

        switch ($ExistingRecords.count) {
            { [int]0 } {
                Write-Warning -Message "Title $Title not found."
            }
            { $_ -ge 1 } {
                If (-not $Force) {
                    $VerifyString = ( ([string]@($ExistingRecords).Count) + " record(s) have been found, all will be removed.")
                    $question = 'Are you sure you want to proceed?'
    
                    $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
                    $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
                    $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
    
                    $decision = $Host.UI.PromptForChoice($VerifyString, $question, $choices, 1)                    
                }
                if ($decision -eq 0 -or $Force -eq $true) {
                    ForEach ($ExistingRecord in $ExistingRecords) {
                        $Uri = ("https://itdpv.nd.gov/winapi/passwords/" + $ExistingRecord.PasswordID + "?MoveToRecycleBin=True")

                        $InvokeRestMethodParams = @{
                            Method      = 'Delete';
                            Uri         = $Uri;
                            ContentType = 'application/json';
                        }
                        If ($PSBoundParameters.ContainsKey('Credential')) {
                            $InvokeRestMethodParams += @{Credential = $Credential }
                        }
                        Else {
                            $InvokeRestMethodParams += @{UseDefaultCredentials = $true }
                        }

                        Invoke-RestMethod @InvokeRestMethodParams
                    }
                }
                else {
            
                }
            }
        }

        
        
    }
    
    end {
        
    }
}