<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Location;
use App\Models\Source;

class FrontIndexController extends Controller
{
    private $limit = 15;

    private function getRecent()
    {
        $locations = Location::where("active", true)->orderByDesc('updated_at')->get();
        $list      = [];
        foreach ($locations as $location) {
            if (($location->block_count + $location->silence_count) >= 2) {
                array_push($list, $location);
            }
        }
        return $list;
    }

    public function start()
    {
        $list        = $this->getRecent();
        $latest_date = $list[0]->updated_at->format('Y M d');
        return view('front.index', [
            'count'       => count($list),
            'sources'     => count(Source::where("active", true)->get()),
            'recent'      => $list,
            'latest_date' => $latest_date,
            'title'       => "The Bad Space"
        ]);
    }

    public function indexSearch(Request $request)
    {
        // this grabs the search results from the db
        $terms     = $request->index_search;
        $rawSearch = $terms;
        $terms     = str_replace(",", "", $terms);
        $terms     = str_replace(" ", "|", $terms);
        $raw       = DB::select("SELECT * FROM searchlocations(?)", [$terms]);
        $results   = [];
        foreach ($raw as $item) {
            if (($item->block_count + $item->silence_count) > 2) {
                array_push($results, $item);
            }
        }

        //this gets recent updates to display under search results
        $list        = $this->getRecent();
        $latest_date = $list[0]->updated_at->format('Y M d');
        return view('front.index', [
            'count'       => count($list),
            'sources'     => count(Source::where("active", true)->get()),
            'recent'      => $list,
            'results'     => $results,
            'recent'      => $list,
            'terms'       => $terms,
            'latest_date' => $latest_date,
            'title'       => "Search Results",
        ]);
    }

    public function about()
    {
        $sources = Source::where("active", true)->get();
        return view('front.about', [
            'title'   => "ABOUT",
            'sources' => $sources
        ]);
    }

    public function appeals()
    {
        return view('front.appeals', [
            'title' => "LOCATION APPEALS",
        ]);
    }

    public function location(string $uuid = "1")
    {
        $location = Location::where("uuid", $uuid)->first();
        $sources  = Source::where("active", true)->get();
        $name     = "NO LOCATION FOUND";
        if ($location) {
            $name = $location->name;
        }
        return view('front.location', [
            'title'         => str_replace(".", " ", $name),
            'location'      => $location,
            'actions'       => $location->block_count + $location->silence_count,
            'sources_count' => count($sources),
            'images'        => json_decode($location->images),
            'updated'       => $location->updated_at->format('Y M d'),
        ]);
    }

    public function listings(int $pageNum = 1)
    {
        $locations = Location::where("active", true)->get();
        $active    = [];
        foreach ($locations as $location) {
            if (($location->block_count + $location->silence_count) >= 2) {
                array_push($active, $location);
            }
        }

        $pages = [];

        if (count($active) != 0) {
            if (count($active) < $this->limit) {
                $this->limit = count($active) - 1;
            }
            $range = $pageNum * $this->limit - $this->limit;

            if ($range != 0) {
                $range = $range + 1;
            }
            for ($i = 0; $i <= $this->limit; ++$i) {
                if (isset($active[$i + $range])) {
                    array_push($pages, $active[$i + $range]);
                } else {
                    // chill out
                }
            }
        }

        $pageCount = ceil(count($active) / $this->limit);

        if ($range != 0) {
            $range = $range + 1;
        }

        $next = $pageNum + 1;
        if ($next > $pageCount) {
            $next = 1;
        }

        $prev = $pageNum - 1;

        if ($prev <= 0) {
            $prev = $pageCount;
        }

        return view('front.listing', [
            'title'      => "Listings",
            'sources'    => count(Source::where("active", true)->get()),
            "totalPages" => $pageCount,
            "prev"       => $prev,
            "next"       => $next,
            'pageNum'    => $pageNum,
            'locations'  => $pages
        ]);
    }
}