<?php
namespace Grav\Plugin\Shortcodes;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class SpotiplayShortcode extends Shortcode {
    public function init() {
        $this->shortcode->getHandlers()->add('spotiplay', function(ShortcodeInterface $sc) {
            $dataFile = GRAV_ROOT . '/user/data/spotiplay.json';
            $data = [];

            if (file_exists($dataFile)) {
                $data = json_decode(file_get_contents($dataFile), true) ?? [];
            }

            // Render a custom plugin Twig template, passing the JSON data to it
            return $this->twig->processTemplate('partials/spotiplay.html.twig', [
                'spotiplay_data' => $data
            ]);
        });
    }
}
