<?php

namespace App\Http\Controllers;

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

class NewsController extends Controller
{ 
    private static function TimestampToDateTime($timestamp) {
      $result = new \DateTime;
      $result->setTimestamp($timestamp);
      return $result;
    }
 
    public function show(Request $request, $id)
    {
        parent::registerView($request, 'nieuws', $id);
        $apiResult = $this->API('nieuws/bericht/' . $id);
	$newsItem = new \Model\NewsItem($apiResult->news);

	switch($apiResult->version) {
		case 1:
			return view('newsitem', ['news' => $newsItem, 'metadata' => $newsItem->metadata]);
			break;

		case 2:
			$source = $apiResult->source;
			$newsItem->published = self::TimestampToDateTime($source->created);
			$newsItem->edited = self::TimestampToDateTime($source->updated);
			$newsItem->author = $source->author;
			$newsItem->images = null; // Images will be embedded
			$newsItem->video = null; // Videos will be embedded
			$newsItem->content = $source->blocks;
			return view('newsitem', ['news' => $newsItem, 'metadata' => $newsItem->metadata]);
        }
    }

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

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

    public function themelist(Request $request, $theme)
    {
        return $this->listNews($request, 'thema/' . $theme, ucfirst($theme));
    }
 
    public function search(Request $request, $query) 
    {
        return $this->listNews($request, 'zoeken/' . $query, 'Zoekresultaat')->with('query', urldecode($query));
    }
    

    public function activeblog()
    {
        $apiResult = $this->API('blog/overzicht');
        if(count($apiResult)) {
            $blog = new \Model\Blog($apiResult[0]);
            if($blog->is_active) {
              return redirect($blog->url);
            }
        }
 
        return abort(404);
    } 

    public function blog(Request $request, $id)
    {
        parent::registerView($request, 'blog', $id);

        $page = (int)$request->get('pagina', 1);
        $hasNext = true;
        while($page > 0) {
          $apiResult = $this->API('blog/overzicht/' . (int)$id . '?pagina=' . (int)max(1, $page));
  
          $blog = new \Model\Blog($apiResult->blog);
          $items = [];
          foreach($apiResult->items as $blogItem)
          {
              $items[] = new \Model\NewsItem($blogItem);
          }
  
          if(count($items) || ($page == 1)) 
          {
            return view('blog', ['blog' => $blog, 'pagina' => $page, 'items' => $items, 'hasNext' => $hasNext && count($items) == 15]);
          }

          $hasNext = false;
          --$page;
        }

        return abort(404);
    }
   
    private function listNews(Request $request, $url, $title = null)
    {
        $page = (int)$request->get('pagina', 1);
        $apiResult = $this->API('nieuws/' . $url . '?pagina=' . (int)max(1, $page));
        $news = [];
        foreach($apiResult->news as $newsItem)
        {
            $news[] = new \Model\NewsItem($newsItem);
        }

        return view($request->ajax() ? 'partial/newslist_small' : ($title == null ? 'home' : 'newslist'), ['title' => $title, 'news' => $news, 'searchURL' => 'nieuws/zoeken']);
    }

    public function popular()
    {
        $apiResult = $this->API('nieuws/populair');
        $news = [];
        foreach($apiResult as $newsItem)
        {
            $news[] = new \Model\NewsItem($newsItem);
        }

        return view('popularnews', ['news' => $news]);
    }

    public function regionieuws() 
    {
        return view('listen', [
            'source' => $this->API_URL . 'nieuws/regionieuws',
            'title' => 'Regionieuws', 
            'content' => 'het laatste nieuws uit de regio',
            'isStream' => false,
            'canDownload' => true ]);
    }
}
