<?php
/**
 * Elite Notification Source Base
 *
 * @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_Give_Donation
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Give_Donation extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'GIVE';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'Give';

	/**
	 * The on initialize is generated by appsbd
	 */
	public function on_initialize() {
		parent::on_initialize();
		$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_give_donation_temp', $this->__( 'Give Donation Template' ), '[donor] just donated', '', 'Y', $this->__( 'ex. [donor] just donated' ), '', '<span>' . $this->__( 'Use' ) . ' <strong>[donor]</strong> ' . $this->__( 'is for donor name,' ) . '</span>', '', 'col-12 col-sm-12 col-md-6', '', '', '' ),
			Elite_Notification_Custom_Field::get_numeric( 'give_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-4', '', '', $this->__( 'Days' ) ),
			Elite_Notification_Custom_Field::get_radio(
				'give_image_type',
				$this->__( 'Image Type' ),
				'F',
				'',
				array(
					array(
						'val'   => 'F',
						'title' => $this->__( 'Featured Image' ),
					),
					array(
						'val'   => 'G',
						'title' => $this->__( 'Gravater Image' ),
					),
				),
				'',
				'',
				'',
				'col-6 col-sm-6 col-md-4',
				''
			),
			Elite_Notification_Custom_Field::get_radio(
				'give_amount_show',
				$this->__( 'Amount Show' ),
				'D',
				'',
				array(
					array(
						'val'   => 'D',
						'title' => $this->__( 'Donation Amount' ),
					),
					array(
						'val'   => 'N',
						'title' => $this->__( 'None' ),
					),
				),
				'',
				'',
				'',
				'col-6 col-sm-6 col-md-4',
				''
			),
			Elite_Notification_Custom_Field::get_radio(
				'give_title_show',
				$this->__( 'Donation Title' ),
				'E',
				'',
				array(
					array(
						'val'   => 'E',
						'title' => $this->__( 'Enable' ),
					),
					array(
						'val'   => 'A',
						'title' => $this->__( 'Anonymous Title' ),
					),
				),
				'',
				'',
				'',
				'col-6 col-sm-6 col-md-4',
				''
			),
		);
		return $cf;
	}
	/**
	 * The set edd actions is generated by appsbd
	 */
	public static function set_edd_actions() {
		add_action(
			'save_post_edd_payment',
			function ( $post_id, $post ) {
				Elite_Notification_Source::set_load_queue();
			},
			10,
			2
		);
	}

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

	/**
	 * 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 ) {
		$give_template         = $source_item->get_param_option( 'apbd_give_donation_temp' );
		$review_interval_input = $source_item->get_param_option( 'give_display_from_last', 0 );
		$give_amount           = $source_item->get_param_option( 'give_amount_show', 'D' );
		$give_title            = $source_item->get_param_option( 'give_title_show', 'E' );
		$give_image_type       = $source_item->get_param_option( 'give_image_type', 'F' );
		$slnf_style            = $source_item->slnf_style;
		$args                  = array(
			'numberposts' => $source_item->item_count,
			'orderby'     => 'date',
			'order'       => 'DESC',
			'post_type'   => 'give_payment',
			'limit'       => $source_item->item_count,
			'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 );
				$item            = new Elite_Notification_Item();
				$item->id        = hash( 'crc32b', $post->ID );
				$item->source_id = $source_item->id;
			if ( 'F' == $give_image_type ) {
				$item->img = get_the_post_thumbnail_url( $meta_keys['_give_payment_form_id'][0] );
			} else {
				$item->img = get_avatar_url( $meta_keys['_give_payment_donor_id'][0] );
			}
			if ( 'D' == $give_amount ) {
				$amnt_float = $this->get_amount_msg( $meta_keys );
			}
			if ( 'E' == $give_title ) {
				$give_title_data = $this->getDonateTitle_msg( $meta_keys );
			} else {
				$give_title_data = 'Anonymous Product';
			}
				$item->des          = '<div class=" apbd-el-first apbd-mb-2 apbd-dtls-text">' . $this->get_template_msg( $give_template, $meta_keys ) . '</div><div class="apbd-dtls-title apbd-with-wrap" >' . $amnt_float . ' ' . $give_title_data . '</div>';
				$item->link_url     = $meta_keys['_give_current_url'][0];
				$item->view_style   = $slnf_style;
				$item->typ          = $this->__( 'Donation' );
				$item->nt_by        = '';
				$item->op_timestamp = strtotime( $post->post_date );
				$item->op_date      = gmdate( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $meta_keys['_give_completed_date'][0] );

				$response_array[] = $item;

		}

		return $response_array;
	}


	/**
	 * The get template msg is generated by appsbd
	 *
	 * @param mixed $template It is template param.
	 * @param mixed $post_meta It is post_meta param.
	 *
	 * @return string|string[]
	 */
	public function get_template_msg( $template, $post_meta ) {
		$params = array();
		if ( isset( $post_meta['_give_donor_billing_first_name'][0] ) && isset( $post_meta['_give_donor_billing_last_name'][0] ) ) {
			$params['donor'] = apbd_slnf_short_name( $post_meta['_give_donor_billing_first_name'][0] . ' ' . $post_meta['_give_donor_billing_last_name'][0], 15 );
		}
		foreach ( $params as  $prop => $target ) {
			$template = str_replace( '[' . $prop . ']', $target, $template );
		}
		return $template;
	}

	/**
	 * The get amount msg is generated by appsbd
	 *
	 * @param mixed $post_meta It is post_meta param.
	 *
	 * @return string
	 */
	public function get_amount_msg( $post_meta ) {

		if ( isset( $post_meta['_give_payment_total'][0] ) && isset( $post_meta['_give_payment_currency'][0] ) ) {
			$params = sprintf( '%.2f', $post_meta['_give_payment_total'][0] ) . ' ' . $post_meta['_give_payment_currency'][0] . ' for';
		}
		return $params;
	}

	/**
	 * The getDonateTitle msg is generated by appsbd
	 *
	 * @param mixed $post_meta_title It is post_meta_title param.
	 *
	 * @return mixed
	 */
	public function getDonateTitle_msg( $post_meta_title ) {

		if ( isset( $post_meta_title['_give_payment_form_title'][0] ) ) {
			$params = $post_meta_title['_give_payment_form_title'][0];
		}
		return $params;
	}

	/**
	 * The get icon is generated by appsbd
	 *
	 * @return mixed|string
	 */
	public function get_icon() {
		return $this->get_file_link( 'images/brand_images/source_give.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_give_donation_temp' => AppInput::post_value( 'apbd_give_donation_temp' ),
			'give_display_from_last'  => AppInput::post_value( 'give_display_from_last' ),
			'give_image_type'         => AppInput::post_value( 'give_image_type' ),
			'give_amount_show'        => AppInput::post_value( 'give_amount_show' ),
			'give_title_show'         => AppInput::post_value( 'give_title_show' ),
		);
		$is_ok  = true;
		if ( empty( $params['apbd_give_donation_temp'] ) ) {
			Elite_Notification_Lite::add_error( 'Donation Give template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['give_display_from_last'] ) ) {
			Elite_Notification_Lite::add_error( 'Display from last has been required' );
			$is_ok = false;
		}if ( empty( $params['give_image_type'] ) ) {
			Elite_Notification_Lite::add_error( 'Image type has been required' );
			$is_ok = false;
		}if ( empty( $params['give_amount_show'] ) ) {
			Elite_Notification_Lite::add_error( 'Give amount has been required' );
			$is_ok = false;
		}if ( empty( $params['give_title_show'] ) ) {
			Elite_Notification_Lite::add_error( 'Donation title has been required' );
			$is_ok = false;
		}
		if ( ! $is_ok ) {
			return false;
		}
		return $item_object->set_source_param( $params );
	}
}
