<?php

use App\System\Library\Complements\Util;
use App\System\Library\Security\Hash;
use App\System\Library\Detection\MobileDetect;
use App\System\Library\Media\Video;
$detect = new MobileDetect();
$isMobile = $detect->isMobile() || $detect->isTablet();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>{{$production->title}} | Bandicot.com</title>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
        <meta name="robots" content="noindex">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
        {{-- Bootstrap --}}
        {{ HTML::style('assets/plugins/bootstrap/css/bootstrap.css', array('media' => 'screen')) }}
        {{ HTML::style('assets/plugins/bootstrap/css/bootstrap-theme.css', array('media' => 'screen')) }}

        {{--CSS PERSONALIZADO--}}
        {{ HTML::style('assets/css/ui/videoplayer.css', array('media' => 'screen')) }}
        {{ HTML::style('assets/css/ui/videoplayer-mobile.css', array('media' => 'screen')) }}

        {{-- jQuery (necessary for Bootstraps JavaScript plugins) --}}
        {{ HTML::script('assets/js/jquery.js') }}
        {{ HTML::script('assets/js/jquery-2.1.4.min.js') }}

        <link rel="shortcut icon" href="{{URL::to("/assets/images/favicon.png")}}">
 /** Recibe una peticion con token de un unico uso que retornara la url de la fuente del video
  * 
  * @param type $token Token de la reproduccion generada anteriormente
  * @param type $id_video El id del video
  * @return type
  */
 function getVideoSource($token, $id_video, $time)
 {
     //Tiempo de paja
     // sleep(2);
     $playback = Auth::user()->playbacks()->where(User::ATTR_PLAYBACKS_PIVOT_TOKEN, $token)->get();
     //Verifica que el token de peticion exista
     if (count($playback) == 0) {
         return abort(404);
     }
     $playback = $playback[0];
     //Verifica que el token no haya sido validado
     if (intval($playback->pivot->validate) >= 2) {
         return abort(404);
     }
     //Valida el token de reproduccion
     if ($time > 0) {
         Auth::user()->playbacks()->where(User::ATTR_PLAYBACKS_PIVOT_TOKEN, $token)->update(array(User::ATTR_PLAYBACKS_PIVOT_VALIDATE => intval($playback->pivot->validate) + 1));
     } else {
         Auth::user()->playbacks()->where(User::ATTR_PLAYBACKS_PIVOT_TOKEN, $token)->update(array(User::ATTR_PLAYBACKS_PIVOT_VALIDATE => 2));
     }
     $detect = new MobileDetect();
     $video = new Video($id_video);
     if ($detect->isMobile() || $detect->isTablet()) {
         $url_video = $video->getUrlVideoPlayer();
     } else {
         //Entrega la URL del video
         $url_video = $video->getUrlVideo();
     }
     header("HTTP/1.1 301 Moved Permanently");
     header("Location: {$url_video}");
     header("Connection: close");
 }