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

namespace Elite_Notification_Lite\Source;

use Appsbd_Lite\V4\libs\AppInput;
use Elite_Notification_Lite\Api\Sanl_Elite_Api;
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_Comment_Source
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Comment_Source extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'SELF-COMMENT';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'WordPress Comment';

	/**
	 * The on initialize is generated by appsbd
	 */
	public function on_initialize() {
		add_action( 'comment_post', array( $this, 'set_load_request' ), 10, 2 );
		add_action(
			'deleted_comment',
			function ( $comment_id ) {
				Elite_Notification_Source::set_load_queue();
			}
		);
		add_action(
			'edit_comment',
			function ( $comment_id ) {
				Elite_Notification_Source::set_load_queue();
			}
		);
		$this->is_show_title = false;
	}

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

	/**
	 * The set load request is generated by appsbd
	 *
	 * @param mixed $comment_id It is comment_ID param.
	 * @param mixed $comment_approved It is comment_approved param.
	 */
	public function set_load_request( $comment_id, $comment_approved ) {
		if ( 1 === $comment_approved ) {
			Elite_Notification_Source::set_load_queue();
		}
	}

	/**
	 * 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 ) {
		$des_template = $source_item->get_param_option( 'des_template' );
		$slnf_style   = $source_item->slnf_style;
		$args         = array(
			'number'       => $source_item->item_count,
			'comment_type' => 'comment',
		);
		if ( $source_item->get_param_option( 'comment_status', 'P' ) ) {
			$args['comment_approved'] = 1;
		}
		$comment_interval_input = $source_item->get_param_option( 'display_from_last', 0 );
		$comments               = get_comments( $args );

		if ( $comment_interval_input ) {
			$limit_date = strtotime( '-' . $comment_interval_input . ' days' );
		}

		$response_array = array();

		foreach ( $comments as $comment ) {
			$comment_timestamp = strtotime( $comment->comment_date );
			if ( $comment_interval_input > 0 ) {
				if ( $comment_timestamp < $limit_date ) {
					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(
				$des_template,
				$comment
			) . '</div><div class="apbd-dtls-title" >' . ( ! empty( $post_obj->post_title ) ? $post_obj->post_title : '' ) . '</div>';
			$item->link_url     = get_comment_link( $comment );
			$item->view_style   = $slnf_style;
			$item->typ          = $this->__( 'Comment' );
			$item->nt_by        = '';
			$item->op_timestamp = $comment_timestamp;
			$item->op_date      = gmdate(
				get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
				$item->op_timestamp
			);
			$response_array[]   = $item;
		}

		return $response_array;
	}

	/**
	 * The get comment time is generated by appsbd
	 *
	 * @param int $comment_id It is comment_id param.
	 *
	 * @return string
	 */
	public function get_comment_time( $comment_id = 0 ) {
		return sprintf(
			$this->__( '%s ago' ),
			human_time_diff(
				get_comment_date( 'U', $comment_id ),
				current_time( 'timestamp' )
			)
		);
	}

	/**
	 * 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' => 'user' ) 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_wordpress.svg' );
	}

	/**
	 * The check_api is generated by appsbd
	 *
	 * @param mixed $params It is params param.
	 *
	 * @return bool
	 */
	public function check_api( $params ) {
		$valid_api = new Sanl_Elite_Api( $params['api_end_point'], $params['api_key'] );
		if ( ! $valid_api->has_permission( $error ) ) {
			Elite_Notification_Lite::add_error( $error );

			return false;
		}

		return true;
	}

	/**
	 * 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(
			'des_template'      => AppInput::post_value( 'des_template' ),
			'comment_status'    => AppInput::post_value( 'comment_status' ),
			'display_from_last' => AppInput::post_value( 'display_from_last' ),
		);
		$is_ok  = true;
		if ( empty( $params['des_template'] ) ) {
			Elite_Notification_Lite::add_error( 'Description template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['comment_status'] ) ) {
			Elite_Notification_Lite::add_error( 'Status of comment has been required' );
			$is_ok = false;
		}
		if ( empty( $params['display_from_last'] ) ) {
			Elite_Notification_Lite::add_error( 'Display From the Last has been required' );
			$is_ok = false;
		}

		if ( ! $is_ok ) {
			return false;
		}

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

	/**
	 * The get custom fields is generated by appsbd
	 *
	 * @return array|mixed
	 */
	public function get_custom_fields() {
		$cf = array(
			Elite_Notification_Custom_Field::get_textbox(
				'des_template',
				$this->__( 'Comment Template' ),
				'[user] just commented',
				'N',
				'Y',
				$this->__( 'Comment Template' ),
				'',
				'<span>' .
				$this->__( 'Use' ) .
				' <strong>[user]</strong> ' .
				$this->__( 'is for customer name,' ) .
				' <strong>[time]</strong> ' .
				$this->__( 'is for entry time' ) .
				'</span>',
				'',
				'col-12 col-sm-12 col-md-6',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_numeric(
				'display_from_last',
				$this->__( 'Display From the Last' ),
				'',
				'N',
				'Y',
				$this->__( 'Display From the Last' ),
				'',
				'<span>' . $this->__( 'Last how many days will stay?' ) . '</span>',
				'',
				'col-6 col-sm-6 col-md-3',
				'',
				'',
				$this->__( 'Days' )
			),
			Elite_Notification_Custom_Field::get_radio(
				'comment_status',
				$this->__( 'Comment Status' ),
				'A',
				'',
				array(
					array(
						'val'   => 'A',
						'title' => $this->__( 'All' ),
					),
					array(
						'val'   => 'P',
						'title' => $this->__( 'Approve' ),
					),
				),
				'',
				'',
				'',
				'col-6 col-sm-6 col-md-3',
				''
			),
		);

		return $cf;
	}
}
