<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use stdClass;

use Illuminate\Http\Response;


class VideoController extends Controller
{
    function get_error($ErrorExaction){
        $myObj = new stdClass();
        $myObj->error = true;
        $myObj->msg = $ErrorExaction;
//        $myObj->madeBy = "A.El-zahaby";
//        $myObj->instagram = "egy.js";
        $myJSON = json_encode($myObj,JSON_PRETTY_PRINT);
        echo $myJSON;
        exit;
    }

    function get_log($dump){
        if (isset($_GET['log'])) var_dump($dump).'\n\n\n';
    }


    function getVideoStream(Request $request){

        header('Access-Control-Allow-Origin: *');

        if(isset($request->v) && $request->v != ""){
//            parse_str( parse_url( $request->v, PHP_URL_QUERY ), $vars );
//            $id=$vars['v'];
            $id=$request->v;
            $dt=file_get_contents("https://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en");
//
//            dd($id);
            if (strpos($dt, 'status=fail') !== false) {
                $x=explode("&",$dt);
                $t=array(); $g=array(); $h=array();
                foreach($x as $r){
                    $c=explode("=",$r);
                    $n=$c[0]; $v=$c[1];
                    $y=urldecode($v);
                    $t[$n]=$v;
                }
                $x=explode("&",$dt);
                foreach($x as $r){
                    $c=explode("=",$r);
                    $n=$c[0]; $v=$c[1];
                    $h[$n]=urldecode($v);
                }
                $g[]=$h;
                $g[0]['error'] = true;
//                $g[0]['instagram'] = "egy.js";
//                $g[0]['apiMadeBy'] = 'El-zahaby';
                echo json_encode($g,JSON_PRETTY_PRINT);
            }else{
                $x=explode("&",$dt);
                $t=array(); $g=array(); $h=array();
                foreach($x as $r){
                    $c=explode("=",$r);
                    $n=$c[0]; $v=$c[1];
                    $y=urldecode($v);
                    $t[$n]=$v;
                }


                $streams_old =json_decode(urldecode($t['player_response']));
//                $streams = explode(',',urldecode($t['url_encoded_fmt_stream_map']));
                $streams=$streams_old->streamingData->formats;

//        if(empty($streams[0])){ get_error('ops! this video has something wrong! :( '); }
                if(empty($streams[0])){
                    $this->get_log($streams);
                }
                foreach($streams as $dt){
//                    $x=explode("&",$dt);
//                    foreach($x as $r){
//                        $c=explode("=",$r);
//                        if ($c[0]  == 'itag'){ // reference:  https://superuser.com/q/1386658

//                            switch ($c[1]){
                    switch ($dt->itag){

                        case '18':
                                    $h['mimeType'] = "mp4";
                                    $h['width'] = "640";
                                    $h['height'] = "360";
                                    $h['qualityLabel'] = '360p';
                                    break;
                                case '22':
                                    $h['mimeType'] = "mp4";
                                    $h['width'] = "1280";
                                    $h['height'] = "720";
                                    $h['qualityLabel'] = '720p';
                                    break;
                                case '43':
                                    $h['mimeType'] = "webm";
                                    $h['width'] = "640";
                                    $h['height'] = "360";
                                    $h['qualityLabel'] = '360p';
                                    break;
                                default:

                                    $h['mimeType'] = null;
                                    $h['width'] = null;
                                    $h['height'] = null;
                                    $h['qualityLabel'] = '';
                            }
//                        }
//                        $n=$c[0]; /* => */ $v=$c[1];
//                        $h[$n]=urldecode($v);
//                    }
                    $g[]=$dt;
//                    $g[]=$h;
                }

                //header('Access-Control-Allow-Origin: *');
                //header('Content-Type: application/json');
                $statusCode = 200;

                return \Illuminate\Support\Facades\Response::json($g, $statusCode);
             //   return Response::json( $g);
                //echo json_encode($g,JSON_PRETTY_PRINT);

            }
        }else{
            $this->get_error("Ops, there is no youtube link!");
        }

    }

    public function getYoutubeDirectLinkMp4(Request $request){

        parse_str(file_get_contents("https://www.youtube.com/get_video_info?video_id=$request->id"),$info);
        $streams = $info['adaptive_fmts']; // $info['url_encoded_fmt_stream_map'];
        $streams = explode(',',$streams);

        $listQuality = [];

        $st = [];
        foreach($streams as $stream){
            array_push($st,$stream);
            print_r($stream);

        }
        $statusCode = 200;

     //   return \Illuminate\Support\Facades\Response::json($st, $statusCode);


    }


}
