<?php
/**
 * Elite Notification WordPress Download Stats Source
 *
 * @package Elite_Notification_Lite\Source
 */

namespace Elite_Notification_Lite\Source;

use Appsbd_Lite\V4\libs\AppInput;
use Elite_Notification_Lite\Api\Sanl_Wordpress_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_Wordpress_Download_Stats
 *
 * @package Elite_Notification_Lite\Source
 */
class Ens_Wordpress_Download_Stats extends Elite_Notification_Source_Base {
	/**
	 * Its property id
	 *
	 * @var string
	 */
	public $id = 'DOWNLOAD-STATS';
	/**
	 * Its property source_name
	 *
	 * @var string
	 */
	public $source_name = 'Wordpress.org';

	/**
	 * The on initialize is generated by appsbd
	 */
	public function on_initialize() {
		add_action(
			'save_post',
			function ( $post_id, $post ) {
				Elite_Notification_Source::set_load_queue();
			},
			10,
			2
		);
		$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_textbox(
				'wp_down_stats_temp',
				$this->__( 'Wordpress Download Template' ),
				'has been downloaded [Total Download] times in [last 7 days] and total active user [Total Active]',
				'',
				'Y',
				$this->__( 'Wordpress Download Template' ),
				'',
				'<span>' . $this->__( 'Use,' ) . ' <strong>[Total Download]</strong> ' . $this->__( 'is for how many times your product downloaded,' ) . ' <strong>[Download Today]</strong> ' . $this->__( 'is for how many product downloaded today,' ) . ' <strong>[Download Yesterday]</strong> ' . $this->__( 'is for how many product downloaded yesterday,' ) . ' <strong>[last 7 days]</strong> ' . $this->__( 'is for last how many days your product downloaded,' ) . ' <strong>[last week]</strong> ' . $this->__( 'is for last week how many of your product downloaded,' ) . ' <strong>[Total Active]</strong> ' . $this->__( 'is for how many active user downloaded' ) . '</span>',
				'',
				'col-12',
				'',
				'',
				''
			),
			Elite_Notification_Custom_Field::get_textbox( 'wp_down_stats_slug', $this->__( 'Slug' ), '', '', 'Y', $this->__( 'Product Slug' ), '', '', '', 'col-12 col-sm-12 col-md-6', '', '', '' ),
			Elite_Notification_Custom_Field::get_radio(
				'wp_down_stats_slug_type',
				$this->__( 'Slug Type' ),
				'P',
				'',
				array(
					array(
						'val'   => 'T',
						'title' => $this->__( 'Theme' ),
					),
					array(
						'val'   => 'P',
						'title' => $this->__( 'Plugin' ),
					),
				),
				'',
				'',
				'',
				'col-6 col-sm-6 col-md-3',
				''
			),
			Elite_Notification_Custom_Field::get_radio(
				'wp_down_stats_link_type',
				$this->__( 'Link Type' ),
				'P',
				'',
				array(
					array(
						'val'   => 'P',
						'title' => $this->__( 'Product Page' ),
					),
					array(
						'val'   => 'N',
						'title' => $this->__( 'None' ),
					),
				),
				'',
				'',
				'',
				'col-6 col-sm-6 col-md-3',
				''
			),
		);

		return $cf;
	}

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

	/**
	 * 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 ) {
		$down_stats_slug       = $source_item->get_param_option( 'wp_down_stats_slug' );
		$down_stats_slug_type  = $source_item->get_param_option( 'wp_down_stats_slug_type' );
		$down_stats_link_type  = $source_item->get_param_option( 'wp_down_stats_link_type' );
		$wp_down_stat_template = $source_item->get_param_option( 'wp_down_stats_temp' );
		$slnf_style            = $source_item->slnf_style;
		$plugin_inf            = Sanl_Wordpress_Api::get_full_information( $down_stats_slug, $down_stats_slug_type );
		$response_array        = array();
		$current_timestamp     = current_time( 'timestamp' );
		$item                  = new Elite_Notification_Item();
		$item->id              = hash( 'crc32b', wp_json_encode( $plugin_inf ) );
		$item->source_id       = $source_item->id;
		$item->img             = $plugin_inf->thumb_img;
		$item->des             = '<div class=" apbd-el-first apbd-dtls-text apbd-mb-2 apbd-with-nowrap">' . apbd_slnf_short_name(
			$plugin_inf->name,
			31
		) . '</div><div class="apbd-dtls-title apbd-with-wrap" >' . $this->get_template_msg(
			$wp_down_stat_template,
			$plugin_inf
		) . '</div>';
		$item->link_url        = 'P' == $down_stats_link_type ? $plugin_inf->link_url : '#';
		$item->view_style      = $slnf_style;
		$item->typ             = $this->__( 'Download Status' );
		$item->nt_by           = '';
		$item->op_timestamp    = $current_timestamp;
		$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 $data It is data param.
	 *
	 * @return string|string[]
	 */
	public function get_template_msg( $template, $data ) {
		$params = array(
			'Product'            => $data->name,
			'Download Today'     => apbd_convert_number_to_k( $data->download_today, true ),
			'Total Download'     => apbd_convert_number_to_k( $data->download_all_time, true ),
			'Download Yesterday' => apbd_convert_number_to_k( $data->download_yesterday, true ),
			'last 7 days'        => apbd_convert_number_to_k( $data->download_in_7days, true ),
			'last week'          => apbd_convert_number_to_k( $data->download_last_week, true ),
			'Total Active'       => apbd_convert_number_to_k( $data->active_installs, true ),
		);
		foreach ( $params 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_wp_org.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(
			'wp_down_stats_temp'      => AppInput::post_value( 'wp_down_stats_temp' ),
			'wp_down_stats_slug'      => AppInput::post_value( 'wp_down_stats_slug' ),
			'wp_down_stats_slug_type' => AppInput::post_value( 'wp_down_stats_slug_type' ),
			'wp_down_stats_link_type' => AppInput::post_value( 'wp_down_stats_link_type' ),
		);
		$is_ok  = true;
		if ( empty( $params['wp_down_stats_temp'] ) ) {
			Elite_Notification_Lite::add_error( 'WordPress Download Stats template has been required' );
			$is_ok = false;
		}
		if ( empty( $params['wp_down_stats_slug'] ) ) {
			Elite_Notification_Lite::add_error( 'Slug has been required' );
			$is_ok = false;
		}
		if ( empty( $params['wp_down_stats_slug_type'] ) ) {
			Elite_Notification_Lite::add_error( 'Slug type has been required' );
			$is_ok = false;
		}
		if ( empty( $params['wp_down_stats_link_type'] ) ) {
			Elite_Notification_Lite::add_error( 'Link type has been required' );
			$is_ok = false;
		}
		$get_basic_info = Sanl_Wordpress_Api::get_plugin_basic(
			$params['wp_down_stats_slug'],
			$params['wp_down_stats_slug_type']
		);
		if ( empty( $get_basic_info->slug ) ) {
			Elite_Notification_Lite::add_error( "Slug doesn't exists in the wordpress.org" );
			$is_ok = false;
		}
		if ( ! $is_ok ) {
			return false;
		}

		return $item_object->set_source_param( $params );
	}
}
