<?php
namespace Elementor\LearnDash;

defined('ABSPATH') || exit;

class WKFE_PRO_LD_CPT_Metabox {
    
    public function __construct( ) {
        $this->url = plugin_dir_url(__FILE__);

        add_action( 'add_meta_boxes', [$this,'wkfe_ld_template_course_cover_photo'] );
        add_action( 'add_meta_boxes', [$this,'wkfe_ld_template_course_short_description'] );
        add_action( 'add_meta_boxes', [$this,'wkfe_ld_template_course_level'] );
        add_action( 'add_meta_boxes', [$this,'wkfe_ld_template_course_estimated_time'] );
        add_action( 'save_post', [$this,'save_wkfe_ld_template_course_custom_info'] );
        add_action('admin_enqueue_scripts',[$this,'wkfe_ld_template_course_cover_photo_scripts']);
    }

    public function wkfe_ld_template_course_cover_photo_scripts($screen) {
        $screen = get_current_screen();
        if( 'sfwd-courses' == $screen->id ) {
            
            if( function_exists( 'wp_enqueue_media' ) ) {
                wp_enqueue_media();
            }

            wp_enqueue_script('wkfe-ld-template-image-upload',$this->url. 'assets/js/image-upload.js',array('jquery'),'1.0',true);
        }
    }

    public function wkfe_ld_template_course_cover_photo() {
        add_meta_box( 'wkfe-ld-template-course-cover-photoid', __( 'Course Cover Photo', 'widgetkit-pro' ), [$this,'wkfe_ld_template_course_cover_photo_display_cb'], 'sfwd-courses' );
    }

    public function wkfe_ld_template_course_short_description() {
        add_meta_box( 'wkfe-ld-template-course-short-descid', __( 'Course Short Description', 'widgetkit-pro' ), [$this,'wkfe_ld_template_course_short_description_display_cb'], 'sfwd-courses' );
    }

    public function wkfe_ld_template_course_level() {
        add_meta_box( 'wkfe-ld-template-course-level-id', __( 'Course Difficulties Level', 'widgetkit-pro' ), [$this,'wkfe_ld_template_course_level_display_cb'], 'sfwd-courses' );
    }

    public function wkfe_ld_template_course_estimated_time() {
        add_meta_box( 'wkfe-ld-template-course-estimated-time-id', __( 'Course Estimated Time', 'widgetkit-pro' ), [$this,'wkfe_ld_template_course_estimated_time_display_cb'], 'sfwd-courses' );
    }

    public function wkfe_ld_template_course_cover_photo_display_cb($post) {
        
        $imgurl = get_post_meta($post->ID,'_wkfe_ld_template_course_cover_photo',true);
    
        wp_nonce_field( 'wkfe_ld_template_course_cover_photo_custom_box', 'wkfe_ld_template_course_cover_photo_nonce' );
        ?>
            <input id="wkfe-ld-template-course-cover-photo" class="regular-text" name="wkfe-ld-template-course-cover-photo" type="text" value="<?php echo esc_url($imgurl); ?>">
            <button class="button wkfe_ld_template_course_cover_photo_upload"><?php esc_html_e( 'Upload Image', 'widgetkit-pro' ); ?></button>
        <?php
    }

    public function wkfe_ld_template_course_short_description_display_cb($post) {
        $desc = get_post_meta($post->ID,'_wkfe_ld_template_course_short_desc',true);
    
        wp_nonce_field( 'wkfe_ld_template_course_short_desc_custom_box', 'wkfe_ld_template_course_short_descnonce' );
        ?>
            <textarea rows="4" cols="50" id="wkfe-ld-template-course-short-desc" class="large-text" name="wkfe-ld-template-course-short-desc" placeholder="<?php echo esc_attr( 'Write course description here', 'widgetkit-pro' ); ?>"><?php echo wp_kses_post($desc); ?></textarea>
        <?php
    }

