#requires -Modules @{ModuleName='PwshSpectreConsole'; ModuleVersion='2.1.2'}, @{ModuleName='PSBlueSky'; ModuleVersion='0.6.0'}

# REQUIRES WINDOWS TERMINAL PREVIEW 1.22+ FOR SIXEL https://devblogs.microsoft.com/commandline/windows-terminal-preview-1-22-release/
# Or another sixel capable terminal emulator but I've only tested on Windows Terminal Preview so far

# REQUIRES BLUE SKY CREDS TO BE SETUP
$timeline = Get-BskyTimeline -Limit 15

$formattedPosts = @()
foreach ($post in $timeline) {
  $author = Get-BskyProfile $post.Author

  # Format some chunks of text
  $avatarWidth = 15
  $authorImage = Get-SpectreImage $author.Avatar -MaxWidth $avatarWidth
  $likes = ":red_heart:  [red]$($post.Liked)[/]"
  $authorDisplayName = "[white]$($post.AuthorDisplay.Trim())[/]"
  $authorLink = "[grey69 link=$($post.URL)]@$($post.Author)[/]"
  $postDate = "[grey42]$($post.Date)[/]"

  # Format the post, images require PSBlueSky to add another property for image links
  $formattedPost = @($post.Text)
  if ($post.Image) {
    $formattedPost += Write-SpectreHost "" -PassThru
    $formattedPost += @(
      Get-SpectreImage $post.Image -MaxWidth 50
    )
  }

  $panelHeight = [Math]::Max(6, ($formattedPost | Format-SpectreRows | Format-SpectrePanel | Get-SpectreRenderableSize).Height)

  $formattedPosts += [ordered]@{
    Image = ($authorImage | Format-SpectrePanel -Border None | Format-SpectrePadded -Top 1 -Bottom 1 -Left 0 -Right 0)
    Post  = $formattedPost | Format-SpectreRows | Format-SpectrePanel -Title "[white] $likes $authorDisplayName $authorLink $postDate [/]" -Color DeepSkyBlue3_1 -Height $panelHeight -Expand
  }
  $formattedPosts += [ordered]@{ Image = ""; Post = "" } # spacing hack
}

$formattedPosts | Format-SpectreTable -HideHeaders -Border None