<?php

namespace CarDealerPress\Includes\Functions;

use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
class Extender_Ajax {
	/**
	 * @var false|mixed|void
	 */
	private $admin_options;
	/**
	 * @var false|mixed|void
	 */
	private $theme_options;
	private $passed_parameters;

	public function __construct() {
		$this->theme_options = Option::get_cdp_theme_options();
		$this->admin_options = Option::get_cdp_admin_options();
	}

	/* AJAX Handler */
	public function extender_ajax_handler() {
		$data_request = array();
		$nonce_check = isset( $_REQUEST['nonce'] ) && wp_verify_nonce( sanitize_key($_REQUEST['nonce']), 'cdp_nonce' );
		if ($nonce_check ){
			$data_request = $_REQUEST;
		}
		if ( isset( $data_request['parameters'] ) ) {
			$extender_functions      = new Extender_Functions();
			$this->passed_parameters = $data_request['parameters'];
			$extender_functions->load_parameters( $this->passed_parameters );
			$extender_functions->load_request_data( $data_request );
		}
		$handler      = $data_request['handler'] ?? '';
		$request_data = $data_request['data'] ?? array();
		switch ( $handler ) {
			case 'replace':
				if ( isset( $request_data['mpg'] ) ) {
					if ( 'city' === $request_data['mpg'] ) {
						if ( isset( $this->passed_parameters['highway_to'] ) ) {
							$value = $this->passed_parameters['highway_to'];
							unset( $this->passed_parameters['highway_to'] );
							$this->passed_parameters['city_to'] = $value;
						}
						if ( isset( $this->passed_parameters['highway_from'] ) ) {
							$value = $this->passed_parameters['highway_from'];
							unset( $this->passed_parameters['highway_from'] );
							$this->passed_parameters['city_from'] = $value;
						}
					} else {
						if ( isset( $this->passed_parameters['city_to'] ) ) {
							$value = $this->passed_parameters['city_to'];
							unset( $this->passed_parameters['city_to'] );
							$this->passed_parameters['highway_to'] = $value;
						}
						if ( isset( $this->passed_parameters['city_from'] ) ) {
							$value = $this->passed_parameters['city_from'];
							unset( $this->passed_parameters['city_from'] );
							$this->passed_parameters['highway_from'] = $value;
						}
					}
				}
				unset( $this->passed_parameters['page'] );
				unset( $request_data['mpg'] );
				$extender_functions = new Extender_Functions();
				$extender_functions->extender_construct_parameter_update();
				$output['content']     = $extender_functions->extender_populate_content();
				$output['url']         = $extender_functions->extender_construct_url();
				$output['parameters']  = $this->passed_parameters;
				$output['ajax_script'] = ( isset( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ? urldecode( stripslashes( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ) : '' );
				$output['key']         = 'update';
				break;
			case 'update':
				unset( $this->passed_parameters['page'] );
				$extender_functions = new Extender_Functions();
				$output['parameters']  = $extender_functions->extender_construct_parameter_update();
				$output['content']     = $extender_functions->extender_populate_content();
				$output['url']         = $extender_functions->extender_construct_url();
				$output['ajax_script'] = ( isset( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ? urldecode( stripslashes( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ) : '' );
				$output['key']         = 'update';
				break;
			case 'clear_search':
				foreach ( $this->passed_parameters as $key => $value ) {
					if ( ! in_array( $key, array( 'saleclass', 'per_page' ) ) ) {
						unset( $this->passed_parameters[ $key ] );
					}
				}
				$extender_functions->extender_construct_parameter_update();
				$output['parameters']  = $this->passed_parameters;
				$output['content']     = $extender_functions->extender_populate_content();
				$output['url']         = $extender_functions->extender_construct_url();
				$output['ajax_script'] = ( isset( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ? urldecode( stripslashes( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ) : '' );
				$output['key']         = 'update';
				break;
			case 'remove':
				unset( $this->passed_parameters['page'] );
				unset( $this->passed_parameters[ 'tags' ] );
				$extender_functions->extender_construct_parameter_update();
				$output['content']     = $extender_functions->extender_populate_content();
				$output['url']         = $extender_functions->extender_construct_url($this->passed_parameters);
				$output['parameters']  = $this->passed_parameters;
				$output['ajax_script'] = ( isset( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ? urldecode( stripslashes( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ) : '' );
				$output['key']         = 'remove';
				break;
			case 'pageURL':
				$extender_functions = new Extender_Functions();
				$extender_functions->extender_construct_parameter_update();
				$this->passed_parameters['page'] = $request_data['page'];
				$output['url']                   = $extender_functions->extender_construct_url();
				$output['key']                   = 'pageURL';
				break;
			case 'page':
				$extender_functions = new Extender_Functions();
				$extender_functions->extender_construct_parameter_update();
				$this->passed_parameters['page'] = $request_data['page'];
				$srp_view                        = $this->theme_options['theme']['inventory_display']['list']['search_filters']['general']['srp_view'];
				switch ( $srp_view ) {
					case 'list':
						$output['content']['inventory'] = $extender_functions->extender_inventory_list_view();
						break;
					case 'grid':
						$output['content']['inventory'] = $extender_functions->extender_inventory_list_view_grid();
						break;
				}
				$output['url']         = $extender_functions->extender_construct_url();
				$output['ajax_script'] = ( isset( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ? urldecode( stripslashes( $this->theme_options['theme']['inventory_display']['list']['scripts']['ajax'] ) ) : '' );
				$output['key']         = 'page';
				break;
			case 'form':
				$extender_functions = new Extender_Functions();
				$output['content']  = $extender_functions->extender_get_gravity_form();
				$output['id']       = $request_data['vin'];
				$output['key']      = 'form';
				break;
			// case 'admin':
			// $output = array(
			// 'content' => '',
			// 'append'  => '',
			// );
			// switch ( $request_data['key'] ) {
			// case 'save':
			// $output['content'] = 'Saved';
			// break;
			// case 'map':
			// switch ( $request_data['action'] ) {
			// case 'add':
			// $output['content'] = $extender_functions->extender_get_admin_maps( true );
			// $output['append']  = 'customMaps';
			// break;
			// case 'save':
			// $address = rawurlencode( trim( $request_data['data']['street'] . ' ' . $request_data['data']['city'] . ' ' . $request_data['data']['state'] . ' ' . $request_data['data']['zip'] ) );
			// $geocode = ( empty( $address ) ? array() : $extender_functions->extender_map_get_geocode( $address ) );
			// if ( isset( $geocode['results'] ) ) {
			// $this->theme_options['theme']['inventor_display']['detail']['maps']['custom'][ $request_data['data']['dealer_id'] ] = array(
			// 'dealer_id' => $request_data['data']['dealer_id'],
			// 'name'      => $request_data['data']['name'],
			// 'street'    => $request_data['data']['street'],
			// 'city'      => $request_data['data']['city'],
			// 'state'     => $request_data['data']['state'],
			// 'zip'       => $request_data['data']['zip'],
			// 'zoom'      => $request_data['data']['zoom'],
			// 'lat'       => $geocode['results'][0]['geometry']['location']['lat'],
			// 'lng'       => $geocode['results'][0]['geometry']['location']['lng'],
			// );
			// }
			// $output['content'] = 'Saved';
			// break;
			// case 'delete':
			// unset( $this->theme_options['theme']['inventor_display']['detail']['maps']['custom'][ $request_data['data']['dealer_id'] ] );
			// break;
			// }
			// break;
			// }
			// break;
			case 'get_gform':
				$output['id'] = $this->passed_parameters['key'];
				( new Saved_Ajax() )->apply_ajax_gravity_hooks( $this->passed_parameters['hooks'] );
				$form              = gravity_form( $this->passed_parameters['form'], $display_title = false, $display_description = false, $display_inactive = false, $field_values = false, $ajax = true, $tabindex = 0, $echo = false );
				$output['content'] = "<div class='list-form-wrapper active'>$form</div>";
				break;
			default:
				$output = array( 'content' => '' );
				break;
		}

		echo wp_json_encode( $output );
		wp_die();
	}


}
