<?php
// Silence is golden.

	$settings = $this->get_settings_for_display();
    $id = $this->get_id();

    if( class_exists('SFWD_LMS') ) :
            $elementor_editor = \Elementor\Plugin::instance()->editor->is_edit_mode();
            if( $elementor_editor ) {
                $course_id = learndash_post_arr()[0];
            } else {
                $course_id = learndash_get_course_id();
            }

            if( is_user_logged_in() ) {
                $user_id = get_current_user_id();
            } else {
                $user_id = 0;
            }

            global $course_pager_results;
            $lessons = learndash_get_course_lessons_list( $course_id , $user_id );
            $quizzes = learndash_get_course_quiz_list( $course_id  );
            $has_course_content = ( ! empty( $lessons ) || ! empty( $quizzes ) );
            $has_access = sfwd_lms_has_access( $course_id, $user_id );
            $course_meta = get_post_meta( $course_id, '_sfwd-courses', true );
            $show_course_content = ( ! $has_access && 'on' === $course_meta['sfwd-courses_course_disable_content_table'] ? false : true );
            $has_lesson_quizzes = learndash_30_has_lesson_quizzes( $course_id, $lessons );
            $lesson_progression_enabled = learndash_lesson_progression_enabled( $course_id );
            $has_topics = false;
            $lesson_topics = array();

            if ( ! empty( $lessons ) ) {
                foreach ( $lessons as $lesson ) {

                    $all_topics = learndash_topic_dots( $lesson['post']->ID, false, 'array', null, $course_id );

                    $topic_pager_args = apply_filters(
                        'ld30_ajax_topic_pager_args',
                        array(
                            'course_id' => $course_id,
                            'lesson_id' => $lesson['post']->ID,
                        )
                    );

                    $lesson_topics[ $lesson['post']->ID ] = learndash_process_lesson_topics_pager( $all_topics, $topic_pager_args );

                    if ( ! empty( $lesson_topics[ $lesson['post']->ID ] ) ) {
                        $has_topics = true;
                    }
                }
            }

            if ( $has_course_content && $show_course_content ) :
                ?>
                <div class="learndash-wrapper wkpro-tooltip">
                    <div class="ld-item-list ld-lesson-list">
                        <div class="ld-section-heading">
                            <h2>
                                <?php
                                echo esc_html__('Course Content','widgetkit-pro');
                                ?>
                            </h2>
                            <div class="ld-item-list-actions" data-ld-expand-list="true">
            
                                <?php
                                // Only display if there is something to expand
                                if ( $has_topics || $has_lesson_quizzes ) :
                                    ?>
                                    <div class="ld-expand-button ld-primary-background" id="<?php echo esc_attr( 'ld-expand-button-' . $course_id ); ?>" data-ld-expands="<?php echo esc_attr( 'ld-item-list-' . $course_id ); ?>" data-ld-expand-text="<?php echo esc_attr_e( 'Expand All', 'widgetkit-pro' ); ?>" data-ld-collapse-text="<?php echo esc_attr_e( 'Collapse All', 'widgetkit-pro' ); ?>">
                                        <span class="ld-icon-arrow-down ld-icon"></span>
                                        <span class="ld-text"><?php echo esc_html_e( 'Expand All', 'widgetkit-pro' ); ?></span>
                                    </div> <!--/.ld-expand-button-->
                                    <?php
                                    // TODO @37designs Need to test this
                                    if ( apply_filters( 'learndash_course_steps_expand_all', false, $course_id, 'course_lessons_listing_main' ) ) {
                                        ?>
                                        <script>
                                            jQuery(document).ready(function(){
                                                setTimeout(function(){
                                                    jQuery("<?php echo esc_attr( '#ld-expand-button-' . $course_id ); ?>").trigger('click');
                                                }, 1000);
                                            });
                                        </script>
                                        <?php
                                    }
                                endif;
            
                                ?>
            
                            </div> <!--/.ld-item-list-actions-->
                        </div> <!--/.ld-section-heading-->
            
                        <?php
            
                        learndash_get_template_part(
                            'course/listing.php',
                            array(
                                'course_id'                  => $course_id,
                                'user_id'                    => $user_id,
                                'lessons'                    => $lessons,
                                'lesson_topics'              => ! empty( $lesson_topics ) ? $lesson_topics : [],
                                'quizzes'                    => $quizzes,
                                'has_access'                 => $has_access,
                                'course_pager_results'       => $course_pager_results,
                                'lesson_progression_enabled' => $lesson_progression_enabled,
                            ),
                            true
                        );
        
                        ?>
                        
            
                    </div> <!--/.ld-item-list-->
                </div>
                <?php
            endif;
    
        else :
    ?>
        <div class="plugin-active-massage">
            <h3><?php esc_html_e('Please activate LearnDash LMS plugin first', 'widgetkit-pro');?></h3>
        </div>
    <?php
    endif;