<?php 
/**
 * v2.1.0
 * shortcode: Course Grid
 * status: completed
 */

add_shortcode('proqyz-quizzes', 'shortcode_proqyz_list_quiz');
add_shortcode('ilms-quizzes', 'shortcode_proqyz_list_quiz');
function shortcode_proqyz_list_quiz( $atts = [], $content = null, $tag = '' ) {
    global $wp;

    // load header
    $atts = array_change_key_case((array)$atts, CASE_LOWER);
    # override default attributes with user attributes
    $wporg_atts = shortcode_atts(
        array(
            'course_category'       => 'all',
            'handler'               => ''
        ) , $atts, $tag
    );

    $shortcode_url = home_url(add_query_arg(array(),$wp->request));

    $courses_list = "";
    if( class_exists('ST_ProQyz_Oauth') ) {

       

        $params = [
            "where" => (object) [],
            "grant_type" => "request",
            "permission" => "quizzes",
            "request_id" => date("U")
        ];

        $ilms_quizzes_res = (object) ST_ProQyz_Oauth::rest_post_request("/quizzes/list", $params);

        if( isset($ilms_quizzes_res->success) ) {
            $remote_c_res  = (object) $ilms_quizzes_res->success;
            $remote_c_json = isset($remote_c_res->json)? (object) $remote_c_res->json : null;
            if( $remote_c_json ){
                $quizzes = isset($remote_c_json->quizzes)? (array) $remote_c_json->quizzes : [];
                if( count($quizzes) > 0 ) {

                    $course_access  = true;
                    $quiz_html      = "";

                    foreach( $quizzes as $qkey => $quiz ) {
                        $quiz           = (object) $quiz;
                        $quiz_id        = $quiz->_id;
                        $quiz_category  = $quiz->_category;
                        $quiz_type      = $quiz->_type;
                        $quiz_slug      = $quiz->_slug;
                        $quiz_title     = $quiz->title;

                        if($quiz_type == "ielts" && $quiz_category == "reading"){
                            $quiz_category = "ielts-reading";
                        } else if($quiz_type == "ielts" && $quiz_category == "listening"){
                            $quiz_category = "ielts-listening";
                        } else if($quiz_type == "ielts" && $quiz_category == "writing"){
                            $quiz_category = "ielts-writing";
                        } else if($quiz_type == "ielts" && $quiz_category == "speaking"){
                            $quiz_category = "ielts-speaking";
                        } else if($quiz_type == "gov" && $quiz_category == "non-sectional-timing"){
                            $quiz_category = "gov-non-sectional-timing";
                        } else if($quiz_type == "gov" && $quiz_category == "sectional-timing"){
                            $quiz_category = "gov-sectional-timing";
                        } else if($quiz_type == "proqyz" && $quiz_category == "pdf-quiz"){
                            $quiz_category = "proqyz-pdf-quiz";
                        }
                        
                        $session_category = "single";
                        /**
                         * helper shortcode function - checked v2.1.0
                         */
                        $get_session = proqyz_get_user_session_details((object) [
                            "quiz_category"     => $quiz_category,
                            "quiz_type"         => $quiz_type,
                            "quiz_id"           => $quiz_id,
                            "quiz_slug"         => $quiz_slug,
                            "session_category"  => "single",
                            "course_category"   => null,
                            "course_id"         => null,
                            "lesson_id"         => null
                        ]);
                        
                        $lesson_preview = 1;
                        // template file - checked v2.1.0
                        require ST_PROQYZ_ABSPATH .'includes/templates/shortcodes/quiz/default/proqyz-quiz-default-layout-shortcode.php';
                    
                    }

                    // Enqueue assets and styles only once per page
                    static $quiz_list_assets_enqueued = false;
                    if ( ! $quiz_list_assets_enqueued ) {
                        $quiz_list_assets_enqueued = true;

                        // Enqueue ProQyz fonts once per page
                        if ( function_exists('st_proqyz_enqueue_fonts') ) {
                            st_proqyz_enqueue_fonts();
                        }

                        $plugin_dir = plugin_dir_url(ST_PROQYZ_PLUGIN_FILE);
                        wp_enqueue_style('st-proqyz-course-common-css', $plugin_dir . 'includes/templates/shortcodes/course/default/dist/css/common.css', array(), false);
                        wp_enqueue_style('st-proqyz-course-iot-icons-css', $plugin_dir . 'includes/templates/shortcodes/course/default/dist/css/iot-icons.css', array(), false);
                        wp_enqueue_style('st-proqyz-course-style-4-css', $plugin_dir . 'includes/templates/shortcodes/course/default/dist/css/style-4.css', array(), false);
                        wp_enqueue_script("st-proqyz-quiz-single", $plugin_dir . 'includes/templates/shortcodes/quiz/default/dist/js/proqyz-list.js', array( 'jquery' ),'1.0', false ); 
                                    

                        // Default styles (prints CSS once)
                        // require_once ST_PROQYZ_ABSPATH .'includes/templates/shortcodes/course/default/default-style.php';

                        // Add inline CSS to head once using wp_add_inline_style
                        static $inline_css_added = false;
                        if (!$inline_css_added) {
                            $inline_css_added = true;
                            
                            // Register and enqueue a style handle for quiz list
                            wp_register_style('proqyz-quiz-list-css', false);
                            wp_enqueue_style('proqyz-quiz-list-css');
                            wp_add_inline_style('proqyz-quiz-list-css', '.ast-single-post .entry-content a, .ast-comment-content a:not(.ast-comment-edit-reply-wrap a){text-decoration:none!important;}');
                        }
                    }

                    ob_start(); ?>
                    <div class="ielts-lms-root exam-volume-detail exam-detail">
                        <div class="practice-list__card course-card-new ielts-topics" data-content="List of Quizzes">
                            <h5 class="practice-list__title">List of Quizzes</h5>
                            <div class="practice-list__items flex-grid">
                                <?php echo $quiz_html; ?>
                            </div>
                        </div>
                    </div>
                    <!-- end::lesson module --> 
                    <?php return ob_get_clean();

                } else {
                    return "0 Quizzes found";
                }
            } 
        } else {
            $errors = (object) $ilms_quizzes_res->error;
            return $errors->message;
        }
    }

}

