<?php

namespace Simple_Giveaways;

/**
 * Class SG_Reports
 */
class SG_Reports {

	/**
	 * SG_Reports constructor.
	 */
	public function __construct() {
		include_once untrailingslashit( GASAP_ROOT ) . '/includes/abstracts/class-sg-report.php';
		include_once 'class-sg-report-date.php';
		include_once 'class-sg-report-giveaways.php';
		include_once 'class-sg-report-actions.php';
		do_action( 'sg_reports_loaded' );
		// add_action( 'sg_reports_subscribers_sub_reports', array( $this, 'add_premium_reports' ) );
	}

	/**
	 * Add Premium Subscribers Reports
	 */
	public function add_premium_reports() {
		$reports      = $this->get_reports();
		$upgrade_link = giv_fs()->get_upgrade_url();

		if ( ! isset( $reports['subscribers']['reports']['referrals'] ) ) {
			?>
			<li class="premium">
				<span><?php esc_html_e( 'Top Referrals', 'giveasap' ); ?></span>
				<sup><small><a href="<?php echo esc_url( $upgrade_link ); ?>"><?php esc_html_e( 'Upgrade', 'giveasap' ); ?></a></small></sup>
			</li>
			<?php
		}
	}

	/**
	 * Get all reports
	 */
	public function get_reports() {
		$reports = array(
			'subscribers' => array(
				'title'   => __( 'Subscribers', 'giveasap' ),
				'reports' => array(
					'date'      => __( 'Subscribers', 'giveasap' ),
					'giveaways' => __( 'By Giveaways', 'giveasap' ),
					'actions'   => __( 'Actions', 'giveasap' ),
				),
			),
		);

		return apply_filters( 'ss_get_reports', $reports );
	}

	/**
	 * Render the Reports section
	 */
	public function render() {
		$reports = $this->get_reports();
		$report  = $this->get_report();
		include_once untrailingslashit( GASAP_ROOT ) . '/admin/views/reports/reports.php';
	}


	/**
	 * Get the Report object.
	 *
	 * @param string $type
	 *
	 * @return SG_Report_Date
	 */
	public function get_report( $type = '' ) {
		if ( ! $type ) {
			$type = isset( $_GET['report'] ) ? Helpers::unslash_and_clean( $_GET['report'] ) : 'date';
		}

		$class_name = $this->get_class_name( $type );

		if ( class_exists( $class_name ) ) {
			return new $class_name();
		}

		return new SG_Report_Date();
	}

	/**
	 * @param $name
	 *
	 * @return string
	 */
	public function get_class_name( $name ) {
		$name       = str_replace( '-', ' ', $name );
		$class_name = '\Simple_Giveaways\SG_Report_' . str_replace( ' ', '_', ucfirst( $name ) );
		return apply_filters( 'sg_report_class_name', $class_name, $name );
	}
}
