<?php
/**
 * Elite Notification Easy Digital Downloads 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_Easy_Digital_Downloads
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Easy_Digital_Downloads extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'EASY-DIGITAL';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'Easy Digital Download';

	/**
	 * 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_easy_digital',
				$this->__( 'Easy Digital Download Template' ),
				'[customer] just purchased',
				'N',
				'Y',
				$this->__( 'ex. [customer] just purchased' ),
				'',
				'<span>' . $this->__( 'Use', 'elite-notification' ) . ' <strong>[customer]</strong> ' . $this->__( 'is for customer name' ) . '</span>',
				'',
				'col-12 col-sm-12 col-md-6',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_numeric(
				'easy_digital_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-2',
				'',
				'',
				$this->__( 'Days' ),
			),
			Elite_Notification_Custom_Field::get_radio(
				'edd_image_type',
				$this->__( 'Image Type' ),
				'P',
				'',
				array(
					array(
						'val'   => 'P',
						'title' => $this->__( 'Product Image' ),
					),
					array(
						'val'   => 'C',
						'title' => $this->__( 'Customer Image' ),
					),
				),
				'',
				'',
				'',
				'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( 'sales' );
	}

	/**
	 * 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 ) {
		$easy_template         = $source_item->get_param_option( 'apbd_easy_digital' );
		$review_interval_input = $source_item->get_param_option( 'easy_digital_display_from_last', 0 );
		$slnf_style            = $source_item->slnf_style;
		$image_type            = $source_item->get_param_option( 'edd_image_type', 'P' );
		$args                  = array(
			'numberposts' => $source_item->item_count,
			'orderby'     => 'date',
			'order'       => 'DESC',
			'post_type'   => 'edd_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, '_edd_payment_meta', true );
			if ( ! empty( $meta_keys['cart_details'] ) ) {
				$items            =& $meta_keys['cart_details'];
				$selected_item    = null;
				$order_item_count = is_countable( $items ) ? count( $items ) : 0;
				foreach ( $items as $item ) {
					if ( ! $selected_item ) {
						$selected_item = $item;
					} elseif ( $item['item_price'] > $selected_item['item_price'] ) {
						$selected_item = $item;
					}
				}
				$titlemsg = ( $order_item_count > 1 ? apbd_slnf_short_name(
					$selected_item['name'],
					23
				) . ' ' . $this->__( '& %s more products', $order_item_count ) : $selected_item['name'] );

				$item            = new Elite_Notification_Item();
				$item->id        = hash( 'crc32b', $post->ID );
				$item->source_id = $source_item->id;
				if ( 'U' == $image_type ) {
					$item->img = get_avatar_url( $meta_keys['user_info']['id'], 32 );
				} else {
					$item->img = get_the_post_thumbnail_url( $selected_item['id'] );
				}

				$item->des          = '<div class=" apbd-el-first apbd-mb-2 apbd-dtls-text">' . $this->get_template_msg(
					$easy_template,
					$meta_keys
				) . '</div><div class="apbd-dtls-title apbd-with-wrap" >' . ( $titlemsg ) . '</div>';
				$item->link_url     = get_permalink( $selected_item['id'] );
				$item->view_style   = $slnf_style;
				$item->typ          = $this->__( 'Sales' );
				$item->nt_by        = '';
				$item->op_timestamp = strtotime( $post->post_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 $order_info It is order_info param.
	 *
	 * @return string|string[]
	 */
	public function get_template_msg( $template, $order_info ) {
		$customer = get_user_by( 'ID', $order_info['user_info']['id'] );
		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 );

		$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_edd.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_easy_digital'              => AppInput::post_value( 'apbd_easy_digital' ),
			'easy_digital_display_from_last' => AppInput::post_value( 'easy_digital_display_from_last' ),
			'edd_image_type'                 => AppInput::post_value( 'edd_image_type' ),
		);
		$is_ok  = true;
		if ( empty( $params['apbd_easy_digital'] ) ) {
			Elite_Notification_Lite::add_Error( 'Easy Digital Download template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['easy_digital_display_from_last'] ) ) {
			Elite_Notification_Lite::add_Error( 'Display from last has been required' );
			$is_ok = false;
		}
		if ( empty( $params['edd_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 );
	}
}
