<?php
/**
 * template: ProQyz Full Mock Test - Template
 */
$proqyz_course_id   = get_query_var('course_id');
$proqyz_lesson_id   = get_query_var('lesson_id');
$quiz_id            = get_query_var('quiz_id');
$quiz_category      = get_query_var('quiz_category');

// main oauth class
if( !class_exists('ST_ProQyz_Oauth') ) { 
    $proqyz_response = 'ProQyz OAuth class missing';
    include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
    exit; 
}

if(!isset($quiz_id) || empty($quiz_id)) {
    $proqyz_response = 'Quiz id missing or empty';
    include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
    exit; 
}

if(!isset($quiz_category) || empty($quiz_category)) {
    $proqyz_response = 'Quiz category missing or empty';
    include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
    exit; 
}

if( !in_array($quiz_category, [ "ielts-listening", "ielts-reading", "ielts-writing", "ielts-speaking" ]) ) {
    $proqyz_response = 'Invalid quiz category';
    include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
    exit; 
}


$fetch_rest_quiz = false;
$session_obj     = [];

# begin:: fetch course
$where_params_course = [
    "where" => (object) [],
    "grant_type" => "request",
    "permission" => "course"
];


$main_params_course = array_merge($where_params_course,[
    "request_id" => date("U")
]);

// rest api - get coruse
$rest_course = (object) ST_ProQyz_Oauth::rest_post_request("/courses/course/".$proqyz_course_id, $main_params_course);
if( isset($rest_course->success) ) {
    $remote_c_res  = (object) $rest_course->success;
    $remote_c_json = isset($remote_c_res->json)? (object) $remote_c_res->json : null;
    
    if( !$remote_c_json ){
        // remote - json
        $proqyz_response = "Invalid Response from Server";
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/400.php';
        exit;
    }

    $c = isset($remote_c_json->_c)? (object) $remote_c_json->_c : null;
    if( !$c ){
        // null - c
        // remote - json
        $proqyz_response = "Null Response from Server";
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/400.php';
        exit;
    }

    $res_course     = isset($c->course)? (object) $c->course : null;
    $res_lessons    = isset($c->lessons)? (array) $c->lessons : [];
    $fetch_rest_quiz = true;
    $session_obj = [
        "course_title"      => $res_course->title,
        "course"            => $res_course,
        "lessons"           => $res_lessons,
        "session_category"  => "fullmock-test",
        "course_id"         => $proqyz_course_id,
        "lesson_id"         => $proqyz_lesson_id,
        'course_category'   => "proqyz-course",
        "quiz_category"     => $proqyz_quiz_category,
        "api_id"            => $proqyz_quiz_id
    ];
}
        
if( isset($rest_course->error) ) {
    $error = (object) $rest_course->error;
    $proqyz_response  = $error->message;
    $proqyz_code      = $error->status;
    if($proqyz_code == "404"){
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
    } else {
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/500.php';
    }
    exit;
}
# end:: proqyz-course 

if( $fetch_rest_quiz ) {

    $where_params = [
        "where" => (object) [],
        "grant_type" => "request",
        "permission" => "quiz"
    ];

    
    $main_params = array_merge($where_params,[
        "request_id" => date("U")
    ]);

    // rest api - fetch quiz
    $response = (object) ST_ProQyz_Oauth::rest_post_request("/quizzes/quiz/".$proqyz_quiz_id, $main_params);
    if (!isset($response->error)) {

        /**
        * sections [html]
        * styles   [html]
        */
        $html_response = load_quiz((object) [
            'category'          => $proqyz_quiz_category,
            'res'               => $response,
            'session'           => (object) $session_obj
        ]);
        
        echo $html_response;
        
    } else {
        $error = (object) $response->error;
        $proqyz_response  = $error->message;
        $proqyz_code      = $error->status;
        if($proqyz_code == "404"){
            include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
        } else {
            include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/500.php';
        }
        exit;
    }
}
