<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \Model\NewsItem;

class ImagesController extends Controller
{ 
    private static function TimestampToDateTime($timestamp) {
      $result = new \DateTime;
      $result->setTimestamp($timestamp);
      return $result;
    }
 
    public function show(Request $request, $id)
    {
        $apiResult = $this->API('beelden/details/' . $id);
	$imagesItem = new \Model\NewsItem($apiResult->images);

	return view('imagesitem', ['images' => $imagesItem, 'metadata' => $imagesItem->metadata]);
    }

    public function overview(Request $request)
    {
        return $this->listImages($request, 'overzicht');
    }

    public function regionlist(Request $request, $region)
    {
        return $this->listImages($request, 'regio/' . $region, ucfirst($region));
    }

    public function themelist(Request $request, $theme)
    {
        return $this->listImages($request, 'thema/' . $theme, ucfirst($theme));
    }
 
    private function listImages(Request $request, $url, $title = null)
    {
        $page = (int)$request->get('pagina', 1);
        $apiResult = $this->API('beelden/' . $url . '?pagina=' . (int)max(1, $page));
        $images = [];
        foreach($apiResult->items as $imagesItem)
        {
            $images[] = new \Model\NewsItem($imagesItem);
        }

        return view($request->ajax() ? 'partial/imageslist_small' : 'imageslist', ['title' => $title, 'images' => $images]);
    }
}
