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

namespace Elite_Notification_Lite\Source;

use Appsbd_Lite\V4\libs\AppInput;
use Elite_Notification_Lite\Api\Sanl_Fast_Spring_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_Fast_Spring
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Fast_Spring extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'FASTSPRING';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'FastSpring';

	/**
	 * The on initialize is generated by appsbd
	 */
	public function on_initialize() {
		$this->is_show_title = false;
		add_filter( 'slnf_source_grid_item', array( $this, 'process_web_url' ) );
		Elite_Notification_Source::get_module_instance()->add_ajax_action( 'fs-view-web-url', array( $this, 'view_web_url' ) );
	}
	/**
	 * The get custom fields is generated by appsbd
	 *
	 * @return array|mixed
	 */
	public function get_custom_fields() {

		$cf = array(
			Elite_Notification_Custom_Field::get_textbox( 'fs_api_username', $this->__( 'Username' ), '', 'N', 'Y', $this->__( 'User name' ), '', '', '', 'col-12 col-sm-12 col-md-6', '', '', '' ),
			Elite_Notification_Custom_Field::get_password_box( 'fs_api_key', $this->__( 'API Key' ), '', 'N', 'Y', $this->__( 'API Key' ), '', '', '', 'col-12 col-sm-12 col-md-6', '', '', '' ),
			Elite_Notification_Custom_Field::get_password_box( 'fs_product_id', $this->__( 'FastSpring Product ID' ), '', 'N', 'N', $this->__( 'Product Id' ), '', '<span>' . $this->__( 'Keep Blank for all product' ) . '</span>', '', 'col-12 col-sm-12 col-md-6', '', '', '' ),
			Elite_Notification_Custom_Field::get_password_box( 'fs_product_link', $this->__( 'Product landing page link' ), '', 'N', 'N', $this->__( 'Page Link' ), '', '<span>' . $this->__( "The will be your product link, remember, if you don't select specific product then it will be link for all product" ) . '</span>', '', 'col-12 col-sm-12 col-md-6', '', '', '' ),
			Elite_Notification_Custom_Field::get_textbox(
				'apbd_fs_temp',
				$this->__( 'FastSpring Notification Template' ),
				'[customer] just purchased [nl] from [full address]',
				'N',
				'Y',
				$this->__( 'ex. [customer] just purchased [nl] from [full address]' ),
				'',
				'<span>' . $this->__( 'Use' ) . ' <strong>[customer]</strong> ' . $this->__( 'is for customer name,' ) . ' <strong>[city]</strong> ' . $this->__( 'is for city of customer,' ) . ' <strong>[state]</strong> ' . $this->__( 'is for state of customer,' ) . ' <strong>[country]</strong> ' . $this->__( 'is for country of customer,' ) . ' <strong>[full address]</strong> ' . $this->__( 'is for full address of customer,' ) . ' <strong>[nl]</strong> ' . $this->__( 'is for new line' ) . '</span>',
				'',
				'col-12 col-sm-9'
			),
			Elite_Notification_Custom_Field::get_numeric( 'fs_display_from_last', $this->__( 'Display From the Last' ), 5, 'N', 'Y', $this->__( 'Display From the Last' ), '', '<span>' . $this->__( 'Last how many days will stay?' ) . '</span>', '', 'col-12 col-sm-3', 'text-center', '', $this->__( 'Days' ) ),
		);
		return $cf;
	}
	/**
	 * The view web url is generated by appsbd
	 */
	public function view_web_url() {
	}

	/**
	 * The process web url is generated by appsbd
	 *
	 * @param mixed $item It is item param.
	 *
	 * @return mixed
	 */
	public function process_web_url( $item ) {
		if ( ! empty( $item->source_params['fs_api_web_mode'] ) && 'W' == $item->source_params['fs_api_web_mode'] ) {
			$item->action .= "<a data-effect='mfp-move-from-top' class='popupformWR btn btn-warning btn-xs' href='" . Elite_Notification_Source::get_module_instance()->get_action_url( 'fs-view-web-url', array( 'id' => $item->id ) ) . "'>" . $this->__( 'View Web URL' ) . '</a>';
		}

		return $item;
	}

	/**
	 * 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 ) {
		$fs_api_key           = $source_item->get_param_option( 'fs_api_key' );
		$fs_api_username      = $source_item->get_param_option( 'fs_api_username' );
		$fs_product_id        = $source_item->get_param_option( 'fs_product_id', '' );
		$fs_product_link      = $source_item->get_param_option( 'fs_product_link' );
		$fs_display_from_last = $source_item->get_param_option( 'fs_display_from_last' );
		$apbd_fs_temp         = $source_item->get_param_option( 'apbd_fs_temp' );
		$slnf_style           = $source_item->slnf_style;
		$response_array       = array();
		$fs                   = new Sanl_Fast_Spring_Api( $fs_api_username, $fs_api_key, $fs_product_id );
		$orders               = $fs->get_fast_spring_orders( gmdate( 'Y-m-d', strtotime( '-' . $fs_display_from_last . ' DAYS' ) ), gmdate( 'Y-m-d' ), $source_item->item_count );
		if ( ! empty( $orders ) ) {

			foreach ( $orders as $order ) {
				$total_items = count( $order->items );
				$str_counter = '';
				if ( 0 == $total_items ) {
					continue;
				} elseif ( $total_items > 1 ) {
					$str_counter = ' ' . $this->__( '& %s more products', $total_items - 1 );
				}
				$order_item        =&$order->items[0];
				$review_time_stamp = $order->changed_in_seconds;
				$item              = new Elite_Notification_Item();
				$item->id          = hash( 'crc32b', $order->id );
				$item->source_id   = $source_item->id;

				$item->img          = $order_item->image_url;
				$titlemsg           = $order_item->display . $str_counter;
				$item->des          = '<div class="apbd-dtls-title  apbd-el-first apbd-mb-2" >' . ( $titlemsg ) . '</div><div class=" apbd-dtls-text apbd-with-wrap">' . $this->get_template_msg( $apbd_fs_temp, $order ) . '</div>';
				$item->link_url     = $fs_product_link;
				$item->view_style   = $slnf_style;
				$item->typ          = $this->__( 'Sales' );
				$item->nt_by        = '';
				$item->op_timestamp = $review_time_stamp;
				$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 It is order param.
	 *
	 * @return string|string[]
	 */
	public function get_template_msg( $template, $order ) {
		$data                 = array();
		$data['customer']     = apbd_slnf_short_name( $order->customer->first . ' ' . $order->customer->last, 18 );
		$data['nl']           = '<br/>';
		$data['city']         = $order->address->city;
		$data['state']        = $order->address->region_display;
		$data['country']      = $order->address->country;
		$data['full address'] = $order->address->display;
		foreach ( $data as $prop => $target ) {
			$template = str_replace( '[' . $prop . ']', $target, $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_fastspring.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_fs_temp'         => AppInput::post_value( 'apbd_fs_temp' ),
			'fs_display_from_last' => AppInput::post_value( 'fs_display_from_last' ),
			'fs_api_web_mode'      => AppInput::post_value( 'fs_api_web_mode', 'A' ),
			'fs_web_url'           => AppInput::post_value( 'fs_web_url' ),
			'fs_api_username'      => AppInput::post_value( 'fs_api_username' ),
			'fs_api_key'           => AppInput::post_value( 'fs_api_key' ),
		);
		$is_ok  = true;
		if ( empty( $params['apbd_fs_temp'] ) ) {
			Elite_Notification_Lite::add_error( 'FastSpring template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['fs_display_from_last'] ) ) {
			Elite_Notification_Lite::add_error( 'Display from last has been required' );
			$is_ok = false;
		}
		if ( empty( $params['fs_api_web_mode'] ) ) {
			Elite_Notification_Lite::add_error( 'FastSpring mode type has been required' );
			$is_ok = false;
		}

		if ( ! $is_ok ) {
			return false;
		}
		return $item_object->set_source_param( $params );
	}
}
