<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\File;
use Illuminate\View\View;

class RoscoLekoController extends Controller {
    public function getImages(): array {
        $images = [];
        $path = 'images/pandamonium';
        if (isLegacy()) {
            $path = 'images/pandamonium-legacy';
        }
        foreach (File::glob(public_path($path).'/*') as $path) {
            $image_data = [];
            try {
                $exif = exif_read_data($path);
            } catch (Exception $ex) {

            }
            $image_data["path"] = str_replace(public_path(), '', $path);
            if (isset($exif)) {
                if (isset($exif["ImageDescription"])) {
                    $image_data["description"] = $exif["ImageDescription"];
                }
                if (isset($exif["DateTime"])) {
                    $image_data["date"] = strtotime($exif["DateTime"]);
                }
            }
            array_push($images, $image_data);
        }

        usort($images, function ($a, $b) {
            $dateA = $a['date'] ?? PHP_INT_MIN;
            $dateB = $b['date'] ?? PHP_INT_MIN;
            return $dateB <=> $dateA;
        });

        return $images;
    }

    /**
     * Shows the page
     * @return View
     */
    public function show(): View {
        return view('pandamonium', [
            'images' => $this->getImages(),
        ]);
    }
}
