<?php
/**
 * Elite Notification Elite Licenser 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;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Ens_Elite_Licenser_Source
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Elite_Licenser_Source extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'APBDELS';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'Elite Licenser';

	/**
	 * 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(
				'api_end_point',
				$this->__( 'API End Point' ),
				'',
				'N',
				'Y',
				$this->__( 'API End Point' ),
				'',
				'',
				'',
				'col-12 col-sm-12 col-md-6',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_textbox(
				'api_key',
				$this->__( 'API Key' ),
				'',
				'N',
				'Y',
				$this->__( 'API Key' ),
				'',
				'',
				'',
				'col-12 col-sm-12 col-md-6',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_textbox(
				'des_template',
				$this->__( 'Description Template' ),
				'[customer] just purchased',
				'N',
				'Y',
				$this->__( 'ex. [customer] just purchased' ),
				'',
				'<span>' . $this->__(
					'Use',
					'elite-notification'
				) . ' <strong>[customer]</strong> ' . $this->__(
					'is for customer name,',
					'elite-notification'
				) . ' <strong>[product]</strong> ' . $this->__(
					'is for product name,',
					'elite-notification'
				) . ' <strong>[license]</strong> ' . $this->__(
					'is for license title,',
					'elite-notification'
				) . ' <strong>[time]</strong> ' . $this->__(
					'is for entry time',
					'elite-notification'
				) . '</span>',
				'',
				'col-12 ',
				'',
				'',
				''
			),
		);

		return $cf;
	}

	/**
	 * 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 ) {
		$api_end_point  = $source_item->get_param_option( 'api_end_point' );
		$api_key        = $source_item->get_param_option( 'api_key' );
		$des_template   = $source_item->get_param_option( 'des_template' );
		$slnf_style     = $source_item->slnf_style;
		$source_by      = $source_item->title;
		$valid_api      = new Sanl_Elite_Api( $api_end_point, $api_key );
		$error          = '';
		$license_list   = $valid_api->get_license_list( $source_item->item_count, $error );
		$response_array = array();
		foreach ( $license_list as $data ) {
			$item               = new Elite_Notification_Item();
			$item->source_id    = $source_item->id;
			$item->title        = $data->product_name;
			$item->img          = get_avatar_url( $data->client_email, 32 );
			$item->des          = '<div class=" apbd-dtls-title apbd-mb-2  apbd-el-first" >' . $data->product_name . '</div><div class="apbd-dtls-text apbd-with-wrap ">' . $this->get_template_msg(
				$des_template,
				$data
			) . '</div>';
			$item->view_style   = $slnf_style;
			$item->link_url     = ! empty( $data->product_link ) ? $data->product_link : '';
			$item->typ          = $this->__( 'Sales' );
			$item->nt_by        = '';
			$item->op_timestamp = strtotime( $data->entry_time );
			$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->client_name ) ) {
			$data->client_name = apbd_slnf_short_name( $data->client_name, 17 );
		}
		foreach (
			array(
				'client_name'   => 'customer',
				'product_name'  => 'product',
				'license_title' => 'license',
				'entry_time'    => 'time',
			) 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_elite_licenser.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(
			'api_key'       => AppInput::post_value( 'api_key' ),
			'api_end_point' => AppInput::post_value( 'api_end_point' ),
			'des_template'  => AppInput::post_value( 'des_template' ),
		);
		$is_ok  = true;
		if ( empty( $params['api_key'] ) ) {
			Elite_Notification_Lite::add_error( 'API Key has been required' );
			$is_ok = false;
		}
		if ( empty( $params['api_end_point'] ) ) {
			Elite_Notification_Lite::add_error( 'API End Point has been required' );
			$is_ok = false;
		}
		if ( empty( $params['des_template'] ) ) {
			Elite_Notification_Lite::add_error( 'Description template has been required' );
			$is_ok = false;
		}
		if ( $is_ok && ! $this->check_api( $params ) ) {
			$is_ok = false;
		}
		if ( ! $is_ok ) {
			return false;
		}

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