<?php

namespace App\Controller;

use \Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
 *
 */
class ReferenceController extends AbstractController
{
    /**
     * Method to retrieve a Bible reference
     *
     * @param string $ref
     *
     * @return string
     */
    public static function retrieveBible($ref): string
    {
        if(count(explode(' ', $ref)) > 2) {
            list($index, $book, $chapter, $passage) = preg_split("/:| /", $ref);
            $book = "{$index}{$book}";
        } else {
            list($book, $chapter, $passage) = preg_split("/:| /", $ref);
        }

        $file = glob(dirname(dirname(__DIR__))."/references/Bible/* - {$book}/{$book}{$chapter}.md");
        $res = implode("\n", file(current($file)));

        return $res;
    }

    /**
     * Method to retrieve a creed
     *
     * @param string
     *
     * @return string
     */
    public static function retrieveCreed($ref)
    {
        $file = match($ref) {
            'apc' => "Apostle's Creed.md",
            'atc' => "Athanasian Creed.md",
            'dc' => 'Definition of Chalcedon.md',
            'fc' => 'French Confession.md',
            'nc' => 'Nicene Creed.md'
        };

        return implode("\n", file(dirname(dirname(__DIR__))."/references/Creeds/{$file}"));
    }

    /**
     * Method to retrieve a Heidelberg Catechism reference
     *
     * @param string $ref
     *
     * @return string
     */
    public static function retrieveHC($ref)
    {
        $ref = strtoupper($ref);
        return implode("\n", file(dirname(dirname(__DIR__))."/references/Heidelberg/{$ref}.md"));
    }

    /**
     * Method to retrieve a Belgian Catechism reference
     *
     * @param string $ref
     *
     * @return string
     */
    public static function retrieveBC($ref)
    {
        $ref = ucfirst($ref);
        return implode("\n", file(dirname(dirname(__DIR__))."/references/Belgic/{$ref}.md"));
    }

    public static function retrieveCD($ref)
    {
        $ref = strtoupper($ref);
        return implode("\n", file(dirname(dirname(__DIR__))."/references/Dort/{$ref}.md"));
    }

    public static function retrieveWSC($ref)
    {
        $art = str_pad($ref, 3, '0', STR_PAD_LEFT);
        $files = glob(dirname(dirname(__DIR__))."/references/Westminster/Shorter Catechism/WSC{$art}.md");
        return implode("\n", file(current($files)));
    }

    public static function retrieveWLC($ref)
    {
        $art = str_pad($ref, 3, '0', STR_PAD_LEFT);
        $files = glob(dirname(dirname(__DIR__))."/references/Westminster/Larger Catechism/WLC{$art}.md");
        return implode("\n", file(current($files)));
    }

    public static function retrieveWCF($ref)
    {
        $files = glob(dirname(dirname(__DIR__))."/references/Westminster/Confessions/Chapter {$ref}.md");
        return implode("\n", file(current($files)));
    }
}
