<?php
/**
 * Class LP_Quiz_CURD.
 *
 * @author  ThimPress
 * @package LearnPress/Classes/CURD
 * @since   3.0.0
 */

/**
 * Prevent loading this file directly
 */
defined( 'ABSPATH' ) || exit();

if ( ! function_exists( 'LP_Quiz_CURD' ) ) {

	/**
	 * Class LP_Quiz_CURD
	 *
	 * Class to manipulating quiz with database.
	 */
	class LP_Quiz_CURD extends LP_Object_Data_CURD implements LP_Interface_CURD {

		/**
		 * LP_Quiz_CURD constructor.
		 */
		public function __construct() {
			$this->_error_messages = array(
				'QUIZ_NOT_EXISTS' => __( 'The quiz does not exist.', 'learnpress' ),
			);
		}

		/**
		 * @param LP_Quiz $quiz
		 *
		 * @return LP_Quiz|mixed
		 * @throws Exception
		 */
		public function load( &$quiz ) {
			$id = $quiz->get_id();
			if ( ! $id || learn_press_get_post_type( $id ) !== LP_QUIZ_CPT ) {

				throw new Exception( sprintf( __( 'Invalid quiz with ID "%d".', 'learnpress' ), $id ) );
			}

			// LP_Helper_CURD::update_meta_cache( $quiz->get_id() );

			$quiz->set_data_via_methods(
				array(
					// 'show_result'          => get_post_meta( $quiz->get_id(), '_lp_show_result', true ),
					'passing_grade_type'   => get_post_meta( $quiz->get_id(), '_lp_passing_grade_type', true ),
					'passing_grade'        => get_post_meta( $quiz->get_id(), '_lp_passing_grade', true ),
					// 'show_check_answer'    => get_post_meta( $quiz->get_id(), '_lp_show_check_answer', true ),
					// 'count_check_answer'   => get_post_meta( $quiz->get_id(), '_lp_check_answer_count', true ),
					// 'show_hint'            => get_post_meta( $quiz->get_id(), '_lp_show_hint', true ),
					// 'archive_history'      => get_post_meta( $quiz->get_id(), '_lp_archive_history', true ),
					// 'count_hint'           => get_post_meta( $quiz->get_id(), '_lp_hint_count', true ),
					'pagination'           => get_post_meta( $quiz->get_id(), '_lp_pagination', true ),
					'review_questions'     => get_post_meta( $quiz->get_id(), '_lp_review', true ),
					'show_correct_review'  => get_post_meta( $quiz->get_id(), '_lp_show_correct_review', true ),
					'preview'              => get_post_meta( $quiz->get_id(), '_lp_preview', true ),
					// 'minus_points'         => get_post_meta( $quiz->get_id(), '_lp_minus_points', true ),
					'minus_skip_questions' => get_post_meta( $quiz->get_id(), '_lp_minus_skip_questions', true ),

					'instant_check'        => get_post_meta( $quiz->get_id(), '_lp_instant_check', true ),
					'negative_marking'     => get_post_meta( $quiz->get_id(), '_lp_negative_marking', true ),
					'retake_count'         => get_post_meta( $quiz->get_id(), '_lp_retake_count', true ),
				)
			);

			return $quiz;
		}

		/**
		 * Create quiz, with default meta.
		 *
		 * @param  $args
		 *
		 * @return int|WP_Error
		 */
		public function create( &$args ) {

			$args = wp_parse_args(
				$args,
				array(
					'id'      => '',
					'status'  => 'publish',
					'title'   => __( 'New Quiz', 'learnpress' ),
					'content' => '',
					'author'  => learn_press_get_current_user_id(),
				)
			);

			$quiz_id = wp_insert_post(
				array(
					'ID'           => $args['id'],
					'post_type'    => LP_QUIZ_CPT,
					'post_status'  => $args['status'],
					'post_title'   => $args['title'],
					'post_content' => $args['content'],
					'post_author'  => $args['author'],
				)
			);

			if ( $quiz_id ) {
				// add default meta for new lesson
				$default_meta = LP_Quiz::get_default_meta();

				if ( is_array( $default_meta ) ) {
					foreach ( $default_meta as $key => $value ) {
						update_post_meta( $quiz_id, '_lp_' . $key, $value );
					}
				}
			}

			return $quiz_id;
		}

		public function update( &$quiz ) {
			// TODO: Implement update() method.
		}

		/**
		 * Delete quiz.
		 *
		 * @since 3.0.0
		 *
		 * @param object $quiz_id
		 */
		public function delete( &$quiz_id ) {
			// course curd
			$curd = new LP_Course_CURD();

			// allow hook
			do_action( 'learn-press/before-delete-quiz', $quiz_id );

			// remove quiz from course items
			$curd->remove_item( $quiz_id );
			// remove questions from quiz
			$this->remove_questions( $quiz_id, '', true );
		}

		/**
		 * Duplicate quiz.
		 *
		 * @since 3.0.0
		 *
		 * @param       $quiz_id_origin
		 * @param array   $args
		 *
		 * @return mixed|WP_Error
		 */
		public function duplicate( &$quiz_id_origin, $args = array() ) {
			$quiz_id_new = 0;

			try {
				if ( ! $quiz_id_origin ) {
					return new WP_Error( 'lp/clone/quiz/id_null', 'Op! Quiz ID not found' );
				}

				if ( learn_press_get_post_type( $quiz_id_origin ) != LP_QUIZ_CPT ) {
					return new WP_Error( 'lp/clone/quiz/type_invalid', 'Op! The quiz does not exist' );
				}

				// Ensure that user can create quiz
				if ( ! current_user_can( 'edit_posts' ) ) {
					return new WP_Error( 'lp/clone/quiz/permission', 'Sorry! You have not permission to duplicate this quiz' );
				}

				// Duplicate quiz
				$quiz_id_new = learn_press_duplicate_post( $quiz_id_origin, $args, true );

				if ( ! $quiz_id_new || is_wp_error( $quiz_id_new ) ) {
					return new WP_Error( 'lp/clone/quiz/clone_error', 'Sorry! Failed to duplicate quiz!' );
				} else {
					// duplicate questions.
					$this->duplicate_questions( $quiz_id_origin, $quiz_id_new );

					do_action( 'learn-press/item/after-duplicate', $quiz_id_origin, $quiz_id_new, $args );
				}
			} catch ( Throwable $e ) {
				error_log( $e->getMessage() );
			}

			return $quiz_id_new;
		}

		/**
		 * Duplicate questions.
		 *
		 * @param int $quiz_id_origin
		 * @param int $quiz_id_new
		 *
		 * @return void
		 */
		public function duplicate_questions( int $quiz_id_origin, int $quiz_id_new ) {
			$question_curd = new LP_Question_CURD();

			try {
				$quiz_origin = learn_press_get_quiz( $quiz_id_origin );
				if ( ! $quiz_origin ) {
					return;
				}

				$questions_origin = $quiz_origin->get_question_ids();

				foreach ( $questions_origin as $question_id_origin ) {
					$can_clone = true;
					$args      = compact( 'question_id_origin', 'quiz_origin', 'quiz_id_new' );
					$can_clone = apply_filters( 'lp/quiz/question/can-clone', $can_clone, $args );

					if ( ! $can_clone ) {
						continue;
					}

					// duplicate question
					$question_id_new = $question_curd->duplicate( $question_id_origin, array( 'post_status' => 'publish' ) );

					if ( ! $question_id_new || is_wp_error( $question_id_new ) ) {
						continue;
					}

					// add duplicate question to new quiz
					$this->add_question( $quiz_id_new, $question_id_new );
				}
			} catch ( Throwable $e ) {
				error_log( $e->getMessage() );
			}
		}

		/**
		 * @param LP_Quiz $quiz
		 *
		 * @deprecated 4.1.7
		 */
		/*protected function _load_questions( &$quiz ) {
			$id        = $quiz->get_id();
			$questions = LP_Object_Cache::get( 'questions-' . $id, 'learn-press/quizzes' );

			if ( false === $questions || $quiz->get_no_cache() ) {
				$this->load_questions( $quiz->get_id() );
			}
		}*/

		/**
		 * Read question of a quiz from database
		 *
		 * @param int    $quiz_id
		 * @param string $context
		 *
		 * @return array|bool
		 */
		/*public function read_questions( $quiz_id, $context = 'display' ) {
			$quiz = learn_press_get_quiz( $quiz_id );

			if ( ! $quiz ) {
				return false;
			}

			return $quiz->get_question_ids( $context );
		}*/

		/**
		 * Read all question ids of a quiz.
		 *
		 * @param int    $quiz_id
		 * @param string $context
		 *
		 * @return array
		 */
		public function read_question_ids( int $quiz_id = 0, string $context = 'display' ): array {
			$lp_question_db = LP_Question_DB::getInstance();
			$question_ids   = [];

			try {
				$quiz = learn_press_get_quiz( $quiz_id );
				if ( ! $quiz ) {
					return array();
				}

				if ( $context === 'display' ) {
					$statuses = array( 'publish' );
				} else {
					$statuses = array( 'publish', 'draft', 'auto-draft' );
				}

				$filter          = new LP_Question_Filter();
				$filter->ID = $quiz_id;
				$filter->post_status = $statuses;

				$question_ids = $lp_question_db->get_list_question_ids_of_quiz( $filter );
			} catch ( Throwable $e ) {
				error_log( $e->getMessage() );
			}

			return $question_ids;
		}

		/**
		 * @deprecated 4.1.7
		 */
		/*public function update_question_ids( $quiz_id ) {
			wp_cache_delete( 'quiz-' . $quiz_id, 'quiz-questions' );

			$quiz = learn_press_get_quiz( $quiz_id );

			if ( $quiz ) {
				$question_ids = $quiz->get_question_ids();
			} else {
				$question_ids = array();
			}

			update_post_meta( $quiz_id, '_questions', $question_ids );
			update_post_meta( $quiz_id, '_question_count', sizeof( $question_ids ) );
		}*/

		/**
		 * Get quiz ids that contains a question.
		 *
		 * @since 3.1.0
		 *
		 * @param int $question_id
		 *
		 * @return array
		 */
		public function get_quiz_by_question( $question_id ) {
			global $wpdb;
			$query = $wpdb->prepare(
				"
				SELECT ID
				FROM {$wpdb->posts} p
				INNER JOIN {$wpdb->learnpress_quiz_questions} qq ON qq.quiz_id = p.ID
				WHERE qq.question_id = %d
			",
				$question_id
			);

			return $wpdb->get_col( $query );
		}

		public function load_questions( $quiz_id ) {

			global $wpdb;

			if ( is_array( $quiz_id ) ) {
				foreach ( $quiz_id as $q_id ) {
					$this->load_questions( $q_id );
				}

				return;
			}

			$query = $wpdb->prepare(
				"
				SELECT ID, post_title, post_content, post_status, post_type, post_author, post_date, post_name
				FROM {$wpdb->posts} p
				INNER JOIN {$wpdb->prefix}learnpress_quiz_questions qq ON p.ID = qq.question_id
				WHERE qq.quiz_id = %d
				AND p.post_status = %s
				ORDER BY question_order, quiz_question_id ASC
			",
				$quiz_id,
				'publish'
			);

			$question_ids = array();
			$results      = $wpdb->get_results( $query );

			if ( $results ) {
				foreach ( $results as $k => $v ) {
					settype( $v, 'object' );
					wp_cache_set( $v->ID, $v, 'posts' );

					$question_ids[] = $v->ID;
				}
			}

			LP_Object_Cache::set( 'questions-' . $quiz_id, $question_ids, 'learn-press/quizzes' );

			// LP_Helper_CURD::cache_posts( $question_ids );
			$question_factory = new LP_Question_CURD();
			$question_factory->load_answer_options( $question_ids );
		}

		/**
		 * Sort questions by order.
		 * Check in an array of questions if there is a key 'order'.
		 *
		 * @param $questions
		 *
		 * @return mixed
		 */
		protected function _maybe_sort_questions( &$questions ) {
			if ( ! $questions ) {
				return $questions;
			}

			$first = reset( $questions );

			if ( empty( $first['order'] ) ) {
				return $questions;
			}

			uasort( $questions, array( $this, '_callback_sort_questions' ) );

			return $questions;
		}

		public function _callback_sort_questions( $a, $b ) {
			return $a['order'] > $b['order'];
		}

		/**
		 * Reorder question by indexed number.
		 *
		 * @param LP_Quiz|WP_Post|int $the_quiz
		 * @param mixed               $questions
		 *
		 * @return mixed
		 */
		public function reorder_questions( $the_quiz, $questions = false ) {
			global $wpdb;

			$the_quiz = learn_press_get_quiz( $the_quiz );

			if ( ! $the_quiz ) {
				return false;
			}

			if ( false == $questions ) {
				$query = $wpdb->prepare(
					"
				SELECT quiz_question_id as id
				FROM {$wpdb->prefix}learnpress_quiz_questions
				WHERE quiz_id = %d
				ORDER BY question_order ASC
			",
					$the_quiz->get_id()
				);

				$rows = $wpdb->get_results( $query );

				if ( $rows ) {
					$update = array();
					$ids    = wp_list_pluck( $rows, 'id' );
					$format = array_fill( 0, sizeof( $ids ), '%d' );

					foreach ( $rows as $order => $row ) {
						$update[] = $wpdb->prepare( 'WHEN quiz_question_id = %d THEN %d', $row->id, $order + 1 );
					}

					$query = $wpdb->prepare(
						"
							UPDATE {$wpdb->prefix}learnpress_quiz_questions
							SET question_order = CASE
							" . join( "\n", $update ) . '
							ELSE question_order END
							WHERE quiz_question_id IN(' . join( ',', $format ) . ')
						',
						$ids
					);

					return $wpdb->query( $query );
				}
			} else {
				$query = "
				UPDATE {$wpdb->learnpress_quiz_questions}
				SET question_order = CASE
			";
				for ( $order = 0, $n = sizeof( $questions ); $order < $n; $order ++ ) {
					$query .= $wpdb->prepare( 'WHEN question_id = %d THEN %d', $questions[ $order ], $order + 1 ) . "\n";
				}
				$query .= sprintf( 'ELSE question_order END WHERE quiz_id = %d', $the_quiz->get_id() );

				return $wpdb->query( $query );
			}

			return false;
		}


		/**
		 * Get all questions in a quiz
		 *
		 * @param LP_Quiz $the_quiz
		 *
		 * @return array|mixed
		 */
		public function get_questions( $the_quiz ) {
			$the_quiz = learn_press_get_quiz( $the_quiz );

			if ( ! $the_quiz ) {
				return $this->get_error( 'QUIZ_NOT_EXISTS' );
			}

			return LP_Object_Cache::get( 'questions-' . $the_quiz->get_id(), 'learn-press/quizzes' );
		}

		/**
		 * Add existing question into quiz.
		 *
		 * @param LP_Quiz|int $the_quiz
		 * @param             $question_id
		 * @param array       $args
		 *
		 * @return mixed false on failed
		 */
		public function add_question( $the_quiz, $question_id, $args = array() ) {
			$the_quiz = learn_press_get_quiz( $the_quiz );

			if ( ! $the_quiz ) {
				return $this->get_error( 'QUIZ_NOT_EXISTS' );
			}

			$question = learn_press_get_question( $question_id );

			if ( ! $question ) {
				return false;
			}

			if ( $this->is_exists_question( $the_quiz->get_id(), $question_id ) ) {
				return false;
			}

			// list exist quiz question
			//$list_questions = $this->get_questions( $the_quiz );
			// add new question and set to cache
			//$list_questions[ $question_id ] = strval( $question_id );
			//LP_Object_Cache::set( 'questions-' . $the_quiz->get_id(), $list_questions, 'learn-press/quizzes' );

			global $wpdb;

			$id   = $the_quiz->get_id();
			$args = wp_parse_args( $args, array( 'order' => - 1 ) );

			if ( $args['order'] >= 0 ) {
				$query = $wpdb->prepare(
					"
						UPDATE {$wpdb->prefix}learnpress_quiz_questions
						SET question_order = question_order + 1
						WHERE quiz_id = %d AND question_order >= %d
					",
					$id,
					$args['order']
				);
				$wpdb->get_results( $query );
			} else {
				$query = $wpdb->prepare(
					"
				SELECT max(question_order) + 1 as ordering
				FROM {$wpdb->prefix}learnpress_quiz_questions
				WHERE quiz_id = %d
			",
					$id
				);

				$order = $wpdb->get_var( $query );

				if ( ! $order ) {
					$order = 1;
				}

				$args['order'] = $order;
			}

			$inserted = $wpdb->insert(
				$wpdb->prefix . 'learnpress_quiz_questions',
				array(
					'quiz_id'        => $id,
					'question_id'    => $question_id,
					'question_order' => $args['order'],
				),
				array( '%d', '%d', '%d' )
			);

			$return = $inserted ? $wpdb->insert_id : $inserted;

			do_action( 'learn-press/quiz-added-question', $inserted, $question_id, $the_quiz->get_id() );

			return $return;
		}

		/**
		 * Check if a question (or batch of questions) is already added to quiz.
		 *
		 * @param int       $the_id
		 * @param int|array $ids
		 *
		 * @return array|bool|null|object
		 */
		public function is_exists_question( $the_id, $ids ) {
			global $wpdb;

			settype( $ids, 'array' );
			$format = array_fill( 0, sizeof( $ids ), '%d' );

			$questions_ids = implode( ', ', $ids );

			$query = $wpdb->prepare(
				"
			SELECT quiz_question_id
			FROM {$wpdb->learnpress_quiz_questions}
			WHERE question_id IN( " . join( ',', $format ) . ' )
				AND quiz_id = %d
		',
				$questions_ids,
				$the_id
			);

			$results = $wpdb->get_results( $query );

			if ( $results ) {
				return $results;
			}

			return false;
		}

		/**
		 * Update order question in quiz.
		 *
		 * @param $order
		 *
		 * @return false|int
		 */
		public function sort_questions( $order ) {
			global $wpdb;

			foreach ( $order as $index => $question_id ) {
				$update = $wpdb->update(
					$wpdb->learnpress_quiz_questions,
					array( 'question_order' => $index + 1 ),
					array( 'question_id' => $question_id ),
					array( '%d' ),
					array( '%d' )
				);

				if ( $update === false ) {
					return false;
				}
			}

			return true;
		}

		/**
		 * Remove question from quiz.
		 *
		 * @since 3.0.0
		 *
		 * @param      $quiz_id
		 * @param      $question_id
		 * @param bool        $force | remove all questions from quiz
		 *
		 * @return bool|false|int|WP_Error
		 */
		public function remove_questions( $quiz_id, $question_id, $force = false ) {
			$quiz = learn_press_get_quiz( $quiz_id );

			if ( ! $quiz ) {
				return $this->get_error( 'QUIZ_NOT_EXISTS' );
			}

			global $wpdb;

			if ( $force ) {
				// remove all questions from quiz
				$delete = $wpdb->delete(
					$wpdb->prefix . 'learnpress_quiz_questions',
					array( 'quiz_id' => $quiz_id ),
					array( '%d' )
				);
			} else {
				do_action( 'learn-press/delete-quiz-question', $question_id, $quiz_id );

				// remove question from quiz
				$delete = $wpdb->delete(
					$wpdb->prefix . 'learnpress_quiz_questions',
					array(
						'quiz_id'     => $quiz_id,
						'question_id' => $question_id,
					),
					array( '%d', '%d' )
				);

				do_action( 'learn-press/deleted-quiz-question', $question_id, $quiz_id, $delete );

				// reorder questions
				$this->reorder_questions( $quiz );
			}

			// increment quiz questions
			learn_press_reset_auto_increment( 'learnpress_quiz_questions' );

			return $delete;
		}

		/**
		 * Update order quiz question answers.
		 *
		 * @since 3.0.0
		 *
		 * @param $answers
		 *
		 * @return array
		 */
		public function sort_question_answers( $answers ) {
			global $wpdb;

			$orders = array();

			foreach ( $answers as $index => $answer_id ) {
				$order = $index + 1;

				$orders[ $answer_id ] = $order;

				$wpdb->update(
					$wpdb->learnpress_question_answers,
					array( 'order' => $order ),
					array( 'question_answer_id' => $answer_id )
				);
			}

			return $orders;
		}

		public function add_meta( &$object, $meta ) {
			// TODO: Implement add_meta() method.
		}

		public function delete_meta( &$object, $meta ) {
			// TODO: Implement delete_meta() method.
		}

		public function read_meta( &$object ) {
			// TODO: Implement read_meta() method.
		}

		/**
		 * @param $object
		 * @param $meta
		 *
		 * @return mixed
		 */
		public function update_meta( &$object, $meta ) {
			return learn_press_update_user_item_meta( $object->get_user_item_id(), $meta->meta_key, $meta->meta_value );
		}
	}
}
