<#
.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 Update-ITDPassword {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = "Id")]
        [ValidateRange(1, [UInt32]::MaxValue)]
        [Int]$Id,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = "Title")]
        [ValidateNotNullOrEmpty()]
        [String]$Title,

        [String]
        $Notes,

        [switch]
        $AppendNotes,

        [PSCredential]
        $Credential,

        [switch]
        $Force,

        [switch]
        $All
    )
    
    begin {
        
    }
    
    process {
        $GetITDPasswordParams = @{
            Title = $Title;
        }
        If ($PSBoundParameters.ContainsKey('Credential')) {
            $InvokeRestMethodParams += @{Credential = $Credential }
        }
        Else {
            $InvokeRestMethodParams += @{UseDefaultCredentials = $true }
        }

        $ExistingRecords = Get-ITDPassword @GetITDPasswordParams

        If (-not $Force) {
            $title = ( ([string]@($ExistingRecords).Count) + " record(s) have been found, all will be modified.")
            $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($title, $question, $choices, 1)
        }

        if ($Force -eq $true -or $decision -eq 0) {
            ForEach ($ExistingRecord in $ExistingRecords) {

                $PasswordObj = @{
                    'PasswordID' = $ExistingRecord.PasswordID
                }

                switch ($PSBoundParameters.Keys) {
                    'Notes' {
                        if ($PSBoundParameters.AppendNotes) {
                            $PasswordObj.Notes = $ExistingRecord.Notes + "<div>&nbsp;</div>$($PSBoundParameters.Notes)"
                        }
                        else {
                            $PasswordObj.Notes = $PSBoundParameters.Notes
                        }
                    }
                }

                $InvokeRestMethodParams = @{
                    Method      = 'Put';
                    Uri         = 'https://itdpv.nd.gov/winapi/passwords';
                    ContentType = 'application/json';
                    Body        = ($PasswordObj | ConvertTo-Json);
                }
                If ($PSBoundParameters.ContainsKey('Credential')) {
                    $InvokeRestMethodParams += @{Credential = $Credential }
                }
                Else {
                    $InvokeRestMethodParams += @{UseDefaultCredentials = $true }
                }

                $InvokeRestMethodParams.Body

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