    public function wkfe_ld_template_course_level_display_cb($post) {
        $difficulties = get_post_meta($post->ID,'_wkfe_ld_template_course_difficulties_level',true);
        wp_nonce_field( 'wkfe_ld_template_course_difficulties_level_custom_box', 'wkfe_ld_template_course_difficulties_levelnonce' );
        ?>
            <select name="wkfe_ld_template_course_difficulties_level" >
                <option <?php selected('beginner',$difficulties); ?> value="beginner"><?php echo esc_html__('Beginner','widgetkit-pro'); ?></option>
                <option  <?php selected('intermediate',$difficulties); ?> value="intermediate"><?php echo esc_html__('Intermediate','widgetkit-pro'); ?></option>
                <option  <?php selected('advanced',$difficulties); ?> value="advanced"><?php echo esc_html__('Advanced','widgetkit-pro'); ?></option>
            </select>
        <?php
    }

    public function wkfe_ld_template_course_estimated_time_display_cb($post) {
        $estimatedtime = get_post_meta($post->ID,'_wkfe_ld_template_course_estimated_time',true);
        $estimatedtimeperweek = get_post_meta($post->ID,'_wkfe_ld_template_course_estimated_time_per_week',true);
        wp_nonce_field( 'wkfe_ld_template_course_estimated_time_custom_box', 'wkfe_ld_template_course_estimated_time_nonce' );
        ?>  
            <div class="wkfe-ld-template-metabox-row" style="display:flex;padding:15px 0;">
                <div class="wkfe-ld-template-metabox-left" style="width: 18%;padding:0 2% 0 0;">
                    <label for="wkfe-ld-template-course-estimated-time">
                        <?php echo esc_html__('Estimated Time','widgetkit-pro'); ?>
                    </label>
                </div>
                <div class="wkfe-ld-template-metabox-right" style="width: 80%;">
                    <input id="wkfe-ld-template-course-estimated-time" class="regular-text" name="wkfe-ld-template-course-estimated-time" type="text" value="<?php echo esc_html($estimatedtime); ?>">
                </div>
            </div>
            <div class="wkfe-ld-template-metabox-row" style="display:flex;padding:15px 0;">
                <div class="wkfe-ld-template-metabox-left" style="width: 18%;padding:0 2% 0 0;">
                    <label for="wkfe-ld-template-course-estimated-time-per-week">
                        <?php echo esc_html__('Estimated Time Per Week','widgetkit-pro'); ?>
                    </label>
                </div>
                <div class="wkfe-ld-template-metabox-right" style="width: 80%;">
                    <input id="wkfe-ld-template-course-estimated-time-per-week" class="regular-text" name="wkfe-ld-template-course-estimated-time-per-week" type="text" value="<?php echo esc_html($estimatedtimeperweek); ?>">
                </div>
            </div>
        <?php
    }   

    public function save_wkfe_ld_template_course_custom_info($post_id) {

        if ( ! isset( $_POST['wkfe_ld_template_course_estimated_time_nonce'] ) || ! isset( $_POST['wkfe_ld_template_course_cover_photo_nonce'] ) || ! isset( $_POST['wkfe_ld_template_course_short_descnonce'] ) || ! isset( $_POST['wkfe_ld_template_course_difficulties_levelnonce'] ) ) {
            return $post_id;
        }
        
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        $imagedata = $_POST['wkfe-ld-template-course-cover-photo'];
        $diffcultiesdata = $_POST['wkfe_ld_template_course_difficulties_level'];
        $descdata = sanitize_text_field($_POST['wkfe-ld-template-course-short-desc']);
        $estimatedtimedata = sanitize_text_field($_POST['wkfe-ld-template-course-estimated-time']);
        $estimatedtimeperweekdata = sanitize_text_field($_POST['wkfe-ld-template-course-estimated-time-per-week']);

        update_post_meta( $post_id, '_wkfe_ld_template_course_cover_photo', $imagedata );
        update_post_meta( $post_id, '_wkfe_ld_template_course_short_desc', $descdata );
        update_post_meta( $post_id, '_wkfe_ld_template_course_difficulties_level', $diffcultiesdata );
        update_post_meta( $post_id, '_wkfe_ld_template_course_estimated_time', $estimatedtimedata );
        update_post_meta( $post_id, '_wkfe_ld_template_course_estimated_time_per_week', $estimatedtimeperweekdata );
    }

}

new WKFE_PRO_LD_CPT_Metabox();