<?php
/**
 * Elite Notification Source Base Library
 *
 * @package Elite_Notification_Lite\Libs
 */

namespace Elite_Notification_Lite\Libs;

use Appsbd_Lite\V4\libs\AppInput;
use Elite_Notification_Lite\Core\Elite_Notification_Lite;
use Elite_Notification_Lite\Modules\Elite_Notification_Source;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * Class Elite_Notification_Source_Base
 *
 * @package Elite_Notification_Lite\Libs
 */
abstract class Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var mixed
	 */
	public $id;
	/**
	 * Its property source_icon
	 *
	 * @var mixed
	 */
	public $source_icon;
	/**
	 * Its property source_name
	 *
	 * @var mixed
	 */
	public $source_name;
	/**
	 * Its property style
	 *
	 * @var mixed
	 */
	public $style;
	/**
	 * Its property textDomain
	 *
	 * @var mixed
	 */
	protected $text_domain;
	/**
	 * Its property params
	 *
	 * @var array
	 */
	public $params = array();
	/**
	 * Its property is_show_title
	 *
	 * @var bool
	 */
	public $is_show_title = true;

	/**
	 * The get post value is generated by appsbd
	 *
	 * @param string $key It is key param.
	 * @param string $default It is default param.
	 *
	 * @return mixed|string
	 */
	public function get_post_value( $key = '', $default = '' ) {
		if ( ! empty( $this->params[ $key ] ) ) {
			$default = $this->params[ $key ];
		}
		return AppInput::post_value( $key, $default );
	}

	/**
	 * The get option is generated by appsbd
	 *
	 * @param string $key It is key param.
	 * @param string $default It is default param.
	 *
	 * @return mixed|string
	 */
	public function get_option( $key = '', $default = '' ) {
		if ( isset( $this->params[ $key ] ) ) {
			return $this->params[ $key ];
		}
		return $default;
	}

	/**
	 * The get file link is generated by appsbd
	 *
	 * @param mixed $file_path It is file_path param.
	 *
	 * @return string
	 */
	public function get_file_link( $file_path ) {
		return plugins_url( $file_path, Elite_Notification_Source::get_module_instance()->plugin_file );
	}

	/**
	 * The set params is generated by appsbd
	 *
	 * @param mixed $source_item It is source_item param.
	 */
	public function set_params( $source_item ) {
		if ( $source_item->source_type == $this->id ) {
			$this->params = $source_item->source_params;
		}
	}

	/**
	 * The get params is generated by appsbd
	 *
	 * @return array
	 */
	public function get_params() {
		return $this->params;
	}

	/**
	 * Elite_Notification_Source_Base constructor.
	 */
	final public function __construct() {
		$this->text_domain = Elite_Notification_Lite::get_instance()->text_domain;
		$this->on_initialize();
	}

	/**
	 * The on initialize is generated by appsbd
	 */
	public function on_initialize() {
	}

	/**
	 * The set from post data is generated by appsbd
	 *
	 * @param mixed $item_object It is item_object param.
	 *
	 * @return mixed
	 */
	abstract public function set_from_post_data( &$item_object );

	/**
	 * The get items is generated by appsbd
	 *
	 * @param mixed $source_item It is source_item param.
	 *
	 * @return mixed
	 */
	abstract public function get_items( $source_item );

	/**
	 * The get icon is generated by appsbd
	 *
	 * @return mixed
	 */
	abstract public function get_icon();


	/**
	 * The get categories is generated by appsbd
	 *
	 * @return mixed
	 */
	abstract public function get_categories();

	/**
	 * The get custom fields is generated by appsbd
	 *
	 * @return mixed
	 */
	abstract public function get_custom_fields();

	/**
	 * The get client settings is generated by appsbd
	 */
	public function get_client_settings() {
		$settings          = new \stdClass();
		$settings->id      = $this->id;
		$settings->name    = $this->source_name;
		$settings->icon    = $this->get_icon();
		$settings->cats    = $this->get_categories();
		$settings->is_name = $this->is_show_title;
		$settings->c_flds  = $this->get_custom_fields();
		return $settings;
	}

	/**
	 * The  e is generated by appsbd
	 *
	 * @param mixed $string It is string param.
	 * @param null  $parameter It is parameter param.
	 * @param null  $_ It is _ param.
	 */
	public function _e( $string, $parameter = null, $_ = null ) {
		$args = func_get_args();
		esc_html( call_user_func_array( array( $this, '__' ), $args ) );
	}

	/**
	 * The  ee is generated by appsbd
	 *
	 * @param mixed $string It is string param.
	 * @param null  $parameter It is parameter param.
	 * @param null  $_ It is _ param.
	 */
	public function _ee( $string, $parameter = null, $_ = null ) {
		$args = func_get_args();
		foreach ( $args as &$arg ) {
			if ( is_string( $arg ) ) {
				$arg = $this->__( $arg );
			}
		}
		esc_html( call_user_func_array( 'sprintf', $args ) );
	}

	/**
	 * The    is generated by appsbd
	 *
	 * @param mixed $string It is string param.
	 * @param null  $parameter It is parameter param.
	 * @param null  $_ It is _ param.
	 *
	 * @return mixed
	 */
	public function __( $string, $parameter = null, $_ = null ) {
		$args = func_get_args();
		array_splice( $args, 1, 0, array( $this->text_domain ) );
		return call_user_func_array( array( Elite_Notification_Source::get_module_instance(), '__' ), $args );
	}

	/**
	 * The     is generated by appsbd
	 *
	 * @param mixed $string It is string param.
	 * @param null  $parameter It is parameter param.
	 * @param null  $_ It is _ param.
	 *
	 * @return mixed
	 */
	public function ___( $string, $parameter = null, $_ = null ) {
		$args = func_get_args();
		foreach ( $args as &$arg ) {
			if ( is_string( $arg ) ) {
				$arg = $this->__( $arg );
			}
		}
		return call_user_func_array( 'sprintf', $args );
	}

	/**
	 * The query log is generated by appsbd
	 *
	 * @param bool   $is_start It is isStart param.
	 * @param string $pre_line It is pre_line param.
	 * @param false  $is_clean It is isClean param.
	 */
	public function query_log( $is_start = true, $pre_line = '', $is_clean = false ) {

		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			global $wpdb;
			if ( $is_start ) {
				$this->qm_length = count( $wpdb->queries );
			} else {
				$now_length = count( $wpdb->queries );
				for ( $i = $this->qm_length; $i < $now_length; $i++ ) {
					$q = $wpdb->queries[ $i ];
				}
			}
		}
	}
}
