<?php
/**
 * Elit Notification Envato Source
 *
 * @package Elite_Notification_Lite\Source
 */

namespace Elite_Notification_Lite\Source;

use Appsbd_Lite\V4\libs\AppInput;
use Elite_Notification_Lite\Api\Sanl_Envato_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_Envato_Source
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Envato_Source extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'ENVATO-SOURCE';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'Envato';

	/**
	 * 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_password_box(
				'envato_api_key',
				$this->__( 'Envato API Key (Personal Token)' ),
				'',
				'N',
				'Y',
				$this->__( 'Api Key' ),
				'',
				'',
				'',
				'col-12 col-sm-12 col-md-4',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_textbox(
				'env_des_template',
				$this->__( 'Envato Description Template' ),
				'Just sold from [site]',
				'N',
				'Y',
				$this->__( 'ex. Just sold from [site]' ),
				'',
				'<span>' . $this->__(
					'Use',
					'elite-notification'
				) . ' <strong>[site]</strong> ' . $this->__(
					'is for site name (ex. codecanyon.net, themeforest.net)',
					'elite-notification'
				) . '</span>',
				'',
				'col-12 col-sm-8 col-md-5',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_numeric(
				'env_display_from_last',
				$this->__( 'Display From the Last' ),
				'',
				'',
				'Y',
				$this->__( 'Display From the Last' ),
				'',
				'<span>' . $this->__( 'Last how many days will stay?' ) . '</span>',
				'',
				'col-12 col-sm-4 col-md-3',
				'',
				'',
				$this->__( 'Days' ),
			),
		);

		return $cf;
	}

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

	/**
	 * The get from envato is generated by appsbd
	 *
	 * @param mixed $source_item It is source_item param.
	 * @param array $response_array It is response_array param.
	 * @param int   $page It is page param.
	 */
	public function get_from_envato( $source_item, &$response_array = array(), $page = 1 ) {
		$api_key      = $source_item->get_param_option( 'envato_api_key' );
		$des_template = $source_item->get_param_option( 'env_des_template' );
		$slnf_style   = $source_item->slnf_style;
		$valid_api    = new Sanl_Envato_Api( $api_key );
		$sales        = $valid_api->get_sales( $page );

		$daylimit  = $source_item->get_param_option( 'env_display_from_last', 5 );
		$last_date = '';
		if ( $daylimit > 0 ) {
			$last_date = strtotime( '-' . $daylimit . ' DAYS' );
		}
		if ( is_array( $sales ) ) {
			foreach ( $sales as $data ) {
				if ( $daylimit > 0 ) {
					if ( strtotime( $data->sold_at ) < $last_date ) {
						break;
					}
				}
				$item               = new Elite_Notification_Item();
				$item->id           = hash( 'crc32b', serialize( $data ) );
				$item->source_id    = $source_item->id;
				$item->title        = '';
				$item->img          = $data->item->previews->icon_preview->icon_url;
				$item->des          = '<div class=" apbd-el-first apbd-dtls-text apbd-mb-2 apbd-with-wrap">' . $data->item->name . '</div><div class="apbd-dtls-title apbd-with-wrap" >' . $this->get_template_msg(
					$des_template,
					$data
				) . '</div>';
				$item->link_url     = $data->item->url;
				$item->view_style   = $slnf_style;
				$item->typ          = $this->__( 'Sales' );
				$item->nt_by        = '';
				$item->op_timestamp = appsbd_get_wp_time( $data->sold_at );

				$item->op_date    = gmdate(
					get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
					$item->op_timestamp
				);
				$response_array[] = $item;
				if ( count( $response_array ) >= $source_item->item_count ) {
					break;
				}
			}
		}
	}

	/**
	 * 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 ) {
		$response_array = array();
		$this->get_from_envato( $source_item, $response_array );

		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 ) {
		foreach ( array( 'site' => $data->item->site ) 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_envato.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_Envato_Api( $params['envato_api_key'] );
		if ( ! $valid_api->check_api( $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(
			'envato_api_key'        => AppInput::post_value( 'envato_api_key' ),
			'env_des_template'      => AppInput::post_value( 'env_des_template' ),
			'env_display_from_last' => AppInput::post_value( 'env_display_from_last' ),
		);
		$is_ok  = true;
		if ( empty( $params['envato_api_key'] ) ) {
			Elite_Notification_Lite::add_error( 'Envato API Key has been required' );
			$is_ok = false;
		}
		if ( empty( $params['env_des_template'] ) ) {
			Elite_Notification_Lite::add_error( 'Description template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['env_display_from_last'] ) ) {
			Elite_Notification_Lite::add_error( 'Display from last 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 );
	}
}
