<?php

namespace CarDealerPress\Templates\Inventory;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\functions\Fn_Inventory;
use CarDealerPress\includes\helpers\Dynamic_Site_Headers;
use CarDealerPress\includes\helpers\helpers;

$fn_inventory         = new Fn_Inventory();
$dynamic_site_headers = new Dynamic_Site_Headers( $this->company['dealer_id'] );

$this->compared_vehicles = $this->saved_vehicles = array();
$content                 = '';

$dynamic_site_headers->apply_headers( $this->parameters );

// Load Dependent Scripts.
$dependent_scripts = array( 'jquery', 'jquery-ui-core', 'jquery-ui-tabs', 'jquery-ui-dialog', 'jquery-ui-slider' );
foreach ( $dependent_scripts as $dependent ) {
	wp_enqueue_script( $dependent );
}
wp_enqueue_script( 'cdp_jquery_cookie', CDP_JS . 'jquery-cookie/js.cookie.js', array( 'jquery' ), '2.1.3', true );

// Load Save Form.
/*
if( function_exists('gravity_form_enqueue_scripts') ){
	if( $this->options['vehicle_management_system']['theme']['saved_vehicles']['form'] ){
		$gform_element = '<div style="display: none;" id="saved-gform-id" form="'.$this->options['vehicle_management_system']['theme']['saved_vehicles']['form'].'"></div>';
		$gform_class = 'saved_get_gform';
		gravity_form_enqueue_scripts($this->options['vehicle_management_system']['theme']['saved_vehicles']['form'], true);
	}
}*/

// Load Page Style/Script.
$saved_style_url  = CDP_CSS . 'templates/inventory/compare-page.css';
$saved_style_url  = apply_filters( 'cdp_compare_style_url', $saved_style_url );
$saved_script_url = CDP_JS . 'templates/inventory/save-compare.js';
$saved_script_url = apply_filters( 'cdp_compare_script_url', $saved_script_url );

wp_enqueue_style( 'cdp_compare', $saved_style_url, array(), CDP_VERSION );
wp_register_script( 'cdp_compare_js', $saved_script_url, $dependent_scripts, CDP_VERSION, true );
$ajax_script = admin_url( 'admin-ajax.php' );
wp_localize_script(
	'cdp_compare_js',
	'ajax_object',
	array(
		'url'   => $ajax_script,
		'nonce' => wp_create_nonce( 'cdp_nonce' ),
	)
);
wp_enqueue_script( 'cdp_compare_js' );

// Get Save Cookies.
foreach ( $_COOKIE as $key => $value ) {
	if ( strpos( $key, 'vehicleSave' ) !== false ) {
		$this->saved_vehicles[ str_replace( 'CDPvehicleSave_', '', $key ) ] = $value;
	}
}

// Get Compare Cookies.
foreach ( $_COOKIE as $key => $value ) {
	if ( strpos( $key, 'vehicleCompare' ) !== false ) {
		$this->compared_vehicles[ str_replace( 'CDPvehicleCompare_', '', $key ) ] = $value;
	}
}

// Build Compare Display.
$content .= '<div id="compare-vehicle-container">';

$class   = ( count( $this->saved_vehicles ) > 0 ? 'saved' : 'empty' );
$content .= '<div id="saved-vehicle-container">';
$content .= '<div id="saved-vehicle-count-container" class="' . $class . '" >View Saved Vehicles (<span id="saved-vehicle-count">' . count( $this->saved_vehicles ) . '</span>)</div>';
$content .= '</div>';

