<?php
/**
 * Elite Notification Elearning Source
 *
 * @package Elite_Notification_Lite\Source
 */

namespace Elite_Notification_Lite\Source;

use Appsbd_Lite\V4\libs\AppInput;
use Elite_Notification_Lite\Core\Elite_Notification_Lite;
use Elite_Notification_Lite\Libs\Elite_Notification_Custom_Field;
use Elite_Notification_Lite\Libs\Elite_Notification_Item;
use Elite_Notification_Lite\Libs\Elite_Notification_Source_Base;
use Elite_Notification_Lite\Modules\Elite_Notification_Source;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Ens_Elearning
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Elearning extends Elite_Notification_Source_Base {

	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'E-LEARNING';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'eLearning';

	/**
	 * The on initialize is generated by appsbd
	 */
	public function on_initialize() {
		add_action(
			'save_post',
			function ( $post_id, $post ) {

				Elite_Notification_Source::set_load_queue();
			},
			10,
			2
		);
		$this->is_show_title = false;
	}

	/**
	 * The get custom fields is generated by appsbd
	 *
	 * @return array|mixed
	 */
	public function get_custom_fields() {
		$cf = array(
			Elite_Notification_Custom_Field::get_textbox(
				'apbd_elearning_temp',
				$this->__( 'eLearning Template' ),
				'[customer] just enrolled',
				'',
				'Y',
				$this->__( 'ex.[customer] just enrolled' ),
				'',
				'<span>' . $this->__(
					'Use',
					'elite-notification'
				) . ' <strong>[customer]</strong> ' . $this->__(
					'is for customer name,',
					'elite-notification'
				) . '</span>',
				'',
				'col-12 col-sm-12 col-md-6',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_numeric(
				'elearning_display_from_last',
				$this->__( 'Display From the Last' ),
				'',
				'',
				'Y',
				$this->__( 'Display From the Last' ),
				'',
				$this->__( 'Last how many days will stay?' ),
				'',
				'col-6 col-sm-6 col-md-3',
				'',
				'',
				$this->__( 'Days' ),
			),
			Elite_Notification_Custom_Field::get_radio(
				'etutor_image_type',
				$this->__( 'Image Type' ),
				'',
				'',
				array(
					array(
						'val'   => 'C',
						'title' => $this->__( 'Course Image' ),
					),
					array(
						'val'   => 'S',
						'title' => $this->__( 'Student Image' ),
					),
				),
				'',
				'',
				'',
				'col-6 col-sm-6 col-md-3',
				''
			),
		);

		return $cf;
	}

	/**
	 * The get categories is generated by appsbd
	 *
	 * @return mixed|string[]
	 */
	public function get_categories() {
		return array( 'elearning' );
	}

	/**
	 * The get items is generated by appsbd
	 *
	 * @param mixed $source_item It is source_item param.
	 *
	 * @return array|mixed
	 */
	public function get_items( $source_item ) {
		$elearning_template    = $source_item->get_param_option( 'apbd_elearning_temp' );
		$review_interval_input = $source_item->get_param_option( 'elearning_display_from_last', 0 );
		$image_type            = $source_item->get_param_option( 'etutor_image_type', 'U' );
		$slnf_style            = $source_item->slnf_style;
		$args                  = array(
			'numberposts' => $source_item->item_count,
			'orderby'     => 'date',
			'order'       => 'DESC',
			'post_type'   => 'tutor_enrolled',
			'post_status' => 'published',
			'date_query'  => array(
				'column' => 'post_date',
				'after'  => '- ' . $review_interval_input . ' days',
			),
		);

		$posts          = get_posts( $args );
		$response_array = array();

		foreach ( $posts as $post ) {
			$meta_keys = get_post_meta( $post->ID );
			if ( empty( $post->post_parent ) ) {
				continue;
			}
			$parent_post = get_post( $post->post_parent );
			if ( empty( $parent_post ) ) {
				continue;
			}

			$review_timestamp = strtotime( $post->post_date );
			$item             = new Elite_Notification_Item();
			$item->id         = hash( 'crc32b', wp_json_encode( $post ) );
			$item->source_id  = $source_item->id;
			if ( 'U' == $image_type ) {
				$item->img = get_avatar_url( $post->post_author, 32 );
			} else {
				$item->img = get_the_post_thumbnail_url( $post->post_parent );
			}
			$item->des          = '<div class=" apbd-el-first apbd-mb-2 apbd-dtls-text">' . $this->get_template_msg(
				$elearning_template,
				$post
			) . '</div><div class="apbd-dtls-title apbd-with-wrap" >' . ( ! empty( $parent_post->post_title ) ? $parent_post->post_title : '' ) . '</div><span class="apbd-dtls-rating"></span></div>';
			$item->link_url     = $post->guid;
			$item->view_style   = $slnf_style;
			$item->typ          = $this->__( 'eLearning' );
			$item->nt_by        = '';
			$item->op_timestamp = $review_timestamp;
			$item->op_date      = gmdate(
				get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
				$item->op_timestamp
			);
			$response_array[]   = $item;

			if ( count( $response_array ) >= $source_item->item_count ) {
				break;
			}
		}

		return $response_array;
	}


	/**
	 * The get template msg is generated by appsbd
	 *
	 * @param mixed $template It is template param.
	 * @param mixed $post It is post param.
	 *
	 * @return string|string[]
	 */
	public function get_template_msg( $template, $post ) {
		$customer = get_user_by( 'ID', $post->post_author );
		if ( ! empty( $customer->first_name ) ) {
			$customer_name = $customer->first_name . ' ' . $customer->last_name;
		} else {
			$customer_name = $customer->display_name;
		}
		$customer_name = apbd_slnf_short_name( $customer_name, 18 );

		$data             = array();
		$data['customer'] = $customer_name;
		foreach ( array( 'customer' => 'customer' ) as $prop => $target ) {
			if ( isset( $data[ $prop ] ) ) {
				$template = str_replace( '[' . $target . ']', $data[ $prop ], $template );
			}
		}

		return $template;
	}

	/**
	 * The get icon is generated by appsbd
	 *
	 * @return mixed|string
	 */
	public function get_icon() {
		return $this->get_file_link( 'images/brand_images/source_tutor_lms.svg' );
	}

	/**
	 * The set from post data is generated by appsbd
	 *
	 * @param mixed $item_object It is item_object param.
	 *
	 * @return false|mixed
	 */
	public function set_from_post_data( &$item_object ) {
		$params = array(
			'apbd_elearning_temp'         => AppInput::post_value( 'apbd_elearning_temp' ),
			'elearning_display_from_last' => AppInput::post_value( 'elearning_display_from_last' ),
			'etutor_image_type'           => AppInput::post_value( 'etutor_image_type' ),
		);
		$is_ok  = true;
		if ( empty( $params['apbd_elearning_temp'] ) ) {
			Elite_Notification_Lite::add_error( 'eLearning template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['elearning_display_from_last'] ) ) {
			Elite_Notification_Lite::add_error( 'Display from last has been required' );
			$is_ok = false;
		}
		if ( empty( $params['etutor_image_type'] ) ) {
			Elite_Notification_Lite::add_error( 'Image type has been required' );
			$is_ok = false;
		}

		if ( ! $is_ok ) {
			return false;
		}

		return $item_object->set_source_param( $params );
	}
}
