<?php
/**
 * template: ProQyz fullmock test template
 */
$exit_btn = "";
$proqyz_lesson = (object) $lesson;
$mock_test__course_id = $proqyz_lesson->_courseId;
$mock_test__lesson_id = $proqyz_lesson->_id;
$quizzes = $proqyz_lesson->quizzes;

// body classes
$body_classNames = "user-logged-in path-take-test page-node-type-set has-glyphicons";
// site logo
$custom_logo_id = get_theme_mod( 'custom_logo' );
$logo_url = wp_get_attachment_image_url( $custom_logo_id , 'full' );
$current_user = wp_get_current_user();

# update - slug
$course_url = st_proqyz_get_course_url( $mock_test__course_id );
$exit_btn = '<a href="'.$course_url.'" class="realtest-header__btn-exit">
    Exit<span class="ioticon-log-out"></span>
</a>';

$get_full_test_meta = get_user_meta( $current_user->ID, "proqyz__full-mock-test--$mock_test__course_id-$mock_test__lesson_id", true);
if( $get_full_test_meta) {
    $get_full_test_meta = (object) $get_full_test_meta;
    if(isset($get_full_test_meta->status)) {

        if( $get_full_test_meta->status === "not-started" ) {
            
        } else if(
            $get_full_test_meta->status === "listening-completed--reading-start" || 
            $get_full_test_meta->status === "reading-completed--writing-start" ||
            $get_full_test_meta->status === "writing-completed--speaking-start" ||
            $get_full_test_meta->status === "speaking-completed--finish-test"
        ) {
            /*  $exit_btn = '<a href="javascript:;" class="realtest-header__btn-exit" data-toggle="modal" data-target="#modal-exit-test" data-redirect=".'.$course_url.'">
                Exit<span class="ioticon-log-out"></span>
            </a>';  
            */  
        }
    }
}


// request launcher
if(isset($_GET['request_quiz']) && $_GET['request_quiz'] == true ) {
    $step = "listening";

    
    if( $get_full_test_meta) {
        $get_full_test_meta = (object) $get_full_test_meta;
        if(isset($get_full_test_meta->status)) {
            if($get_full_test_meta->status === "not-started") {
                $current_step = "listening";
            } else if($get_full_test_meta->status === "listening-completed--reading-start") {
                $current_step = "reading";
            } else if($get_full_test_meta->status === "reading-completed--writing-start") {
                $current_step = "writing";
            } else if($get_full_test_meta->status === "writing-completed--speaking-start") {
                $current_step = "speaking";
            } else if($get_full_test_meta->status === "speaking-completed--finish-test") {
                $current_step = "finish-test";
            } 
        }
    }

    wp_send_json((object) get_quiz_by_category($quizzes, $step, $mock_test__course_id, $mock_test__lesson_id));

}

// request rest test
if ( isset($_GET['reset_test']) && $_GET['reset_test'] == true ) {
    if ( is_user_logged_in() ) {
        reset_fullmock_test($mock_test__course_id, $mock_test__lesson_id);
    }
}


?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>" />    
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <?php echo dynamic_css_imports(); ?>
    <link rel="stylesheet" href="<?php echo site_url() . '/wp-content/plugins/spacetree/libs/proqyz/includes/templates/public/quiz/dist/css/polyfixes.css'; ?>" />
    <link rel="stylesheet" href="<?php echo site_url() . '/wp-content/plugins/spacetree/libs/proqyz/includes/templates/public/quiz/dist/css/proqyz-quiz.css'; ?>" />
    <link rel="stylesheet" href="<?php echo site_url() . '/wp-content/plugins/spacetree/libs/proqyz/includes/templates/public/full-mock-test/dist/css/instructions.css'; ?>" />
</head>    
<body class="<?php echo $body_classNames; ?>">
    <div class="dialog-off-canvas-main-canvas" data-off-canvas-main-canvas="">
        <div class="page test-guidance">
            <div class="region region-content">
                <div data-drupal-messages-fallback="" class="hidden"></div>
                <header class="realtest-header">
                    <?php if( $logo_url ) { ?> 
                        <img style="width:145px;" src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo get_bloginfo( 'name' ); ?>" class="realtest-header__logo" />
                    <?php } else { ?>
                        <h4 class="site--title"><?php echo get_bloginfo( 'name' ); ?></h4>    
                    <?php } ?>
                    <div class="realtest-header__user-id hide">XXXXXXX XXXXXXX - 123456</div>
                    <?php echo $exit_btn; ?>
                </header>
                <div class="contextual-region">
                    <div class="container">
                        <?php echo steps($mock_test__course_id, $mock_test__lesson_id, $quizzes); ?>
                    </div>
                </div>
                <?php echo reset_modals($mock_test__course_id, $mock_test__lesson_id); ?>
            </div>
        </div>
    </div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.nicescroll/3.7.6/jquery.nicescroll.min.js" integrity="sha512-zMfrMAZYAlNClPKjN+JMuslK/B6sPM09BGvrWlW+cymmPmsUT1xJF3P4kxI3lOh9zypakSgWaTpY6vDJY/3Dig==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
