<?php

namespace App\Http\Controllers;

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

class StreamController extends Controller
{
    private static $STREAM_URL = 'https://stream.nhgooi.nl:8001/';

    public function liveradio()
    {
        return view('listen', [
            'source' => self::$STREAM_URL . 'mp3live',
            'title' => 'Luister live', 
            'content' => 'de live-uitzending van NH Gooi.',
            'isStream' => true ]);
    }
   
    public function livetv()
    {
        return view('watch', ['stream' => 'https://rrr.sz.xlcdn.com/?account=nhnieuws&file=nhgooi&type=live&service=wowza&protocol=https&output=playlist.m3u8']);
        // return view('watch', ['stream' => 'https://stream.nhgooi.nl:81/tv']);
    }

    public function studio()
    {
        return view('watch', ['stream' => 'https://stream.nhgooi.nl:81/live/studio']);
    }

    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 view('podcastredirect', ['podcast' => $podcast]);
        }

        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 gemeenteraad(Request $request) {
        return view('listen', [
            'source' => self::$STREAM_URL . 'gemhuizen',
            'title' => 'Gemeenteraad Huizen', 
            'content' => 'de openbare vergadering van de gemeenteraad Huizen', 
            'isStream' => true,
            '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 ]);
    }
}
