<?php

namespace App\Http\Controllers;

use \Illuminate\Http\Request;
use \Model\Programma;

class PodcastController extends Controller
{
    public function podcasts(Request $request, $programma)
    {
        $action = 'overzicht';
        $viewData = [];
        if((int)$programma > 0) {
          $action = 'programma/' . (int)$programma;
          $viewData['program'] = new \Model\Program($this->API('programma/details/' . (int)$programma));
        }
        
        return $this->getPodcastList($request, $action, $viewData);
    }

    private function getPodcastList(Request $request, $action, $viewData = [])
    {
        $page = (int)$request->get('pagina', 1);
        $apiResult = $this->API('podcast/' . $action . '?pagina=' . (int)max(1, $page) . '&aantal=100');
        $podcasts = [];
        foreach($apiResult->podcasts as $podcast)
        {
            $podcasts[] = new \Model\Podcast($podcast);
        }

        return view($request->ajax() ? 'partial.podcastitems' : 'podcastseries', array_merge($viewData, ['podcasts' => $podcasts, 'searchURL' => 'gemist/zoeken']));
    }

    public function podcast(Request $request, $id)
    {
	parent::registerView($request, 'podcast', $id);
	$apiResult = $this->API('podcast/details/' . (int)$id);
        $podcast = new \Model\Podcast($apiResult);
        return view('podcastitem', ['podcast' => $podcast, 'metadata' => $podcast->metadata]);
    }
}
