<?php
namespace Simple_Giveaways;

/**
 * Class to change or add WordPress dashboard widgets
 */
class SG_Dashboard_Widget {

	const CACHE_TRANSIENT_KEY = 'sg-dashboard-totals';

	/**
	 * SG_Dashboard_Widget constructor.
	 */
	public function __construct() {

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_dashboard_assets' ) );
		add_action( 'admin_init', array( $this, 'queue_dashboard_widget' ) );
	}

	/**
	 * Adds the dashboard widget if it should be shown.
	 *
	 * @return void
	 */
	public function queue_dashboard_widget() {
		add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
	}

	/**
	 * Adds dashboard widget to WordPress
	 */
	public function add_dashboard_widget() {
		add_filter( 'postbox_classes_dashboard_sg-dashboard-overview', array( $this, 'dashboard_overview_class' ) );
		wp_add_dashboard_widget(
			'sg-dashboard-overview',
			/* translators: %s is the plugin name */
			__( 'Simple Giveaways Overview', 'giveasap' ),
			array( $this, 'display_dashboard_widget' )
		);
	}

	/**
	 * Adds CSS classes to the dashboard widget.
	 *
	 * @param array $classes An array of postbox CSS classes.
	 *
	 * @return array
	 */
	public function dashboard_overview_class( $classes ) {
		$classes[] = 'sg-dashboard-overview';
		return $classes;
	}

	/**
	 * Displays the dashboard widget.
	 */
	public function display_dashboard_widget() {

		$giveaways = $this->get_giveaways();
		$subscribers = $this->get_subscribers_count();

		$published = $ended = $winners = $notified = array();

		if ( $giveaways ) {
			foreach ( $giveaways as $giveaway ) {
				switch ( $giveaway['post_status'] ) {
					case 'giveasap_notified':
						$notified[] = $giveaway;
						$ended[] = $giveaway;
						$winners[] = $giveaway;
						break;
					case 'giveasap_winners':
						$winners[] = $giveaway;
						$ended[] = $giveaway;
						break;
					case 'giveasap_ended':
						$ended[] = $giveaway;
						break;
					case 'publish':
						$published[] = $giveaway;
						break;
				}
			}
		}

		include 'views/dashboard-widget.php';
	}

	/**
	 * Get total number of giveaways.
	 * Using WPDB for shorter and direct SQL.
	 */
	public function get_giveaways() {
		global $wpdb;

		$sql = "SELECT * FROM $wpdb->posts WHERE post_type='giveasap' ORDER BY post_date DESC";

		$results = $wpdb->get_results( $sql, ARRAY_A );

		return $results;
	}

	/**
	 * Get total number of giveaways.
	 * Using WPDB for shorter and direct SQL.
	 */
	public function get_subscribers_count() {
		global $wpdb;

		$sql = "SELECT COUNT(*) FROM $wpdb->giveasap_entries";

		$results = $wpdb->get_var( $sql );

		return $results ? $results : 0;
	}

	/**
	 * Enqueues assets for the dashboard if the current page is the dashboard.
	 */
	public function enqueue_dashboard_assets() {
		if ( ! $this->is_dashboard_screen() ) {
			return;
		}

		wp_enqueue_style( 'sg-admin', GASAP_URI . 'assets/dist/css/admin.css' );
	}

	/**
	 * Checks if the current screen is the dashboard screen.
	 *
	 * @return bool Whether or not this is the dashboard screen.
	 */
	private function is_dashboard_screen() {
		$current_screen = get_current_screen();

		return ( $current_screen instanceof \WP_Screen && $current_screen->id === 'dashboard' );
	}
}