if ( ! empty( $this->compared_vehicles ) ) {
	$content .= '<div id="compare-vehicle-inner-container">';

	// Build First Static Column.
	$content .= '<div class="compare-vehicle-item static">';
	$content .= '<div class="compare-top-content static">';
	$content .= '</div>';
	$content .= '<div class="compare-bottom-content static">';
	$content .= '<div class="compare-data-item static vehicle-type saleclass">Vehicle Type</div>';
	$content .= '<div class="compare-data-item static price">Price</div>';
	$content .= '<div class="compare-data-item static year">Year</div>';
	$content .= '<div class="compare-data-item static make">Make</div>';
	$content .= '<div class="compare-data-item static model">Model</div>';
	$content .= '<div class="compare-data-item static trim">Trim</div>';
	$content .= '<div class="compare-data-item static bodystyle">Bodystyle</div>';
	$content .= '<div class="compare-data-item static odometer">Odometer</div>';
	$content .= '<div class="compare-data-item static transmission">Transmission</div>';
	$content .= '<div class="compare-data-item static drivetrain">Drivetrain</div>';
	// $content .= '<div class="compare-data-item static cylinders">Cylinders</div>'; Don't have this data
	$content .= '<div class="compare-data-item static engine">Engine</div>';
	$content .= '<div class="compare-data-item static interior-color">Int. Color</div>';
	$content .= '<div class="compare-data-item static exterior-color">Ext. Color</div>';
	$content .= '<div class="compare-data-item static stock">Stock #</div>';
	$content .= '<div class="compare-data-item static vin">Vin #</div>';
	$content .= '</div>';
	$content .= '</div>';

	foreach ( $this->compared_vehicles as $key => $values ) {
		$vehicle_raw = ( new Requests() )->cdp_db_init( 'inventory', array( 'vin' => $key ) );

		$compared_data = json_decode( stripslashes( $values ) );
		if ( ! empty( $vehicle_raw ) ) {
			$vehicle = $vehicle_raw[0];
			$content .= '<div id="' . $key . '" class="compare-vehicle-item">'; // compare-vehicle-item Open.
			$content .= '<div class="compare-top-content">';
			$content .= '<div class="compare-top-image">';
			$image   = $vehicle['thumbnail'];
			if ( ( empty( $image ) || strstr( $image, '_no_photo' ) ) && ! empty( $this->theme_options['theme']['overrides']['default_no_image'] ) ) {
				$image = $this->theme_options['theme']['overrides']['default_no_image'];
			}
			// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
			$content .= '<img src="' . $image . '" />';
			$content .= '</div>';
			$content .= '<div class="compare-top-buttons">';
			$content .= '<div class="compare-detail-button"><a href="' . $compared_data->link . '" target="_blank">More Details</a></div>';

			if ( ! empty( $this->saved_vehicles ) ) {
				$save_class = ( array_key_exists( $vehicle['vin'], $this->saved_vehicles ) ? 'checked' : '' );
			} else {
				$save_class = '';
			}

			$content .= '<div class="info-item saved-vehicles-container">';
			$content .= '<div class="inventory-vehicle-save ' . $save_class . '">';
			$content .= '<span class="save-text">' . ( empty( $save_class ) ? 'Save Vehicle' : 'Vehicle Saved' ) . '</span>';
			$content .= '<span class="save-checkbox">' . ( empty( $save_class ) ? '' : 'X' ) . '</span>';
			$content .= '</div>';
			$content .= '</div>';
			$content .= '</div>';
			$content .= '</div>';
			$content .= '<div class="compare-bottom-content">';
			$content .= '<div class="compare-data-item vehicle-type saleclass">' . ( empty( $vehicle['saleclass'] ) ? '<span class="not-available">N/A</span>' : ucfirst( $vehicle['saleclass'] ) ) . '</div>';

			$price   = $fn_inventory->get_price_display(
				array(
					'company' => $this->company,
					'vehicle' => $vehicle,
					'theme'   => 'saved',
				)
			);
			$content .= '<div class="compare-data-item price">$' . number_format( $price['primary_price'], 0, '.', ',' ) . '</div>';
			$content .= '<div class="compare-data-item year">' . ( empty( $vehicle['year'] ) ? '<span class="not-available">N/A</span>' : $vehicle['year'] ) . '</div>';
			$content .= '<div class="compare-data-item make">' . ( empty( $vehicle['make'] ) ? '<span class="not-available">N/A</span>' : $vehicle['make'] ) . '</div>';
			$content .= '<div class="compare-data-item model">' . ( empty( $vehicle['model'] ) ? '<span class="not-available">N/A</span>' : $vehicle['model'] ) . '</div>';
			$content .= '<div class="compare-data-item trim">' . ( empty( $vehicle['trim'] ) ? '<span class="not-available">N/A</span>' : $vehicle['trim'] ) . '</div>';
			$content .= '<div class="compare-data-item bodystyle">' . ( empty( $vehicle['body'] ) ? '<span class="not-available">N/A</span>' : $vehicle['body'] ) . '</div>';
			$content .= '<div class="compare-data-item odometer">' . ( empty( $vehicle['odometer'] ) ? '<span class="not-available">N/A</span>' : $vehicle['odometer'] ) . '</div>';
			$content .= '<div class="compare-data-item transmission">' . ( empty( $vehicle['transmission'] ) ? '<span class="not-available">N/A</span>' : $vehicle['transmission'] ) . '</div>';
			$content .= '<div class="compare-data-item drivetrain">' . ( empty( $vehicle['drivetrain'] ) ? '<span class="not-available">N/A</span>' : $vehicle['drivetrain'] ) . '</div>';
			// $content .= '<div class="compare-data-item static cylinders">Cylinders</div>'; Don't have this data
			$content .= '<div class="compare-data-item engine">' . ( empty( $vehicle['engine'] ) ? '<span class="not-available">N/A</span>' : $vehicle['engine'] ) . '</div>';
			$content .= '<div class="compare-data-item interior-color">' . ( empty( $vehicle['interior'] ) ? '<span class="not-available">N/A</span>' : $vehicle['interior'] ) . '</div>';
			$content .= '<div class="compare-data-item exterior-color">' . ( empty( $vehicle['exterior'] ) ? '<span class="not-available">N/A</span>' : $vehicle['exterior'] ) . '</div>';
			$content .= '<div class="compare-data-item stock">' . ( empty( $vehicle['stock'] ) ? '<span class="not-available">N/A</span>' : $vehicle['stock'] ) . '</div>';
			$content .= '<div class="compare-data-item vin">' . ( empty( $vehicle['vin'] ) ? '<span class="not-available">N/A</span>' : $vehicle['vin'] ) . '</div>';
			$content .= '</div>';
			$content .= '<div class="compare-saved-vehicle-content" style="display: none;">'; // compare-saved-vehicle-content Open.
			$content .= '<span data-name="saleclass" class="save-item removed-saleclass">' . ucfirst( $vehicle['saleclass'] ) . '</span>';
			$content .= '<span data-name="year" class="save-item save-year">' . $vehicle['year'] . '</span>';
			$content .= '<span data-name="make" class="save-item save-make">' . $vehicle['make'] . '</span>';
			$content .= '<span data-name="model" class="save-item save-model">' . $vehicle['model'] . '</span>';
			$content .= '<span data-name="trim" class="save-item save-trim" style="display: none">' . $vehicle['trim'] . '</span>';
			$content .= '<span data-name="image" class="save-item save-image" style="display: none">' . $image . '</span>';
			$content .= '<span data-name="link" class="save-item save-link" style="display: none">' . $compared_data->link . '</span>';
			$content .= '<span data-name="price" class="save-item save-price" style="display: none">' . $price['primary_price'] . '</span>';
			$content .= '</div>'; // compare-saved-vehicle-content Close.
			$content .= '</div>'; // compare-vehicle-item.
		}
	}
	$content .= '</div>';
} else {
	$content .= '<div id="compare-not-found">No vehicles have been selected to be compared.</div>';
}
get_header();
helpers::output_wp_kses( $content );
// echo !empty($gform_element) ? $gform_element: '';
get_footer();
