{
  "$schema": "./.schemas/page.schema.json",
  "title": "Jellyfin",
  "icon": "play.rectangle",
  "sources": {
    "now": {
      "url": "http://jellyfin.local:8096",
      "secret": "jellyfin",
      "auth": {
        "type": "header",
        "name": "Authorization",
        "value": "MediaBrowser Token=\"{secret}\""
      },
      "every": "1m",
      "requests": {
        "sessions": "/Sessions?activeWithinSeconds=960"
      },
      "derive": [
        {
          "name": "streams",
          "value": {
            "each": "sessions",
            "where": {
              "all": [
                {
                  "path": "NowPlayingItem",
                  "exists": true
                },
                {
                  "path": "NowPlayingItem.RunTimeTicks",
                  "gt": 0
                }
              ]
            },
            "sort": {
              "by": "LastActivityDate",
              "order": "desc"
            },
            "fields": {
              "id": "Id",
              "title": {
                "if": {
                  "path": "NowPlayingItem.Type",
                  "equals": "Episode"
                },
                "then": "NowPlayingItem.SeriesName",
                "else": {
                  "if": {
                    "path": "NowPlayingItem.Type",
                    "equals": "Audio"
                  },
                  "then": {
                    "template": "{NowPlayingItem.Album}"
                  },
                  "else": "NowPlayingItem.Name"
                }
              },
              "subtitle": {
                "if": {
                  "path": "NowPlayingItem.Type",
                  "equals": "Episode"
                },
                "then": {
                  "if": {
                    "path": "NowPlayingItem.IndexNumber",
                    "exists": true
                  },
                  "then": {
                    "template": "S{NowPlayingItem.ParentIndexNumber|pad:2}E{NowPlayingItem.IndexNumber|pad:2} · {NowPlayingItem.Name}"
                  },
                  "else": {
                    "template": "{NowPlayingItem.Name}"
                  }
                },
                "else": {
                  "if": {
                    "path": "NowPlayingItem.Type",
                    "equals": "Audio"
                  },
                  "then": {
                    "template": "{NowPlayingItem.AlbumArtist} · {NowPlayingItem.Name}"
                  },
                  "else": {
                    "template": "{NowPlayingItem.ProductionYear|raw}"
                  }
                }
              },
              "user": "UserName",
              "who": {
                "template": "{UserName} · {DeviceName}"
              },
              "method": {
                "path": "PlayState.PlayMethod",
                "values": {
                  "DirectPlay": "Direct play",
                  "DirectStream": "Direct stream",
                  "Transcode": "Transcoding"
                },
                "default": "Direct play"
              },
              "state": {
                "if": {
                  "path": "PlayState.IsPaused",
                  "equals": true
                },
                "then": {
                  "template": "Paused"
                },
                "else": {
                  "template": ""
                }
              },
              "pos": {
                "divide": [
                  "PlayState.PositionTicks",
                  600000000
                ],
                "round": 0
              },
              "run": {
                "divide": [
                  "NowPlayingItem.RunTimeTicks",
                  600000000
                ],
                "round": 0
              },
              "image": {
                "if": {
                  "path": "NowPlayingItem.Type",
                  "equals": "Episode"
                },
                "then": {
                  "if": {
                    "path": "NowPlayingItem.SeriesPrimaryImageTag",
                    "exists": true
                  },
                  "then": {
                    "template": "/Items/{NowPlayingItem.SeriesId}/Images/Primary?fillHeight=144&quality=85"
                  },
                  "else": {
                    "template": ""
                  }
                },
                "else": {
                  "if": {
                    "path": "NowPlayingItem.Type",
                    "equals": "Audio"
                  },
                  "then": {
                    "if": {
                      "path": "NowPlayingItem.AlbumPrimaryImageTag",
                      "exists": true
                    },
                    "then": {
                      "template": "/Items/{NowPlayingItem.AlbumId}/Images/Primary?fillHeight=144&quality=85"
                    },
                    "else": {
                      "template": ""
                    }
                  },
                  "else": {
                    "if": {
                      "path": "NowPlayingItem.ImageTags.Primary",
                      "exists": true
                    },
                    "then": {
                      "template": "/Items/{NowPlayingItem.Id}/Images/Primary?fillHeight=144&quality=85"
                    },
                    "else": {
                      "template": ""
                    }
                  }
                }
              },
              "icon": {
                "path": "NowPlayingItem.Type",
                "values": {
                  "Movie": "film",
                  "Episode": "tv",
                  "Series": "tv",
                  "Audio": "music.note",
                  "MusicAlbum": "music.note",
                  "Book": "book",
                  "MusicVideo": "music.note.tv"
                },
                "default": "play.rectangle"
              },
              "shape": {
                "if": {
                  "path": "NowPlayingItem.Type",
                  "equals": "Audio"
                },
                "then": {
                  "template": "square"
                },
                "else": {
                  "template": "poster"
                }
              },
              "can_control": "SupportsRemoteControl"
            }
          }
        },
        {
          "name": "playing",
          "value": {
            "count": "streams"
          }
        }
      ]
    },
    "library": {
      "url": "http://jellyfin.local:8096",
      "secret": "jellyfin",
      "auth": {
        "type": "header",
        "name": "Authorization",
        "value": "MediaBrowser Token=\"{secret}\""
      },
      "every": "15m",
      "requests": {
        "counts": "/Items/Counts",
        "folders": "/Library/VirtualFolders",
        "recent": "/Items?Recursive=true&SortBy=DateCreated&SortOrder=Descending&IncludeItemTypes=Movie,Episode,MusicAlbum&Fields=DateCreated&Limit=8"
      },
      "derive": [
        {
          "name": "primary",
          "value": {
            "each": "counts",
            "entries": true,
            "where": {
              "path": "key",
              "in": [
                "MovieCount",
                "SeriesCount",
                "AlbumCount",
                "BookCount",
                "MusicVideoCount"
              ]
            },
            "fields": {
              "kind": {
                "path": "key",
                "values": {
                  "MovieCount": "movies",
                  "SeriesCount": "tvshows",
                  "AlbumCount": "music",
                  "BookCount": "books",
                  "MusicVideoCount": "musicvideos"
                }
              },
              "n": "value"
            }
          }
        },
        {
          "name": "secondary",
          "value": {
            "each": "counts",
            "entries": true,
            "where": {
              "path": "key",
              "in": [
                "EpisodeCount",
                "SongCount"
              ]
            },
            "fields": {
              "kind": {
                "path": "key",
                "values": {
                  "EpisodeCount": "tvshows",
                  "SongCount": "music"
                }
              },
              "n": "value"
            }
          }
        },
        {
          "name": "kinds",
          "value": {
            "each": "folders",
            "group": "CollectionType",
            "fields": {
              "kind": "key",
              "libs": {
                "count": "rows"
              }
            }
          }
        },
        {
          "name": "libs0",
          "value": {
            "each": "folders",
            "lookup": {
              "from": "primary",
              "by": "kind",
              "on": "CollectionType",
              "as": "p"
            },
            "fields": {
              "id": "ItemId",
              "name": "Name",
              "kind": "CollectionType",
              "n": "p.n"
            }
          }
        },
        {
          "name": "libs",
          "value": {
            "each": "libs0",
            "lookup": {
              "from": "kinds",
              "by": "kind",
              "on": "kind",
              "as": "k"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "kind": "kind",
              "n": "n",
              "shared": {
                "if": {
                  "path": "k.libs",
                  "gt": 1
                },
                "then": {
                  "template": "yes"
                },
                "else": {
                  "template": "no"
                }
              }
            }
          }
        },
        {
          "name": "lib_words",
          "value": {
            "each": "libs",
            "lookup": {
              "from": "secondary",
              "by": "kind",
              "on": "kind",
              "as": "s"
            },
            "fields": {
              "id": "id",
              "name": "name",
              "n": "n",
              "m": "s.n",
              "shared": "shared",
              "kind_word": {
                "path": "kind",
                "values": {
                  "movies": "Movies",
                  "tvshows": "Shows",
                  "music": "Music",
                  "books": "Books",
                  "musicvideos": "Music videos",
                  "homevideos": "Home videos",
                  "boxsets": "Collections",
                  "mixed": "Mixed"
                },
                "default": ""
              },
              "w1": {
                "path": "kind",
                "values": {
                  "movies": "movie",
                  "tvshows": "show",
                  "music": "album",
                  "books": "book",
                  "musicvideos": "music video"
                }
              },
              "w2": {
                "path": "kind",
                "values": {
                  "tvshows": "episode",
                  "music": "song"
                }
              },
              "p1": {
                "if": {
                  "path": "n",
                  "equals": 1
                },
                "then": {
                  "template": ""
                },
                "else": {
                  "template": "s"
                }
              },
              "p2": {
                "if": {
                  "path": "s.n",
                  "equals": 1
                },
                "then": {
                  "template": ""
                },
                "else": {
                  "template": "s"
                }
              }
            }
          }
        },
        {
          "name": "library_rows",
          "value": {
            "each": "lib_words",
            "fields": {
              "id": "id",
              "name": "name",
              "count": {
                "if": {
                  "path": "shared",
                  "equals": "yes"
                },
                "then": {
                  "template": "{kind_word}"
                },
                "else": {
                  "if": {
                    "path": "w2",
                    "exists": true
                  },
                  "then": {
                    "template": "{n} {w1}{p1} · {m} {w2}{p2}"
                  },
                  "else": {
                    "if": {
                      "path": "w1",
                      "exists": true
                    },
                    "then": {
                      "template": "{n} {w1}{p1}"
                    },
                    "else": {
                      "template": "{kind_word}"
                    }
                  }
                }
              }
            }
          }
        }
      ]
    },
    "server": {
      "url": "http://jellyfin.local:8096",
      "secret": "jellyfin",
      "auth": {
        "type": "header",
        "name": "Authorization",
        "value": "MediaBrowser Token=\"{secret}\""
      },
      "every": "5m",
      "requests": {
        "info": "/System/Info",
        "tasks": "/ScheduledTasks?IsHidden=false",
        "plugins": "/Plugins"
      },
      "derive": [
        {
          "name": "server_rows",
          "value": {
            "each": "info",
            "entries": true,
            "where": {
              "all": [
                {
                  "path": "key",
                  "in": [
                    "HasPendingRestart",
                    "IsInMaintenanceMode"
                  ]
                },
                {
                  "path": "value",
                  "equals": true
                }
              ]
            },
            "fields": {
              "name": {
                "path": "key",
                "values": {
                  "HasPendingRestart": "Restart pending",
                  "IsInMaintenanceMode": "Maintenance mode"
                }
              },
              "state": {
                "template": "warning"
              },
              "detail": {
                "path": "key",
                "values": {
                  "HasPendingRestart": "Waiting for a restart",
                  "IsInMaintenanceMode": "On"
                }
              },
              "group": {
                "template": "Application"
              }
            }
          }
        },
        {
          "name": "task_rows",
          "value": {
            "each": "tasks",
            "where": {
              "path": "IsHidden",
              "not": true
            },
            "fields": {
              "name": "Name",
              "group": "Category",
              "state": {
                "if": {
                  "path": "LastExecutionResult.Status",
                  "in": [
                    "Failed",
                    "Aborted"
                  ]
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "template": "ok"
                }
              },
              "detail": {
                "if": {
                  "path": "State",
                  "equals": "Running"
                },
                "then": {
                  "template": "Running · {CurrentProgressPercentage|round}%"
                },
                "else": {
                  "if": {
                    "path": "LastExecutionResult.Status",
                    "exists": true
                  },
                  "then": {
                    "template": "{LastExecutionResult.Status} · {LastExecutionResult.EndTimeUtc|ago}"
                  },
                  "else": {
                    "template": "Not run yet"
                  }
                }
              }
            }
          }
        },
        {
          "name": "plugin_rows",
          "value": {
            "each": "plugins",
            "fields": {
              "name": "Name",
              "group": {
                "template": "Plugins"
              },
              "state": {
                "if": {
                  "path": "Status",
                  "in": [
                    "Malfunctioned",
                    "NotSupported"
                  ]
                },
                "then": {
                  "template": "warning"
                },
                "else": {
                  "template": "ok"
                }
              },
              "detail": {
                "template": "{Version} · {Status}"
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "server",
      "type": "fleet",
      "placement": "page_info",
      "label": "Server",
      "max_items": 30,
      "items": [],
      "source": {
        "ref": "server",
        "map": {
          "items": {
            "each": [
              "server_rows",
              "task_rows",
              "plugin_rows"
            ],
            "fields": {
              "name": "name",
              "state": "state",
              "detail": "detail",
              "group": "group"
            }
          }
        },
        "failed": {
          "state": "critical",
          "detail": "The server isn't answering",
          "after": 3
        }
      }
    },
    {
      "id": "now",
      "type": "pacing",
      "label": "Now playing",
      "unit": "min",
      "items": [],
      "source": {
        "ref": "now",
        "empty": {
          "text": "Nothing playing",
          "when": {
            "path": "playing",
            "equals": 0
          }
        },
        "map": {
          "items": {
            "each": "streams",
            "fields": {
              "key": "id",
              "label": "title",
              "subtitle": "subtitle",
              "state": "state",
              "detail": {
                "template": "{who} · {method}"
              },
              "value": "pos",
              "target": "run",
              "image": "image",
              "image_shape": "shape",
              "icon": "icon"
            }
          }
        }
      },
      "row_actions": [
        {
          "label": "Message",
          "icon": "bubble.left.and.bubble.right",
          "method": "POST",
          "path": "/Sessions/{id}/Message",
          "inputs": [
            {
              "name": "text",
              "label": "Message",
              "kind": "text"
            }
          ],
          "body": {
            "Header": "Message from Legato",
            "Text": "{input.text}",
            "TimeoutMs": 10000
          },
          "when": {
            "path": "can_control",
            "equals": true
          }
        },
        {
          "label": "Stop",
          "icon": "stop.fill",
          "method": "POST",
          "path": "/Sessions/{id}/Playing/Stop",
          "confirm": {
            "message": "Stop {title} for {user}?",
            "button": "Stop"
          },
          "when": {
            "path": "can_control",
            "equals": true
          }
        }
      ]
    },
    {
      "id": "added",
      "type": "list",
      "label": "Recently added",
      "layout": "shelf",
      "image_shape": "poster",
      "max_items": 4,
      "items": [],
      "source": {
        "ref": "library",
        "map": {
          "items": {
            "each": "recent.Items",
            "fields": {
              "key": "Id",
              "text": {
                "if": {
                  "path": "Type",
                  "equals": "Episode"
                },
                "then": "SeriesName",
                "else": {
                  "if": {
                    "path": "Type",
                    "equals": "Audio"
                  },
                  "then": "Name",
                  "else": "Name"
                }
              },
              "detail": {
                "if": {
                  "path": "Type",
                  "equals": "Episode"
                },
                "then": {
                  "if": {
                    "path": "IndexNumber",
                    "exists": true
                  },
                  "then": {
                    "template": "S{ParentIndexNumber|pad:2}E{IndexNumber|pad:2} · {DateCreated|ago}"
                  },
                  "else": {
                    "template": "Episode · {DateCreated|ago}"
                  }
                },
                "else": {
                  "if": {
                    "path": "Type",
                    "equals": "Audio"
                  },
                  "then": {
                    "template": "Song · {DateCreated|ago}"
                  },
                  "else": {
                    "if": {
                      "path": "Type",
                      "equals": "MusicAlbum"
                    },
                    "then": {
                      "template": "Album · {DateCreated|ago}"
                    },
                    "else": {
                      "template": "{Type} · {DateCreated|ago}"
                    }
                  }
                }
              },
              "image": {
                "if": {
                  "path": "Type",
                  "equals": "Episode"
                },
                "then": {
                  "if": {
                    "path": "SeriesPrimaryImageTag",
                    "exists": true
                  },
                  "then": {
                    "template": "/Items/{SeriesId}/Images/Primary?fillHeight=144&quality=85"
                  },
                  "else": {
                    "template": ""
                  }
                },
                "else": {
                  "if": {
                    "path": "Type",
                    "equals": "Audio"
                  },
                  "then": {
                    "if": {
                      "path": "AlbumPrimaryImageTag",
                      "exists": true
                    },
                    "then": {
                      "template": "/Items/{AlbumId}/Images/Primary?fillHeight=144&quality=85"
                    },
                    "else": {
                      "template": ""
                    }
                  },
                  "else": {
                    "if": {
                      "path": "ImageTags.Primary",
                      "exists": true
                    },
                    "then": {
                      "template": "/Items/{Id}/Images/Primary?fillHeight=144&quality=85"
                    },
                    "else": {
                      "template": ""
                    }
                  }
                }
              },
              "icon": {
                "path": "Type",
                "values": {
                  "Movie": "film",
                  "Episode": "tv",
                  "Series": "tv",
                  "Audio": "music.note",
                  "MusicAlbum": "music.note",
                  "Book": "book",
                  "MusicVideo": "music.note.tv"
                },
                "default": "play.rectangle"
              },
              "image_shape": {
                "if": {
                  "path": "Type",
                  "equals": "MusicAlbum"
                },
                "then": {
                  "template": "square"
                },
                "else": {
                  "template": "poster"
                }
              },
              "url": {
                "template": "http://jellyfin.local:8096/web/#/details?id={Id}"
              }
            }
          }
        }
      }
    },
    {
      "id": "libraries",
      "type": "list",
      "label": "Libraries",
      "items": [],
      "source": {
        "ref": "library",
        "map": {
          "items": {
            "each": "library_rows",
            "fields": {
              "text": "name",
              "detail": "count",
              "url": {
                "template": "legato://page/jellyfin-library?id={id}"
              }
            }
          }
        }
      }
    }
  ]
}
