<?php
/**
 * Add or remove action
 */
remove_action( 'learn-press/before-main-content', 'learn_press_breadcrumb', 10 );
remove_action( 'learn-press/before-main-content', 'learn_press_search_form', 15 );
remove_action( 'learn-press/courses-loop-item-title', 'learn_press_courses_loop_item_thumbnail', 10 );
remove_action( 'learn-press/after-courses-loop-item', 'learn_press_courses_loop_item_instructor', 25 );
remove_action( 'learn-press/after-courses-loop-item', 'learn_press_courses_loop_item_begin_meta', 10 );
remove_action( 'learn-press/after-courses-loop-item', 'learn_press_courses_loop_item_price', 20 );
remove_action( 'learn-press/after-courses-loop-item', 'learn_press_courses_loop_item_end_meta', 30 );
remove_action( 'learn-press/after-courses-loop-item', 'learn_press_course_loop_item_buttons', 35 );
remove_action( 'learn-press/after-courses-loop-item', 'learn_press_course_loop_item_user_progress', 40 );

//show loop thumbnail
add_action( 'educacion-mascot-courses-loop-item-thumbnail', 'learn_press_courses_loop_item_thumbnail', 10 );

























/**
 * Landing
 */
remove_action( 'learn-press/content-landing-summary', 'learn_press_course_students', 10 );
remove_action( 'learn-press/content-landing-summary', 'learn_press_course_price', 25 );
remove_action( 'learn-press/content-landing-summary', 'learn_press_course_buttons', 30 );


/** * Add media meta data for a course
 *
 * @param $meta_box
 */
if ( ! function_exists( 'educacion_mascot_add_course_meta' ) ) {
	function educacion_mascot_add_course_meta( $meta_box ) {
		$fields   = $meta_box['fields'];
		$fields[] = array(
			'name' => esc_html__( 'Media URL', 'educacion-wp' ),
			'id'   => 'educacion_mascot_lp_course_details_media',
			'type' => 'text',
			'size' => 100,
			'desc' => esc_html__( 'Youtube link or Vimeo Video URL', 'educacion-wp' ),
		);
		$fields[] = array(
			'name' => esc_html__( 'Info Button Box', 'educacion-wp' ),
			'id'   => 'educacion_mascot_lp_course_details_info_button',
			'type' => 'text',
			'size' => 100,
			'desc' => esc_html__( 'Add text info button', 'educacion-wp' ),
		);
		$fields[] = array(
			'name' => esc_html__( 'Includes', 'educacion-wp' ),
			'id'   => 'educacion_mascot_lp_course_details_includes',
			'type' => 'wysiwyg',
			'desc' => esc_html__( 'Includes infomation of Courses', 'educacion-wp' ),
		);
		$meta_box['fields'] = $fields;

		return $meta_box;
	}
}
add_filter( 'learn_press_course_settings_meta_box_args', 'educacion_mascot_add_course_meta' );


/**
 * @param $user
 */
if ( ! function_exists( 'educacion_mascot_extra_user_profile_fields' ) ) {
	function educacion_mascot_extra_user_profile_fields( $user ) {
		$user_info = get_the_author_meta( 'lp_info', $user->ID );
		?>
		<h3><?php esc_html_e( 'LearnPress Profile', 'educacion-wp' ); ?></h3>

		<table class="form-table">
			<tbody>
			<tr>
				<th>
					<label for="lp_major"><?php esc_html_e( 'Major', 'educacion-wp' ); ?></label>
				</th>
				<td>
					<input id="lp_major" class="regular-text" type="text"
					       value="<?php echo isset( $user_info['major'] ) ? $user_info['major'] : ''; ?>"
					       name="lp_info[major]">
				</td>
			</tr>
		</table>
		<?php
	}
}

