<?php

/**
 * Prices for Notifier Plugin for DVC
 *
 * @package    DVC Notifier
 * @author    Clark Nidkel Powell
 * @link    http://www.clarknikdelpowell.com
 * @version    1.0
 * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
 */

namespace DVCNotifier;

class Prices {
	private $Notifier;
	private $new_prices = array();
	private $old_prices = array();
	private $settings = array(
		'posts_table'    => '',
		'meta_table'     => '',
		'term_table'     => '',
		'term_tax_table' => ''
	);

	public function __construct( $settings, $Notifier ) {
		$this->settings   = array_merge( $this->settings, $settings );
		$this->Notifier   = $Notifier;
		$this->new_prices = $this->get_new_prices();
		$this->old_prices = $this->get_old_prices();
	}

	private function get_old_prices() {
		global $wpdb;

		$prices = array();

		$old_prices = $wpdb->get_results( "
			SELECT id, price FROM li_prices
		", ARRAY_A );

		foreach ( $old_prices as $old_price ) {
			$prices[ $old_price['id'] ] = $old_price['price'];
		}

		return $prices;
	}

	private function get_new_prices() {
		if (function_exists('\newrelic_start_segment')) {
			$segment = \newrelic_start_segment('DVCNotifier/Prices/get_new_prices_bulk');
		}
		
		global $wpdb;
		$posts = $this->settings['posts_table'];
		$meta = $this->settings['meta_table'];

		$query_start = microtime(true);
		// TEMPORARY: Use original method to ensure data accuracy
		$new_prices = $wpdb->get_results("
			SELECT ID 
			FROM $posts 
			WHERE $posts.post_type = 'listing'
				AND $posts.post_status = 'publish'
		", ARRAY_A);
		$query_duration = microtime(true) - $query_start;

		$prices = array();
		foreach ($new_prices as $new_price) {
			$meta = $this->Notifier->get_metas($new_price['ID']);
			$price = intval($this->Notifier->get_meta($meta, 'listing_points_price')) * intval($this->Notifier->get_meta($meta, 'listing_points_contract'));

			$price = array(
				'id' => $new_price['ID'],
				'price' => $price,
			);

			$prices[] = $price;
		}
		
		if (function_exists('\newrelic_add_custom_parameter')) {
			\newrelic_add_custom_parameter('prices_bulk_query_duration', $query_duration);
			\newrelic_add_custom_parameter('prices_listings_processed', count($prices));
		}
		
		if (isset($segment) && function_exists('\newrelic_end_segment')) {
			\newrelic_end_segment($segment);
		}

		return $prices;
	}

	public function sync_prices() {
		if (function_exists('\newrelic_start_segment')) {
			$segment = \newrelic_start_segment('DVCNotifier/Prices/sync_prices');
		}
		
		global $wpdb;
		$sync_start = microtime(true);

		$delete_start = microtime(true);
		$delete_result = $wpdb->query('DELETE FROM li_prices');
		$delete_duration = microtime(true) - $delete_start;

		$listing_values = array();
		foreach ($this->new_prices as $new_price) {
			$listing_values[] = '(' . intval($new_price['id']) . ',' . intval($new_price['price']) . ')';
		}
		
		$insert_start = microtime(true);
		$insert_result = $wpdb->query('INSERT INTO li_prices (id, price) VALUES ' . implode(',', $listing_values));
		$insert_duration = microtime(true) - $insert_start;
		$total_duration = microtime(true) - $sync_start;
		
		if (function_exists('\newrelic_add_custom_parameter')) {
			\newrelic_add_custom_parameter('price_sync_delete_duration', $delete_duration);
			\newrelic_add_custom_parameter('price_sync_insert_duration', $insert_duration);
			\newrelic_add_custom_parameter('price_sync_total_duration', $total_duration);
			\newrelic_add_custom_parameter('price_sync_records_deleted', $delete_result);
			\newrelic_add_custom_parameter('price_sync_records_inserted', $insert_result);
		}
		
		if (isset($segment) && function_exists('\newrelic_end_segment')) {
			\newrelic_end_segment($segment);
		}
	}

	public function get_reduced_listings() {
		if (function_exists('\newrelic_start_segment')) {
			$segment = \newrelic_start_segment('DVCNotifier/Prices/get_reduced_listings');
		}
		
		$processing_start = microtime(true);
		$reduced_listings = array();
		$comparisons_made = 0;
		
		foreach ($this->new_prices as $new_price) {
			if (key_exists($new_price['id'], $this->old_prices)) {
				$old_price = $this->old_prices[$new_price['id']];
				$price = $new_price['price'];
				$comparisons_made++;

				if ($old_price > $price) {
					$reduced_listings[] = $new_price['id'];
				}
			}
		}
		$processing_duration = microtime(true) - $processing_start;
		
		if (function_exists('\newrelic_add_custom_parameter')) {
			\newrelic_add_custom_parameter('price_comparisons_made', $comparisons_made);
			\newrelic_add_custom_parameter('reduced_listings_found', count($reduced_listings));
			\newrelic_add_custom_parameter('price_comparison_duration', $processing_duration);
		}
		
		if (isset($segment) && function_exists('\newrelic_end_segment')) {
			\newrelic_end_segment($segment);
		}

		return $reduced_listings;
	}
}
