<?php

namespace brain\controllers;

use Kiwilan\Audio\Audio;

class TapesAPI
{
    private $tapesDir = "assets/audio/tapes";
    protected $params;
    public function __construct()
    {
    }

    public function handleRequest($params)
    {
        if (isset($params[2])) {
            switch ($params[2]) {
                case "list":
                    return $this->getList();
                    break;
                case "tracklist":
                    return $this->getTrackList($params[3]);
                    break;
                default:
                    return json_encode([
                      "message" => "This method does not exist",
                      "type" => "ERROR",
                    ]);
                    break;
            }
        } else {
            return json_encode([
              "message" => "This method does not exist",
              "type" => "ERROR",
            ]);
        }
    }

    public function getTrackList($id)
    {
        $folder = $this->tapesDir . "/" . urldecode($id);
        $contents = scandir($folder);
        $tracklist = [];
        foreach ($contents as $key => $value) {
            $path = realpath($folder . DIRECTORY_SEPARATOR . $value);
            if (!is_dir($path) && $value != ".DS_Store") {
                $item = explode("/", $path);
                if ($item[9] != "cover.jpg") {
                    $tags = Audio::read($path);
                    //reading cover data is lil funky, so off for now
                    //$cover = $tags->getCover();
                    //$image = $cover->getContents();
                    $album = "";
                    if (empty($tags->getAlbum())) {
                        $album = "None";
                    } else {
                        $album = $tags->getAlbum();
                    }
                    array_push($tracklist, [
                      "title" => $tags->getTitle(),
                      "album" => $album,
                      "artist" => $tags->getArtist(),
                      "time" => $tags->getDuration(),
                      "file" => $item[9],
                      "tape" => $item[8],
                    ]);
                }
            }
        }
        usort($tracklist, function ($a, $b) {
            return strnatcasecmp($a["file"], $b["file"]);
        });
        return json_encode($tracklist);
    }

    public function getList($obj = false)
    {
        $current = scandir($this->tapesDir);
        $tapeList = [];
        foreach ($current as $key => $value) {
            $path = realpath($this->tapesDir . DIRECTORY_SEPARATOR . $value);
            if (!is_dir($path) && $value != ".DS_Store") {
                $results[] = $path;
            } elseif ($value != "." && $value != ".." && $value != ".DS_Store") {
                $titles = explode("/", $path);
                array_push($tapeList, ["path" => $path, "title" => $titles[8]]);
            }
        }
        if (!$obj) {
            return json_encode($tapeList);
        } else {
            return $tapeList;
        }
    }
}