<?php

/**
 * Class SG_Report_Giveaways
 */
class SG_Report_Giveaways extends SG_Report {

    protected $giveaways = null;

	/**
	 * SG_Report_Date constructor.
	 */
	public function __construct() {
		$this->title = __( 'Subscribers by Giveaways', '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="giveaways" />
				<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() {
		$giveaways = $this->get_data();
		$data      = array();
        foreach ( $giveaways as $giveaway_id => $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();

		$giveaways = $this->get_giveaways();

		foreach ( $giveaways as $index => $giveaway ) {
			if ( isset( $data[ $giveaway->ID ] ) ) {
				$r = 164 + $index * 10;
				$g = 100 + $index * 10;
				$b = 151 + $index * 10;
				$set = array(
					'label' => get_the_title( $giveaway ),
					'backgroundColor' => 'rgba(' . $r . ',' . $g . ',' . $b . ',0.75)',
					'data' => array_values( $data[ $giveaway->ID ] ),
                    'yAxisID' => ''
				);
				$data_sets[] = $set;
			}
		}


		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_entries WHERE date >=%s and date <=%s ", $from, $to ), ARRAY_A );
			$data        = array();
			$found_dates = array();

			foreach ( $results as $result ) {
				$date = date( 'Y-m-d', strtotime( $result['date'] ) );
				$giveaway_id = $result['post_id'];

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

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

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

				$found_dates[ $date ] = 0;

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

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

		return $this->data;
	}

}