<?php

	// Exit if accessed directly
	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	class WS_Form_API_Form_Stat extends WS_Form_API {

		public function __construct() {

			// Call parent on WS_Form_API
			parent::__construct();
		}

		// API - Get chart data - Time based
		public function api_get_chart_data($parameters) {

			$ws_form_form_stat = new WS_Form_Form_Stat();
			$ws_form_form_stat->date_ranges_init();

			$current_user_id = get_current_user_id();

			// Get form ID
			$form_id = absint(WS_Form_Common::get_query_var_nonce('form_id', '', $parameters));

			// Save form ID
			WS_Form_Common::option_set('wsf_dashboard_widget_form_id_' . $current_user_id, $form_id);

			// Date range
			$date_range = WS_Form_Common::get_query_var_nonce('date_range', '', $parameters);
			if(!isset($ws_form_form_stat->date_ranges[$date_range])) { self::api_throw_error(esc_html__('Invalid date range', 'ws-form')); }

			// Save date range
			WS_Form_Common::option_set('wsf_dashboard_widget_date_range_' . $current_user_id, $date_range);

			// Build time from and to
			$time_from_utc = ($ws_form_form_stat->date_ranges[$date_range]['time_from'] !== false) ? $ws_form_form_stat->get_utc_time_from($ws_form_form_stat->date_ranges[$date_range]['time_from']) : false;
			$time_to_utc = ($ws_form_form_stat->date_ranges[$date_range]['time_to'] !== false) ? $ws_form_form_stat->get_utc_time_to($ws_form_form_stat->date_ranges[$date_range]['time_to']) : false;

			// Get data
			$ws_form_form_stat->form_id = $form_id;

			// Get data type
			$data_type = $ws_form_form_stat->date_ranges[$date_range]['data'];
			switch($data_type) {

				case 'time' :

					$chart_data = $ws_form_form_stat->db_get_chart_data_time($time_from_utc, $time_to_utc);
					break;

				case 'total' :

					$chart_data = $ws_form_form_stat->db_get_chart_data_total($time_from_utc, $time_to_utc);
					break;
			}

			// Get form data
			if($form_id == 0) {

				$title = __('All Forms', 'ws-form');

			} else {

				$ws_form_form = new WS_Form_Form();
				$ws_form_form->id = $form_id;
				$form_object = $ws_form_form->db_read(false);

				$title = $form_object->label;
			}

			// Add label to title
			$title .= ' - ' . $chart_type = $ws_form_form_stat->date_ranges[$date_range]['label'];

			$chart_type = $ws_form_form_stat->date_ranges[$date_range]['type'];

			// Return data
			if($chart_data === false) {

				WS_Form_Common::echo_wp_json_encode(array('error' => true));

			} else {

				$config = array(

					'type'	=> $chart_type,

					'data'	=> $chart_data,

					'options' => array(

						'responsive' => true,

						'title' => array(

							'display' => true,
							'text' => $title
						),

						'scales' => array(

							'xAxes' => array(

								array(

									'display' 		=> true,
									'scaleLabel' 	=> ($chart_type == 'line') ? array(

										'display' 		=> true,
										'labelString' 	=> __('Date', 'ws-form')
									) : false
								)
							),

							'yAxes' => array(

								array(

									'display' 		=> true,
									'scaleLabel' 	=> array(

										'display' 		=> true,
										'labelString' 	=> __('Count', 'ws-form')
									),
									'ticks' => array(

										'beginAtZero'	=> true
									)
								)
							)
						)
					)
				);

				if($chart_type == 'line') {

					$config['options']['tooltips'] = array(

						'mode' => 'index',
						'intersect' => false,
					);

					$config['options']['hover'] = array(

						'mode' => 'nearest',
						'intersect' =>true
					);
				}

				WS_Form_Common::echo_wp_json_encode(array('error' => false, 'config' => $config));
			}

			exit;
		}

		// API - Get chart data - Time based
		public function api_get_chart_data_total($parameters) {

			$ws_form_form_stat = new WS_Form_Form_Stat();
			$ws_form_form_stat->date_ranges_init();

			$current_user_id = get_current_user_id();

			// User capability check
			WS_Form_Common::user_must('manage_options_wsform');

			// Get form ID
			$form_id = absint(WS_Form_Common::get_query_var_nonce('form_id', '', $parameters));

			// Save form ID
			WS_Form_Common::option_set('wsf_dashboard_widget_form_id_' . $current_user_id, $form_id);

			// Date range
			$date_range = WS_Form_Common::get_query_var_nonce('date_range', '', $parameters);
			if(!isset($ws_form_form_stat->date_ranges[$date_range])) { self::api_throw_error(esc_html__('Invalid date range', 'ws-form')); }

			// Save date range
			WS_Form_Common::option_set('wsf_dashboard_widget_date_range_' . $current_user_id, $date_range);

			// Build time from and to
			$time_from_utc = ($ws_form_form_stat->date_ranges[$date_range]['time_from'] !== false) ? $ws_form_form_stat->get_utc_time_from($ws_form_form_stat->date_ranges[$date_range]['time_from']) : false;
			$time_to_utc = ($ws_form_form_stat->date_ranges[$date_range]['time_to'] !== false) ? $ws_form_form_stat->get_utc_time_to($ws_form_form_stat->date_ranges[$date_range]['time_to']) : false;

			// Get type
			$type = WS_Form_Common::get_query_var_nonce('type', 'line', $parameters);
			if(!in_array($type, array('line'))) { self::api_throw_error(esc_html__('Invalid chart type', 'ws-form')); }

			// Get data
			$ws_form_form_stat->form_id = $form_id;
			$chart_data = $ws_form_form_stat->db_get_chart_data_time($time_from_utc, $time_to_utc);

			// Get form data
			if($form_id == 0) {

				$title = __('All Forms', 'ws-form');

			} else {

				$ws_form_form = new WS_Form_Form();
				$ws_form_form->id = $form_id;
				$form_object = $ws_form_form->db_read(false);

				$title = $form_object->label;
			}

			// Return data
			if($chart_data === false) {

				WS_Form_Common::echo_wp_json_encode(array('error' => true));

			} else {

				$config = array(

					'type'	=> $type,

					'data'	=> $chart_data,

					'options' => array(

						'responsive' => true,

						'title' => array(
							'display' => true,
							'text' => $title
						),

						'tooltips' => array(
							'mode' => 'index',
							'intersect' => false,
						),

						'hover' => array(
							'mode' => 'nearest',
							'intersect' =>true
						),

						'scales' => array(

							'xAxes' => array(

								array(

									'display' 		=> true,
									'scaleLabel' 	=> array(

										'display' 		=> true,
										'labelString' 	=> __('Date', 'ws-form')
									)
								)
							),

							'yAxes' => array(

								array(

									'display' 		=> true,
									'scaleLabel' 	=> array(

										'display' 		=> true,
										'labelString' 	=> __('Count', 'ws-form')
									)
								)
							)
						)
					)
				);

				WS_Form_Common::echo_wp_json_encode(array('error' => false, 'config' => $config));
			}

			exit;
		}

		// API - Add view
		public function api_add_view($parameters) {

			// Get form ID
			$form_id = absint(WS_Form_Common::get_query_var_nonce('wsffid', '', $parameters));

			// Check form ID
			if($form_id === 0) { exit; }

			try {

				// Log view
				$ws_form_form_stat = new WS_Form_Form_Stat();
				$ws_form_form_stat->form_id = $form_id;
				$ws_form_form_stat->db_add_view();

			} catch (Exception $e) {

				// Exit with error to avoid PHP error messages
				exit;
			}

			// X-Robots-Tag header
			header('X-Robots-Tag: noindex, nofollow');

			return array('error' => false);
		}

		// API - Reset statistics for a form
		public function api_reset($parameters) {

			// Get form ID
			$form_id = absint(WS_Form_Common::get_query_var_nonce('form_id', '', $parameters));

			// Reset form stats
			$ws_form_form_stat = new WS_Form_Form_Stat();
			$ws_form_form_stat->form_id = $form_id;
			$ws_form_form_stat->db_delete();

			// Reset form
			$ws_form_form = new WS_Form_Form();
			$ws_form_form->id = $form_id;
			$ws_form_form->db_count_reset();

			return array('error' => false);
		}

		// API - Send form statistics report
		public function api_report_form_statistics_send($parameters) {

			$ws_form_form_stat = new WS_Form_Form_Stat();
			
			try {

				$ws_form_form_stat->report_form_statistics_send();

			} catch (Exception $e) {

				parent::api_throw_error($e->getMessage());
			}

			return array('error' => false);
		}
	}
