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

namespace Elite_Notification_Lite\Libs;

use Appsbd_Lite\V4\Core\Kernel_Lite;
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_Item
 *
 * @package Elite_Notification_Lite\Libs
 */
class Elite_Notification_Source_Item {
	/**
	 * Its property id
	 *
	 * @var mixed
	 */
	public $id;
	/**
	 * Its property title
	 *
	 * @var mixed
	 */
	public $title;
	/**
	 * Its property item_count
	 *
	 * @var mixed
	 */
	public $item_count;
	/**
	 * Its property source_category
	 *
	 * @var string
	 */
	public $source_category = 'sales';
	/**
	 * Its property source_type
	 *
	 * @var mixed
	 */
	public $source_type;
	/**
	 * Its property status
	 *
	 * @var mixed
	 */
	public $status;
	/**
	 * Its property source_params
	 *
	 * @var array
	 */
	public $source_params = array();
	/**
	 * Its property slnf_style
	 *
	 * @var mixed
	 */
	public $slnf_style;
	/**
	 * Its property click_counter
	 *
	 * @var int
	 */
	public $click_counter = 0;
	/**
	 * Its property view_counter
	 *
	 * @var int
	 */
	public $view_counter = 0;

	/**
	 * The set source param is generated by appsbd
	 *
	 * @param mixed $params It is params param.
	 *
	 * @return bool
	 */
	public function set_source_param( $params ) {
		$this->source_params = $params;
		return true;
	}

	/**
	 * The get param 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_param_option( $key = '', $default = '' ) {
		if ( isset( $this->source_params[ $key ] ) ) {
			return $this->source_params[ $key ];
		}
		return $default;
	}

	/**
	 * The get property raw options is generated by appsbd
	 *
	 * @param mixed $property It is property param.
	 * @param false $is_with_select It is is_with_select param.
	 *
	 * @return array|string[]
	 */
	public function get_property_raw_options( $property, $is_with_select = false ) {

		$return_obj = array(
			'A' => 'Active',
			'I' => 'Inactive',
		);

		if ( $is_with_select ) {
			return array_merge( array( '' => 'Select' ), $return_obj );
		}
		return $return_obj;
	}

	/**
	 * The get property options is generated by appsbd
	 *
	 * @param mixed $property It is property param.
	 * @param false $is_with_select It is is_with_select param.
	 *
	 * @return array|string[]
	 */
	public function get_property_options( $property, $is_with_select = false ) {
		$return_obj = $this->get_property_raw_options( $property, $is_with_select );
		foreach ( $return_obj as &$v ) {
			$v = Elite_Notification_Source::get_module_instance()->__( $v );
		}
		return $return_obj;
	}

	/**
	 * The get property options icon is generated by appsbd
	 *
	 * @param mixed $property It is property param.
	 *
	 * @return array
	 */
	public function get_property_options_icon( $property ) {
		return array();
	}

	/**
	 * The get property options color is generated by appsbd
	 *
	 * @param mixed $property It is property param.
	 *
	 * @return string[]
	 */
	public function get_property_options_color( $property ) {
		return array(
			'A' => 'text-success',
			'I' => 'text-danger',
		);
	}

	/**
	 * The get property options tag is generated by appsbd
	 *
	 * @param mixed  $property It is property param.
	 * @param string $tag It is tag param.
	 * @param string $class_prefix It is class_prefix param.
	 * @param string $class_postfix It is class_postfix param.
	 * @param string $default It is default param.
	 *
	 * @return array|string[]
	 */
	public function get_property_options_tag( $property, $tag = 'span', $class_prefix = 'text-', $class_postfix = '', $default = '' ) {
		$properties = $this->get_property_options( $property );
		if ( count( $properties ) > 0 ) {
			$colors = $this->get_property_options_color( $property );
			$icons  = $this->get_property_options_icon( $property );
			foreach ( $properties as $key => &$value ) {
				$color = ! empty( $colors[ $key ] ) ? $colors[ $key ] : $default;
				$icon  = ! empty( $icons[ $key ] ) ? '<i class="' . $icons[ $key ] . '"></i>' : '';
				$value = "<{$tag} class=\"{$class_prefix}{$color}{$class_postfix}\">{$icon} {$value}</{$tag}>";
			}
		}

		return $properties;
	}

	/**
	 * The set from post data is generated by appsbd
	 *
	 * @return false|mixed
	 */
	public function set_from_post_data() {
		$this->title           = AppInput::post_value( 'title' );
		$this->item_count      = AppInput::post_value( 'item_count' );
		$this->source_type     = AppInput::post_value( 'source_type' );
		$this->source_category = AppInput::post_value( 'source_category' );
		$this->slnf_style      = AppInput::post_value( 'slnf_style' );
		$this->status          = AppInput::post_value( 'status' );
		$is_ok                  = true;
		if ( empty( $this->title ) ) {
			Elite_Notification_Lite::add_error( 'title is required' );
			$is_ok = false;
		}
		if ( empty( $this->item_count ) ) {
			Elite_Notification_Lite::add_error( 'item_count is required' );
			$is_ok = false;
		}
		if ( empty( $this->source_type ) ) {
			Elite_Notification_Lite::add_error( 'source_type is required' );
			$is_ok = false;
		}
		if ( empty( $this->slnf_style ) ) {
			Elite_Notification_Lite::add_error( 'Style has been required' );
			$is_ok = false;
		}
		if ( empty( $this->status ) || ! in_array( $this->status, array( 'A', 'I' ) ) ) {
			$this->status = 'I';
		}
		if ( $is_ok ) {
			$source = Elite_Notification_Source::get_sources_id( $this->source_type );
			if ( $source instanceof Elite_Notification_Source_Base ) {
				return $source->set_from_post_data( $this );
			}
		} else {
			return false;
		}
	}
}
