<?php
/**
 * Mapbd Dummy Notification Model
 *
 * @package Elite_Notification_Lite\Models\Database
 */

namespace Elite_Notification_Lite\Models\Database;

use Cassandra\Date;
use Elite_Notification_Lite\Core\Elite_Notification_Lite_Model;
use Elite_Notification_Lite\Libs\Elite_Notification_Item;
use http\Url;
use Sabberworm\CSS\Property\Charset;

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

/**
 * Class Mapbd_Dummy_Notification
 *
 * @package Elite_Notification_Lite\Models\Database
 */
class Mapbd_Dummy_Notification extends Elite_Notification_Lite_Model {
	/**
	 * Its property id
	 *
	 * @var int
	 */
	public $id;
	/**
	 * Its property title
	 *
	 * @var string
	 */
	public $title;
	/**
	 * Its property img_id
	 *
	 * @var int
	 */
	public $img_id;
	/**
	 * Its property img_url
	 *
	 * @var Url
	 */
	public $img_url;
	/**
	 * Its property body
	 *
	 * @var int
	 */
	public $body;
	/**
	 * Its property type
	 *
	 * @var Charset
	 */
	public $type;
	/**
	 * Its property status
	 *
	 * @var Charset
	 */
	public $status;
	/**
	 * Its property view_count
	 *
	 * @var int
	 */
	public $view_count;
	/**
	 * Its property click_count
	 *
	 * @var int
	 */
	public $click_count;
	/**
	 * Its property nt_by
	 *
	 * @var string
	 */
	public $nt_by;
	/**
	 * Its property op_date
	 *
	 * @var Date
	 */
	public $op_date;
	/**
	 * Its property link_url
	 *
	 * @var Url
	 */
	public $link_url;
	/**
	 * Its property slnf_style
	 *
	 * @var string
	 */
	public $slnf_style;

	/**
	 * Mapbd_Dummy_Notification constructor.
	 */
	public function __construct() {
		parent::__construct();
		$this->set_validation();
		$this->table_name     = 'apbd_dummy_notification';
		$this->primary_key    = 'id';
		$this->unique_key     = array();
		$this->multi_key      = array();
		$this->auto_inc_field = array( 'id' );
		$this->app_base_name  = 'elite-notification';
	}

	/**
	 * The set validation is generated by appsbd
	 */
	public function set_validation() {
		$this->validations = array(
			'id'          => array(
				'Text' => 'Id',
				'Rule' => 'max_length[10]|integer',
			),
			'img_id'      => array(
				'Text' => 'Img Id',
				'Rule' => 'max_length[11]integer',
			),
			'img_url'      => array(
				'Text' => 'Img Url',
				'Rule' => 'max_length[255]',
			),
			'body'        => array(
				'Text' => 'Body',
				'Rule' => '',
			),
			'type'        => array(
				'Text' => 'Type',
				'Rule' => 'required|max_length[1]',
			),
			'status'      => array(
				'Text' => 'Status',
				'Rule' => 'max_length[1]',
			),
			'nt_by'       => array(
				'Text' => 'Notification footer',
				'Rule' => 'max_length[50]',
			),
			'view_count'  => array(
				'Text' => 'View',
				'Rule' => 'max_length[11]|integer',
			),
			'click_count' => array(
				'Text' => 'Click',
				'Rule' => 'max_length[11]|integer',
			),
			'title'       => array(
				'Text' => 'Title',
				'Rule' => 'max_length[255]',
			),
			'op_date'     => array(
				'Text' => 'Order',
				'Rule' => 'max_length[20]',
			),
			'link_url'    => array(
				'Text' => 'Link',
				'Rule' => 'max_length[100]',
			),
			'slnf_style'  => array(
				'Text' => 'Style',
				'Rule' => 'max_length[100]',
			),
		);
	}

	/**
	 * The get notification item is generated by appsbd
	 *
	 * @param mixed $this_obj It is this_obj param.
	 *
	 * @return Elite_Notification_Item
	 */
	public static function get_notification_item( $this_obj ) {
		$item = new Elite_Notification_Item();
		$item->title = $this_obj->title;
		$item->img = $this_obj->img_url;
		$item->des = '<div class="apbd-dtls-title apbd-el-first">' . $this_obj->title . '</div><div class="apbd-dtls-text" >' . $this_obj->body . '</div>';
		$item->link_url = $this_obj->link_url;
		$item->view_style = $this_obj->slnf_style;
		$item->typ = $this_obj->get_text_by_key( 'type', false );
		$item->nt_by = '<small>' . $this_obj->nt_by . '</small>';
		$item->op_timestamp = strtotime( $this_obj->op_date );
		$item->op_date = gmdate( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $item->op_timestamp );
		$item->source_id = 'D' . $this_obj->id;
		return $item;
	}
	/**
	 * The increase view is generated by appsbd
	 *
	 * @param mixed $id It is id param.
	 *
	 * @return bool
	 */
	public static function increase_view( $id ) {
		$id = (int) filter_var( $id, FILTER_SANITIZE_NUMBER_INT );
		if ( $id > 0 ) {
			$obj = new self();
			$obj->view_count( 'view_count + 1', true );
			$obj->set_where_update( 'id', $id );
			return $obj->update();
		} else {
			return false;
		}
	}

	/**
	 * The increase click is generated by appsbd
	 *
	 * @param mixed $id It is id param.
	 *
	 * @return bool
	 */
	public static function increase_click( $id ) {
		$id = (int) filter_var( $id, FILTER_SANITIZE_NUMBER_INT );
		if ( $id > 0 ) {
			$obj = new self();
			$obj->click_count( 'click_count + 1', true );
			$obj->set_where_update( 'id', $id );
			return $obj->update();
		} else {
			return false;
		}
	}

	/**
	 * The create db table is generated by appsbd
	 */
	public static function create_db_table() {
		$this_obj = new static();
		$table    = $this_obj->db->prefix . $this_obj->table_name;
		if ( $this_obj->db->get_var( "show tables like '{$table}'" ) != $table ) {
			$sql = "CREATE TABLE `{$table}`(
                `id` int(11) NOT NULL AUTO_INCREMENT,
                `title` char(255) NOT NULL DEFAULT '',
                `img_id` int(11) NOT NULL DEFAULT '0',
                `img_url` char(255) NOT NULL DEFAULT '',
                `body` char(140) CHARACTER SET utf8 NOT NULL DEFAULT '',
                `type` char(1) NOT NULL DEFAULT '' COMMENT 'radio(S=Sales,C=Comment,O=Other)',
                `status` char(1) NOT NULL DEFAULT 'I' COMMENT 'bool(A=Active,I=Inactive)',
                `view_count` int(11) unsigned NOT NULL DEFAULT 0,
                `click_count` int(11) unsigned NOT NULL DEFAULT 0,
                `nt_by` char(50) NOT NULL DEFAULT '',
                `op_date` timestamp NOT NULL DEFAULT current_timestamp(),
                `link_url` char(150) NOT NULL DEFAULT '',
                `slnf_style` char(100) NOT NULL DEFAULT '',
                PRIMARY KEY (`id`) USING BTREE
            )";
			require_once ABSPATH . 'wp-admin/includes/upgrade.php';
			dbDelta( $sql );
		}
	}

	/**
	 * The delete by id is generated by appsbd
	 *
	 * @param mixed $id It is id param.
	 *
	 * @return bool
	 */
	public static function delete_by_id( $id ) {
		return self::delete_by_key_value( 'id', $id );
	}
}
