<?php
/* Copyright (C) 2021  Alexander Rosenberg
 * This file is AGPL v3. See LICENSE file for more information
 */
require 'options.php';
require 'library.php';
require 'Album.php';

header("Content-Type: text/html");

$method = $_SERVER['REQUEST_METHOD'];

function get_albums() {
    global $LIBRARY_INFO;
    $albums = [];
    $start = $_GET['start'];
    $end = $_GET['end'];
    if (!ctype_digit($start) || !ctype_digit($end) || $start > $end) {
        echo 'Invalid range!';
        http_response_code(400);
        exit(0);
    }
    $len = count($LIBRARY_INFO['albums']);
    if ($start >= $len) {
        return array();
    }
    if ($end >= $len) {
        $end  = $len - 1;
    }
    $albums = [];
    for ($i = $start; $i <= $end; ++$i) {
        $albums[] = new Album($LIBRARY_INFO['albums'][$i]);
    }
    return $albums;
}

switch ($method) {
    case 'HEAD':
    case 'GET':
        $albums = get_albums();
        foreach ($albums as $album) {
            echo $album->get_html();
        }
        break;
    default:
        echo 'Method not permitted';
        http_response_code(405);
        exit(0);
}

?>