add_action( 'show_user_profile', 'educacion_mascot_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'educacion_mascot_extra_user_profile_fields' );


function educacion_mascot_save_extra_user_profile_fields( $user_id ) {

	if ( ! current_user_can( 'edit_user', $user_id ) ) {
		return false;
	}
	update_user_meta( $user_id, 'lp_info', $_POST['lp_info'] );
}

add_action( 'personal_options_update', 'educacion_mascot_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'educacion_mascot_save_extra_user_profile_fields' );

/**
 * About the author
 */
if ( !function_exists( 'educacion_mascot_about_author' ) ) {
	function educacion_mascot_about_author() {
		$lp_info = get_the_author_meta( 'lp_info' );
		$link    = '#';
		if ( get_post_type() == 'lpr_course' ) {
			$link = apply_filters( 'learn_press_instructor_profile_link', '#', $user_id = null, get_the_ID() );
		} elseif ( get_post_type() == 'lp_course' ) {
			$link = learn_press_user_profile_link( get_the_author_meta( 'ID' ) );
		} elseif ( is_single() ) {
			$link = get_author_posts_url( get_the_author_meta( 'ID' ) );
		}
		?>
		<div class="tm-course-author">
			<div class="author-wrapper">
				<div class="author-avatar mr-3">
					<?php echo get_avatar( get_the_author_meta( 'ID' ), 110 ); ?>
				</div>
				<div class="author-bio">
					<div class="author-top">
						<a class="name" href="<?php echo esc_url( $link ); ?>">
							<?php echo get_the_author(); ?>
						</a>
						<?php if ( isset( $lp_info['major'] ) && $lp_info['major'] ) : ?>
							<p class="job-position"><?php echo esc_html( $lp_info['major'] ); ?></p>
						<?php endif; ?>
					</div>
					<?php educacion_mascot_get_author_info_social_links(get_the_author_meta( 'ID' )) ?>

					<div class="author-description">
						<?php echo get_the_author_meta( 'description' ); ?>
					</div>
				</div>
			</div>
		</div>
		<?php
		if ( class_exists( 'LearnPress' ) ) {
			educacion_mascot_co_instructors( get_the_ID(), get_the_author_meta( 'ID' ) );
		}

	}
}

/**
 * Display co instructors
 *
 * @param $course_id
 */
if ( ! function_exists( 'educacion_mascot_co_instructors' ) ) {
	function educacion_mascot_co_instructors( $course_id, $author_id ) {
		if ( ! $course_id ) {
			return;
		}

		if ( class_exists( 'LP_Co_Instructor_Preload' ) ) {
			$instructors = get_post_meta( $course_id, '_lp_co_teacher' );
			$instructors = array_diff( $instructors, array( $author_id ) );
			if ( $instructors ) {
				foreach ( $instructors as $instructor ) {
					//Check if instructor not exist
					$user = get_userdata( $instructor );
					if ( $user === false ) {
						break;
					}
					$lp_info = get_the_author_meta( 'lp_info', $instructor );
					$link    = learn_press_user_profile_link( $instructor );
					?>
					<div class="tm-course-author tm-co-instructor">
						<div class="author-wrapper">
							<div class="author-avatar mr-3">
								<?php echo get_avatar( $instructor, 110 ); ?>
							</div>
							<div class="author-bio">
								<div class="author-top">
									<a class="name" href="<?php echo esc_url( $link ); ?>">
										<span><?php echo get_the_author_meta( 'display_name', $instructor ); ?></span>
									</a>
									<?php if ( isset( $lp_info['major'] ) && $lp_info['major'] ) : ?>
										<p class="job-position"><?php echo esc_html( $lp_info['major'] ); ?></p>
									<?php endif; ?>
								</div>
								<?php educacion_mascot_get_author_info_social_links($instructor) ?>

								<div class="author-description">
									<?php echo get_the_author_meta( 'description', $instructor ); ?>
								</div>
							</div>
						</div>
					</div>
					<?php
				}
			}
		}
	}
}


/**
 * Learning
 */
add_action( 'learn-press/begin-section-loop-item', 'educacion_mascot_add_format_icon', 10 );
if ( ! function_exists( 'educacion_mascot_add_format_icon' ) ) {
	function educacion_mascot_add_format_icon( $item ) {
		$format = get_post_format( $item->get_id() );
		if ( get_post_type( $item->get_id() ) == 'lp_quiz' ) {
			echo '<span class="course-format-icon"><i class="fa fa-clock-o"></i></span>';
		} elseif ( $format == 'video' ) {
			echo '<span class="course-format-icon"><i class="fa fa-play"></i></span>';
		} elseif ( $format == 'audio' ) {
			echo '<span class="course-format-icon"><i class="fa fa-music"></i></span>';
		} elseif ( $format == 'image' ) {
			echo '<span class="course-format-icon"><i class="fa fa-picture-o"></i></span>';
		} elseif ( $format == 'aside' ) {
			echo '<span class="course-format-icon"><i class="fa fa-file-o"></i></span>';
		} elseif ( $format == 'quote' ) {
			echo '<span class="course-format-icon"><i class="fa fa-quote-left"></i></span>';
		} elseif ( $format == 'link' ) {
			echo '<span class="course-format-icon"><i class="fa fa-link"></i></span>';
		} else {
			echo '<span class="course-format-icon"><i class="fa fa-file-o"></i></span>';
		}
	}
}