<?php

/**
 * Class SG_Report_Actions
 */
class SG_Report_Actions extends SG_Report {

	protected $giveaways = null;

	/**
	 * SG_Report_Date constructor.
	 */
	public function __construct() {
		$this->title = __( 'Extra Actions', 'giveasap' );
		$this->type  = 'line';
		$this->show_legend = true;
	}

	/**
	 * Report Options.
	 * @return string
	 */
	public function get_report_options() {
		$time = current_time( 'timestamp' );
		$to   = date( 'Y-m-d', $time );
		$from = date( 'Y-m-d', $time - 1 * YEAR_IN_SECONDS );
		$selected_giveaways = array();
		if ( isset( $_REQUEST['sg_reports_filter'] ) ) {
			$get_report = $_REQUEST['sg_report'];
			$to         = $get_report['to'];
			$from       = $get_report['from'];
			$selected_giveaways = isset( $get_report['giveaways'] ) ? $get_report['giveaways'] : array();
		}


		$giveaways = $this->get_giveaways();

		ob_start();
		?>
		<div class="sg-report-options">
			<form action="<?php echo admin_url('edit.php'); ?>" method="GET">
				<input type="hidden" name="post_type" value="giveasap" />
				<input type="hidden" name="page" value="sg_reports" />
				<input type="hidden" name="section" value="subscribers" />
				<input type="hidden" name="report" value="actions" />
				<div class="sg-report-filter">
					<label for="sg_report_from"><?php esc_html_e( 'From', 'giveasap' ); ?></label>
					<input type="text" id="sg_report_from" name="sg_report[from]" value="<?php echo $from; ?>" />
				</div>
				<div class="sg-report-filter">
					<label for="sg_report_to"><?php esc_html_e( 'To', 'giveasap' ); ?></label>
					<input type="text" id="sg_report_to" name="sg_report[to]" value="<?php echo $to; ?>" />
				</div>
				<div class="sg-report-filter">
					<label for="sg_report_giveaways" class="static"><?php esc_html_e( 'Giveaways', 'giveasap' ); ?></label>
					<select multiple id="sg_report_giveaways" name="sg_report[giveaways][]" class="sg-select2">
						<?php
						foreach ( $giveaways as $giveaway ) {
							$selected = in_array( $giveaway->ID, $selected_giveaways );
							echo '<option ' . selected( true, $selected, false ) . 'value="' . $giveaway->ID . '">' . $giveaway->post_title . '</option>';
						}
						?>
					</select>
				</div>
				<button type="submit" class="sg-button" name="sg_reports_filter"><?php esc_html_e( 'Filter', 'giveasap' ); ?></button>
			</form>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * Get labels for JavaScript.
	 *
	 * @return string
	 */
	public function get_labels() {
		$actions   = $this->get_data();
		$data      = array();
		foreach ( $actions as $action_type => $dates ) {
			foreach ( $dates as $date => $value ) {
				if ( in_array( $date, $data ) ) {
					continue;
				}
				$data[] = $date;
			}
		}
		return "'" . implode( "','", $data ) . "'";
	}

	public function get_giveaways() {
		if ( null === $this->giveaways ) {
			$this->giveaways = sg_get_giveaways( array(
				'post_status' => array( 'publish', 'giveasap_ended', 'giveasap_winners', 'giveasap_notified' )
			) );
		}

		return $this->giveaways;
	}

	/**
	 * Get data sets.
	 *
	 * @return array
	 */
	public function get_data_sets() {
		$data      = $this->get_data();
		$data_sets = array();

		$index = 0;
		foreach ( $data as $action_type => $data_row ) {
			$r = 164 + $index * 10;
			$g = 100 + $index * 10;
			$b = 151 + $index * 10;
			$set = array(
				'label' => $action_type,
				'backgroundColor' => 'rgba(' . $r . ',' . $g . ',' . $b . ',0.75)',
				'data' => array_values( $data_row ),
				'yAxisID' => ''
			);
			$data_sets[] = $set;
			$index++;
		}


		return $data_sets;
	}

	/**
	 * Get Data
	 */
	public function get_data() {
		global $wpdb;

		if ( null === $this->data ) {
			$time = current_time( 'timestamp' );
			$to   = date( 'Y-m-d', $time );
			$from = date( 'Y-m-d', $time - 1 * YEAR_IN_SECONDS );
			$selected_giveaways = array();

			if ( isset( $_REQUEST['sg_reports_filter'] ) ) {
				$get_report = $_REQUEST['sg_report'];
				$to         = $get_report['to'];
				$from       = $get_report['from'];

				$selected_giveaways = isset( $get_report['giveaways'] ) ? $get_report['giveaways'] : array();
			}
			$results     = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->giveasap_actions WHERE datetime >=%s and datetime <=%s ", $from, $to ), ARRAY_A );
			$data        = array();
			$found_dates = array();

			foreach ( $results as $result ) {
				$date          = date( 'Y-m-d', strtotime( $result['datetime'] ) );
				$giveaway_id   = $result['giveaway_id'];
				$action_type   = $result['action'];
                $action_object = sg_get_action_object( $action_type );
                $action        = $action_object->get_title();

				if ( $selected_giveaways && ! in_array( $giveaway_id, $selected_giveaways ) ) {
					continue;
				}

				if ( ! isset( $data[ $action ] ) ) {
					$data[ $action ] = array();
				}

				if ( ! isset( $data[ $action ][ $date ] ) ) {
					$data[ $action ][ $date ] = 0;
				}

				$found_dates[ $date ] = 0;

				$data[ $action ][ $date ] += 1;
			}

			$actions = array_keys( $data );
			foreach ( $actions as $action_type ) {
				foreach ( $found_dates as $date => $value ) {
					if ( ! isset( $data[ $action_type ][ $date ] ) ) {
						$data[ $action_type ][ $date ] = $value;
					}
				}
			}
			$this->set_data( $data );
		}

		return $this->data;
	}

}