<script>
    jQuery(document).ready(function($) {
        var iphoneCheck = isiPhoneOriPad();




        function isiPhoneOriPad() {
            var userAgent = navigator.userAgent;
            var isiPhone = /iPhone/i.test(userAgent);
            var isiPad = /iPad/i.test(userAgent);
            var iOS13_iPad = (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
            return isiPhone || isiPad || iOS13_iPad;
        }

        function showStepInstruction() {
            if ($('.btn-full-test__step-1').length) {
                $('.btn-full-test__step-1').on('click', function(e) {
                    e.preventDefault();
                    $('.realtest-header__btn-exit').removeClass('hide');
                    $('.step-instruction').removeClass('hide');
                    $('.step-guidance').addClass('hide');
                });
            }
        }

        function showStep4Skills() {
            if (!$('.instruction-box__btn').length) return;

            $('.instruction-box__btn').on('click', function(e) {
                e.preventDefault();
                if ($(this).hasClass('-disabled')) return;

                $(this).addClass('ajax-preload');

                // Replace with your WordPress AJAX URL or REST API endpoint
                var url = window.location.href;

                if (url) {
                    $.ajax({
                        url: url,
                        type: 'GET',
                        dataType: 'json',
                        data: {
                            request_quiz: true
                        },
                        success: function(data) {
                            if (data.status === 'success') {
                                // localStorage.setItem('id_simulation_' + data.node_id, data.ielts_score);
                                // var $link_skill = data.url_taketest_listening;

                                $('.test-card.-listening.-active .test-card__btn-skill')
                                    .attr('data-href', data?.url);
                                $('.step-instruction').addClass('hide');
                                $('.step-4-skills').removeClass('hide');
                            }
                        },
                        error: function() {
                            $(this).removeClass('ajax-preload');
                            alert('There was an error during the initialization of the test simulation, please click again or reload the page.');
                        }
                    });
                } else {
                    console.warn("url not found");
                }
            });
        }

        function clickStartTest() {
            $('.test-card__btn-skill').on('click', function(e) {
                if ($(this).hasClass('-disabled')) return;
                var href = $(this).data('href');
                if (href) {
                    window.location.href = href;
                }
            });
        }

        function enableBtnConfirmIntructions() {
            if (!$('.instruction-box__video').length) return;

            let vid = $('.instruction-box__video')[0];
            vid.ontimeupdate = function() {
                if (this.currentTime >= this.duration) {
                    $('.instruction-box__btn').removeClass('-disabled');
                }
            };
        }

        function enableBtnStartNowEachSkills() {
            if (!$('.test-card.-active .test-card__video').length) return;

            let vid = $('.test-card.-active .test-card__video')[0];
            vid.ontimeupdate = function() {
                if (this.currentTime >= this.duration) {
                    var button_skills = $('.test-card.-active .test-card__btn-skill');
                    var button_title = button_skills.data('title');
                    var button_href = button_skills.data('href');
                    if (button_href) {
                        button_skills
                            .addClass('-start-now')
                            .html(button_title)
                            .removeClass('-disabled');
                    }
                }
            };
        }

        function scrollSkillActive() {
            if ($('.step-4-skills').length && !$('.step-4-skills').hasClass('hide')) {
                $(window).on('load', function() {
                    $('html, body').animate({
                        scrollTop: $('.test-card.-active').offset().top - $('.realtest-header').height() - 50
                    }, 500);
                });
            }
        }

        function exitTest() {
            $('.realtest-header__btn-exit.-confirm').on('click', function(e) {
                e.preventDefault();
                var url = $(this).attr('data-href');
                if (url) {
                    $('.modal-exit-test .-btn-exits-test').attr('href', url);
                }
            });
        }

        function checkIOS() {
            if (!iphoneCheck) return;
            $('#modal-refusal').modal('show');
        }

        // Run functions
        checkIOS();

        if (!iphoneCheck) {
            showStepInstruction();
            showStep4Skills();
            enableBtnConfirmIntructions();
            enableBtnStartNowEachSkills();
            clickStartTest();
            scrollSkillActive();
            exitTest();
        }

    });

</script>
</body>
</html>

