<?php

namespace App\Http\Controllers;

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

class RadioController extends Controller
{
    public function schedule(Request $request, $shiftWeeks = 0) 
    {
        $apiResult = $this->API('programma/schema/week/' . (int)$shiftWeeks);
        $start = self::JsonToDateTime($apiResult->startdate);
        $end = self::JsonToDateTime($apiResult->enddate);
        $schedule = [];
        foreach($apiResult->schedule as $program)
        {
            $schedule[] = [
                'starttime' => self::JsonToDateTime($program->start),
                'endtime' => self::JsonToDateTime($program->end),
				'shift' => (int)$shiftWeeks,
                'program' => new \Model\Program($program->program)
            ];
        }

        return view($request->ajax() ? 'radioscheduleweek' : 'radioschedule', ['start' => $start, 'end' => $end, 'schedule' => $schedule, 'shift' => $shiftWeeks]);
    }
 
    public function onair()
    {
		$data = $this->API('programma/schema/onair');
		return response()->json($data);
    }

    public function program($id) 
    {
        $apiResult = $this->API('programma/details/' . (int)$id);
        return view('radioprogram', ['program' => new \Model\Program($apiResult)]);
    }

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

        $related = [];
        if($podcast->program != null) 
        {
            $apiRelated = $this->API("podcast/programma/{$podcast->program->id}?date={$podcast->created->format('Y-m-d')}");
            foreach($apiRelated->podcasts as $relatedItem)
            {
                $related[] = new \Model\Podcast($relatedItem);
            }
        }

        return view('podcastitem', ['podcast' => $podcast, 'metadata' => $podcast->metadata, 'related' => $related, 'searchURL' => 'gemist/zoeken']);
    }

    public function podcasts(Request $request, $programma = null)
    {
        $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);
    }

    public function searchpodcast(Request $request, $query)
    {
        return $this->getPodcastList($request, 'zoeken/' . $query)->with('query', urldecode($query));
    } 

    public function terugluisteren(Request $request)
	{
		$programs = [];
		$now = new \DateTimeImmutable('2 minutes ago');
		$apiResult = $this->API('programma/schema/recent');
		foreach($apiResult->schedule as $item) {
			if(!$item->program->nonstop && !$item->program->rerun) {
        			$item->start = self::JsonToDateTime($item->start);
			        $item->end = self::JsonToDateTime($item->end);
				$item->current = $item->end >= $now;
				$item->program = new \Model\Program($item->program);
				$programs[] = $item;
			}
		}

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

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

}
