<?php

namespace App\Http\Controllers;

use \Illuminate\Http\Request;
use \Model\Podcast;

class StreamController extends Controller
{
    public function liveradio()
    {
        return view('listen', [
            'source' => 'http://stream.nhgooi.nl:8000/mp3live', 
            'title' => 'Luister live', 
            'content' => 'de live-uitzending van NH Gooi.',
            'isStream' => true ]);
    }
   
    public function livetv()
    {
        return view('watch');
    }

    public function podcast(Request $request, $id)
    {
        $apiResult = $this->API('podcast/details/' . (int)$id);
        $podcast = new \Model\Podcast($apiResult);
        if($podcast->auth != $request->get('auth')) {
          return abort(403);
        }

        return view('listen', [
            'source' => $this->API_URL . 'podcast/download/' . $apiResult->url . '?auth=' . $podcast->auth, 
            'title' => $podcast->title, 
            'content' => $podcast->title,
            'isStream' => false, 
            'canDownload' => true ]);
    }

    public function program(Request $request, $year, $month, $day, $hour, $duration, $offset = 0) {
        $date = (new \DateTimeImmutable())->setDate($year, $month, $day)->setTime($hour, 0, 0);
        $current = $date->add(\DateInterval::createFromDateString($offset . ' hours'));

        $hours = [];
        for($i = 0; $i < $duration; $i++) {
          $other = $date->add(\DateInterval::createFromDateString($i . ' hours'));
          $url = '/luister/programma/' . $date->format('Y/m/d/H') . '/' . $duration . '/' . $i;
          $hours[$offset == $i ? '#' : $url] = 'Uur ' . ($i + 1) . ' (' . $other->format('H') . ':00)';
        }
 
        return view('listen', [
            'source' => $this->API_URL . 'programma/download/' . $current->format('Y/m/d/H') . '/1',
            'tabs' => $hours,
            'title' => 'Uitzending terugluisteren',
            'content' => 'de uitzending van ' . $current->format('d-m-Y, H') . ':00 uur',
            'isStream' => false,
            'canDownload' => false ]);
    }

    public function kerkdienst(Request $request) {
        return view('listen', [
            'source' => $this->API_URL . 'kerkdienst/download',
            'title' => 'Kerkdienst gemist', 
            'content' => 'de kerkdienst van afgelopen zondag',
            'isStream' => false,
            'canDownload' => true ]);
    }
}
