<?php
/**
 * Snal WordPress Api
 *
 * @package Elite_Notification_Lite\Api;
 */

namespace Elite_Notification_Lite\Api;

use Elite_Notification_Lite\Libs\Elite_Wp_Plugin_Stats;

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

/**
 * Class Sanl_Wordpress_Api
 *
 * @package Elite_Notification_Lite\Api
 */
class Sanl_Wordpress_Api {
	/**
	 * Sanl_Wordpress_Api constructor.
	 */
	public function __construct() {
	}

	/**
	 * The get plugin basic is generated by appsbd
	 *
	 * @param mixed  $slug It is slug param.
	 * @param string $type It is type param.
	 *
	 * @return Elite_Wp_Plugin_Stats
	 */
	public static function get_plugin_basic( $slug, $type = 'P' ) {
		$type    = strtoupper( $type );
		$payload = array(
			'action'  => 'P' == $type ? 'plugin_information' : 'theme_information',
			'request' => serialize(
				(object) array(
					'slug'   => strtolower( $slug ),
					'fields' => array(
						'description'     => false,
						'active_installs' => true,
						'downloaded'      => true,
						'sections'        => false,
						'versions'        => false,
						'tags'            => false,
						'ratings'         => false,
					),
				)
			),
		);

		$wp_info  = new Elite_Wp_Plugin_Stats();
		$type_str = 'P' == $type ? 'plugins' : 'themes';
		$body     = wp_remote_post( 'http://api.wordpress.org/' . $type_str . '/info/1.0/', array( 'body' => $payload ) );
		if ( ! empty( $body['response']['code'] ) && '200' == $body['response']['code'] ) {
			$d = unserialize( $body['body'] );
			if ( ! empty( $d->slug ) ) {
				$wp_info->name            = $d->name;
				$wp_info->slug            = $d->slug;
				$wp_info->thumb_img       = ! empty( $d->screenshot_url ) ? $d->screenshot_url : '//ps.w.org/' . $d->slug . '/assets/icon-256x256.png?t=' . time();
				$wp_info->link_url        = 'P' == $type ? 'https://wordpress.org/plugins/' . $slug . '/' : 'https://wordpress.org/themes/' . $slug . '/';
				$wp_info->version         = $d->version;
				$wp_info->rating          = $d->rating;
				$wp_info->num_ratings     = $d->num_ratings;
				$wp_info->active_installs = $d->active_installs;
				$wp_info->downloaded      = $d->downloaded;
				$wp_info->added           = ! empty( $d->added ) ? $d->added : '';
			}
		}

		return $wp_info;
	}

	/**
	 * The get full information is generated by appsbd
	 *
	 * @param mixed  $slug It is slug param.
	 * @param string $type It is type param.
	 *
	 * @return Elite_Wp_Plugin_Stats
	 */
	public static function get_full_information( $slug, $type = 'P' ) {
		$type     = strtoupper( $type );
		$type_str = 'P' == $type ? 'plugin' : 'themes';
		$wp_info  = self::get_plugin_basic( $slug, $type );

		if ( ! empty( $wp_info->slug ) ) {
			$body2 = wp_remote_get( 'https://api.wordpress.org/stats/' . $type_str . '/1.0/downloads.php?slug=' . $slug . '&historical_summary=1' );
			if ( ! empty( $body2['response']['code'] ) && '200' == $body2['response']['code'] ) {
				$download_stats = json_decode( $body2['body'] );
				if ( isset( $download_stats->today ) ) {
					$wp_info->download_today     = $download_stats->today;
					$wp_info->download_yesterday = $download_stats->yesterday;
					$wp_info->download_last_week = $download_stats->last_week;
					$wp_info->download_all_time  = $download_stats->all_time;
					$body3                       = wp_remote_get( 'https://api.wordpress.org/stats/' . $type_str . '/1.0/downloads.php?slug=' . $slug . '&limit=7' );
					if ( ! empty( $body3['response']['code'] ) && '200' == $body3['response']['code'] ) {
						$download_in_7days = json_decode( $body3['body'] );
						$total7days        = $wp_info->download_today;
						foreach ( $download_in_7days as $key => $download_in_7_day ) {
							$total7days += $download_in_7_day;
						}
						$wp_info->download_in_7days = $total7days;
					}
				}
			}
		}
		return $wp_info;
	}
}
