<?php

// src/Controller/DataImport.php
// Grab data from transfer app

namespace App\Controller\Routes\Front;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Service\Auth;
use App\Service\Render;
use App\Service\HandleLocations;
use App\Service\HandleExports;

class Index extends AbstractController
{
    /**
     * @Route("/", name="index")
     */
    public function showIndex(Request $request, Auth $auth, Render $render, HandleLocations $locations): Response
    {
        $list = $locations->getActiveLocations();
        if ($request->getMethod() == "GET") {
            return $render->page(["count" => count($list)], "This is The Bad Space", "front/index.twig");
        } else {
            $results = $locations->searchLocations($request->request->get("index_search"));
            return $render->page(
                ["count"    => count($list),
                    "items" => $results['items'],
                    "terms" => $results["terms"]
                ],
                "This is The Bad Space",
                "front/index.twig"
            );
        };
    }

    /**
     * @Route("/location/{uuid}", name="front-location")
     */
    public function showLocations(
        Request $request,
        Render $render,
        HandleLocations $locations,
        string $uuid = "none"
    ): Response {
        $location = $locations->getLocationbyUUID($uuid);
        return $render->page(
            ["location" => $location[0]],
            "The Bad Space | " . $location[0]->getName(),
            "front/location.twig"
        );
    }

    /**
     * @Route("/about", name="about")
     */
    public function showAbout(
        Request $request,
        Auth $auth,
        Render $render,
        HandleLocations $locations
    ): Response {
        return $render->page([], "About The Bad Space", "front/about.twig");
    }

    /**
     * @Route("/listings/page/{pageNum}", name="listings")
     */
    public function showListing(
        Request $request,
        Auth $auth,
        Render $render,
        HandleLocations $locations,
        int $pageNum = 1
    ): Response {
        $list = $locations->getLocationsPage($pageNum, "true");

        $next = $pageNum + 1;
        if ($next > $list["total"]) {
            $next = 1;
        }

        $prev = $pageNum - 1;

        if ($prev <= 0) {
            $prev = $list["total"];
        }

        return $render->page([
            "list"        => $list,
            "currentPage" => $pageNum,
            "nextPage"    => $next,
            "prevPage"    => $prev
        ], "About The Bad Space", "front/listing.twig");
    }

    /**
     * @Route("/exports/{type}", name="exports")
     */
    public function showExports(
        Request $request,
        Auth $auth,
        Render $render,
        HandleLocations $locations,
        HandleExports $exports,
        string $type = "none"
    ): Response {
        switch ($type) {
            case 'none':
            case '':
                return $render->page([], "File Exports", "front/exports.twig");
                break;
            case 'mastodon':
                return $exports->buildMastodonCSV();

                break;
        }
    }
}
