Powerdex.psm1

function Read-DexEntry {
    <#
    .SYNOPSIS
    Given a Pokemon's number or name, returns and reads a description of the Pokemon aloud.
    .DESCRIPTION
    Given a Pokemon's number or name, returns and reads a description of the Pokemon aloud.
    .PARAMETER Id
    Pokedex number of the Pokemon
    .PARAMETER Name
    Name of the Pokemon
    .EXAMPLE
    Look up a Pokemon by their number
    Read-DexEntry -Id 1
    .EXAMPLE
    Look up a Pokemon by their name
    Read-DexEntry -Name "Bulbasaur"
    .NOTES
    Currently, the voice functionality only works with Windows Powershell and Powershell Core on macOS
    #>

    [CmdletBinding(DefaultParameterSetName="ById")]
    [OutputType([string])]
    param (
        [Parameter(Mandatory=$true, ParameterSetName="ById")]
        [int]
        $Id,

        [Parameter(Mandatory=$true, ParameterSetName="ByName")]
        [string]
        $Name
    )

    switch ($PSCmdlet.ParameterSetName) {
        "ById" { $resource = $Id }
        "ByName" { $resource = $Name.ToLower() }
    }

    $pokeApiUrl = "https://pokeapi.co/api/v2/pokemon-species/$resource"

    try {
        $pokeData = Invoke-RestMethod -Uri $pokeApiUrl -Method "GET"
    } catch {
        return "Pokemon not found."
    }

    $name = $pokeData.name.Substring(0,1).ToUpper() + $pokeData.name.Substring(1)
    $color = $pokeData.color.name
    $shape = $pokeData.shape.name
    $title = "$name, the $color $shape pokemon"

    $engFlavorTextEntries = $pokeData.flavor_text_entries | Where-Object { $_.language.name -eq "en" }
    $flavorTextEntry = @($engFlavorTextEntries)[0].flavor_text -replace "\n", " "
    
    $completeEntry = "$title. $flavorTextEntry"

    if ($PSVersionTable["PSEdition"] -eq "Desktop") {
        Add-Type -AssemblyName System.speech
        $speechSynthesizer = New-Object System.Speech.Synthesis.SpeechSynthesizer

        $speechSynthesizer.Speak($completeEntry)
    } elseif ($PSVersionTable["PSEdition"] -eq "Core" -and $PSVersionTable["OS"].StartsWith("Darwin")) {
        say $completeEntry
    }
    return $completeEntry
}