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

	/**
	 * 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( 'wp_review', $this->__( 'Wp Review 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( '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(
				'how_many_star_wp',
				$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 ) {
		$wp_template           = $source_item->get_param_option( 'wp_review' );
		$how_many_star_wp      = $source_item->get_param_option( 'how_many_star_wp', 1 );
		$review_interval_input = $source_item->get_param_option( 'display_from_last', 0 );
		if ( $review_interval_input ) {
			$limit_date = strtotime( '-' . $review_interval_input . ' days' );
		}
		$slnf_style = $source_item->slnf_style;
		$args       = array(
			'numberposts' => 100,
			'orderby'     => 'date',
			'order'       => 'DESC',
			'post_type'   => 'xs_review',
			'date_query'  => array(
				'column' => 'post_date',
				'after'  => '- ' . $review_interval_input . ' days',
			),
		);

		$posts          = get_posts( $args );
		$response_array = array();
		foreach ( $posts as $post ) {
			$review_timestamp = strtotime( $post->post_date );
			$post_meta        = get_post_meta( $post->ID, 'xs_public_review_data', true );
			if ( ! empty( $post_meta ) ) {
				$post_meta = json_decode( trim( $post_meta ) );
			}
			if ( ! isset( $post_meta->xs_reviwer_ratting ) || ( $post_meta->xs_reviwer_ratting * 1 < $how_many_star_wp * 1 ) ) {
				continue;
			}
			$item               = new Elite_Notification_Item();
			$item->id           = hash( 'crc32b', wp_json_encode( $post ) );
			$item->source_id    = $source_item->id;
			$item->img          = get_avatar_url( $post->ID, 32 );
			$item->des          = '<div class=" apbd-el-first apbd-dtls-text">' . $this->get_template_msg( $wp_template, $post_meta ) . '</div><div class="apbd-dtls-title" >' . ( ! empty( $post->post_title ) ? $post->post_title : '' ) . '</div><span class="apbd-dtls-rating">' . apbd_rating_stars( $post_meta->xs_reviwer_ratting ) . '</span></div>';
			$item->link_url     = $post->guid;
			$item->view_style   = $slnf_style;
			$item->typ          = $this->__( 'Review' );
			$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 ) {
		if ( ! empty( $post->xs_reviwer_name ) ) {
			$post->xs_reviwer_name = apbd_slnf_short_name( $post->xs_reviwer_name );
		}
		foreach ( array( 'xs_reviwer_name' => 'reviewer' ) as $prop => $target ) {
			if ( property_exists( $post, $prop ) ) {
				$template = str_replace( '[' . $target . ']', $post->$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_wpreview.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(
			'wp_review'         => AppInput::post_value( 'wp_review' ),
			'display_from_last' => AppInput::post_value( 'display_from_last' ),
			'how_many_star_wp'  => AppInput::post_value( 'how_many_star_wp' ),
		);
		$is_ok  = true;
		if ( empty( $params['wp_review'] ) ) {
			Elite_Notification_Lite::add_error( 'WP Review template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['display_from_last'] ) ) {
			Elite_Notification_Lite::add_error( 'Display from last has been required' );
			$is_ok = false;
		}
		if ( empty( $params['how_many_star_wp'] ) ) {
			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 );
	}
}
