<?php
/**
 * Elite Notification Review X 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_Review_X
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Review_X extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'REVIEW-X';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'ReviewX';

	/**
	 * The on initialize is generated by appsbd
	 */
	public function on_initialize() {
		add_action(
			'comment_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( 'review_x', $this->__( 'ReviewX Template' ), '[reviewer] just reviewed', '', 'Y', $this->__( 'ex. [reviewer] just reviewed' ), '', '<span>' . $this->__( 'Use' ) . ' <strong>[reviewer]</strong> ' . $this->__( 'is for customer name' ) . '</span>', '', 'col-12 col-sm-6 ', '', '', '' ),
			Elite_Notification_Custom_Field::get_numeric( 'reviewx_display_from_last', $this->__( 'Display From the Last' ), '', '', 'Y', $this->__( 'Display From the Last' ), '', '<span>' . $this->__( 'Last how many days will stay?' ) . '</span>', '', 'col-12 col-sm-6 col-md-4', '', '', $this->__( 'Days' ) ),

			Elite_Notification_Custom_Field::get_radio(
				'reviewx_how_many_star',
				$this->__( 'How Many Star?' ),
				'1',
				'',
				array(
					array(
						'val'   => '1',
						'title' => $this->__( 'One Star' ),
					),
					array(
						'val'   => '2',
						'title' => $this->__( 'Two Star' ),
					),
					array(
						'val'   => '3',
						'title' => $this->__( 'Three Star' ),
					),
					array(
						'val'   => '4',
						'title' => $this->__( 'Four Star' ),
					),
				),
				'',
				'<span>' . $this->__( "Below number of star won't take" ) . '</span>',
				'',
				'col-12',
				''
			),
		);
		return $cf;
	}
	/**
	 * The get categories is generated by appsbd
	 *
	 * @return mixed|string[]
	 */
	public function get_categories() {
		return array( 'review' );
	}

	/**
	 * 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 ) {
		$review_x              = $source_item->get_param_option( 'review_x' );
		$how_many_star         = $source_item->get_param_option( 'reviewx_how_many_star' );
		$review_interval_input = $source_item->get_param_option( 'reviewx_display_from_last', 0 );
		$slnf_style            = $source_item->slnf_style;
		$args                  = array(
			'numberposts'      => 100,
			'comment_type'     => 'review',
			'comment_approved' => 1,
			'date_query'       => array(
				'column' => 'comment_date',
				'after'  => '- ' . $review_interval_input . ' days',
			),
		);

		$comment_posts  = get_comments( $args );
		$response_array = array();
		foreach ( $comment_posts as $comment ) {
			$review_timestamp = strtotime( $comment->comment_date );

			$comment_metas = get_comment_meta( $comment->comment_ID, 'rating', true );
			if ( ! isset( $comment_metas ) || ( $comment_metas * 1 < $how_many_star * 1 ) ) {
				continue;
			}
			$post_obj           = get_post( $comment->comment_post_ID );
			$item               = new Elite_Notification_Item();
			$item->id           = hash( 'crc32b', wp_json_encode( $comment ) );
			$item->source_id    = $source_item->id;
			$item->img          = get_avatar_url( $comment, 32 );
			$item->des          = '<div class=" apbd-el-first apbd-dtls-text">' . $this->get_template_msg( $review_x, $comment ) . '</div><div class="apbd-dtls-title" >' . ( ! empty( $post_obj->post_title ) ? $post_obj->post_title : '' ) . '</div><span class="apbd-dtls-rating">' . apbd_rating_stars( $comment_metas ) . '</span></div>';
			$item->link_url     = get_comment_link( $comment );
			$item->view_style   = $slnf_style;
			$item->typ          = $this->__( 'Review' );
			$item->nt_by        = '';
			$item->op_timestamp = appsbd_get_wp_time( $comment->comment_date );
			$item->op_date      = gmdate( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $item->op_timestamp );
			$response_array[]   = $item;

		}
		return $response_array;
	}


	/**
	 * The get template msg is generated by appsbd
	 *
	 * @param mixed $template It is template param.
	 * @param mixed $data It is data param.
	 *
	 * @return string|string[]
	 */
	public function get_template_msg( $template, $data ) {
		if ( ! empty( $data->comment_author ) ) {
			$data->comment_author = apbd_slnf_short_name( $data->comment_author );
		}
		foreach ( array( 'comment_author' => 'reviewer' ) as $prop => $target ) {
			if ( property_exists( $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_reviewx.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(
			'review_x'                  => AppInput::post_value( 'review_x' ),
			'reviewx_display_from_last' => AppInput::post_value( 'reviewx_display_from_last' ),
			'reviewx_how_many_star'     => AppInput::post_value( 'reviewx_how_many_star' ),
		);
		$is_ok  = true;
		if ( empty( $params['review_x'] ) ) {
			Elite_Notification_Lite::add_error( 'ReviewX template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['reviewx_display_from_last'] ) ) {
			Elite_Notification_Lite::add_error( 'Display from last has been required' );
			$is_ok = false;
		}
		if ( empty( $params['reviewx_how_many_star'] ) ) {
			Elite_Notification_Lite::add_error( 'How many star has been required' );
			$is_ok = false;
		}
		if ( ! $is_ok ) {
			return false;
		}
		return $item_object->set_source_param( $params );
	}
}
