<?php

include('config.php');
include("db_mediaq.php");
include("Util.php");

if (!DEBUGGING_MODE){
    header('Content-Type: application/json');
}
if (isset ($_POST['username'])) {
    $username = $_POST ['username'];
    performQuery2($username);
}

/**
 * @param $username
 */
function performQuery2($username)
{
    // Zeitmessung
    $date = new DateTime();
    $startTime = $date->getTimestamp();
    $duration  = round(microtime(true) * 1000);

    // get all video-segments which are not part of user's videos
    $allVideoSegmentsExceptMine = getAllVideosExceptOfUser($username);
    $allVideoSegmentsExceptMineSplittedForEachVideo = splitVideoSegmentsDueToVideo($allVideoSegmentsExceptMine);

    $allmyvideos = getMyVideoIds($username);
    $result_arr = array();

    foreach ($allmyvideos as $myvideoid) {
        // get user's actual video-segments
        $myvideo_meta_entries = getVideoMetaEntries($myvideoid);

        foreach ($myvideo_meta_entries as $myvideo_single_entry) {

            if (DEBUGGING_MODE){
                echo "==========================================".$myvideo_single_entry[VIDEOID]." ".$myvideo_single_entry[FOVNUM]."==========================================</br>";
                foreach ($allVideoSegmentsExceptMineSplittedForEachVideo as $candidate){
                echo "candidate: videoid=".$candidate[0][VIDEOID]. " FOVNUM:".$candidate[0][FOVNUM]." THETAX:".$candidate[0][THETAX]." PLNG:".$candidate[0][PLNG]." PLAT:".$candidate[0][PLAT]." TIMESTAMP: ".$candidate[0][TIMESTAMP]."</br>";
                }
                echo "====================================================================================</br>";
            }
            foreach ($allVideoSegmentsExceptMineSplittedForEachVideo as $allVideoSegmentsOfCandidateVideo) {

                for ($i = 0; $i < sizeof($allVideoSegmentsOfCandidateVideo); $i++) {

                    $candidate_lower = $allVideoSegmentsOfCandidateVideo[$i];
                    if ($i + 1 < sizeof($allVideoSegmentsOfCandidateVideo)) {
                        $candidate_upper = $allVideoSegmentsOfCandidateVideo[$i + 1];

                        if ($candidate_lower[TIMESTAMP] <= $myvideo_single_entry[TIMESTAMP] && $candidate_upper[TIMESTAMP] >= $myvideo_single_entry[TIMESTAMP]) {
                            if (DEBUGGING_MODE){
                                echo "-------------------------------------------------------------------------------------------------------------------</br>";
                                echo "PointOfView_lower: videoid=".$candidate_lower[VIDEOID]. " FOVNUM:".$candidate_lower[FOVNUM]." THETAX:".$candidate_lower[THETAX]." PLNG:".$candidate_lower[PLNG]." PLAT:".$candidate_lower[PLAT]." TIMESTAMP: ".$candidate_lower[TIMESTAMP]."</br>";
                                echo "PointOfView_upper: videoid=".$candidate_upper[VIDEOID]. " FOVNUM:".$candidate_upper[FOVNUM]." THETAX:".$candidate_upper[THETAX]." PLNG:".$candidate_upper[PLNG]." PLAT:".$candidate_upper[PLAT]." TIMESTAMP: ".$candidate_upper[TIMESTAMP]."</br>";
                                echo "PointToBeViewed: videoid=".$myvideo_single_entry[VIDEOID]. " FOVNUM:".$myvideo_single_entry[FOVNUM]." THETAX:".$myvideo_single_entry[THETAX]." PLNG:".$myvideo_single_entry[PLNG]." PLAT:".$myvideo_single_entry[PLAT]." TIMESTAMP: ".$myvideo_single_entry[TIMESTAMP]."</br>";
                            }

                            // point "my_video_segment" is between candidate_lower and upper (time)
                            performPointIntersectsViewingAreaCheck(InterpolatePointsAtTimestamp($candidate_lower, $candidate_upper, $myvideo_single_entry[TIMESTAMP]), $myvideo_single_entry, $result_arr);

                            // plane sweep: skip to next my_video_segment
                            break;
                        } //  else continue
                    }
                }
            }
        }
    }

    $indexedOnly = array();

    $i = 0;
    foreach ($result_arr as $value) {
        $startTimeVideo = getVideoStartTime($value["value"]);
        if (DEBUGGING_MODE) {
            echo "videoStartTime of video ".$value["value"].": ". $startTimeVideo."</br>";
        }
        $value["mintimespan"] = $value["mintimespan"] - $startTimeVideo;
        $value["maxtimespan"] = $value["maxtimespan"] - $startTimeVideo;
        $indexedOnly[$i] = $value;
        $i++;
    }

    closeSqlConnection();

    $duration  =  round(microtime(true) * 1000) - $duration;

    $date = new DateTime();
    $endTime = $date->getTimestamp();
    $return_arr = ['starttime' => $startTime, 'endtime' => $endTime, 'result' => $indexedOnly, 'duration' => $duration];


    // echo json_encode(array('result' => $result_arr));
    if (DEBUGGING_MODE){
        return $return_arr;
    }else {
        echo json_encode($return_arr);
    }
}