<?php

namespace CarDealerPress\Templates\Inventory;

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\Includes\Helpers\Remote_Requests;
use CarDealerPress\templates\partials\inventory\Extra_Boxes;
use CarDealerPress\templates\partials\inventory\Image_Display;
use CarDealerPress\templates\partials\inventory\Prices;
use CarDealerPress\templates\partials\inventory\Forms_Display;
use CarDealerPress\templates\partials\inventory\Schema_Island;
use CarDealerPress\templates\partials\inventory\Vehicle_Attributes;

if ( $vehicle ) {

	$companies = ( new Requests() )->cdp_db_init( 'company' );
	$company   = array();
	foreach ( $companies as $key => $info ) {
		if ( $info['dealer_id'] === $vehicle['dealer_id'] ) {
			$company = $info;
		}
	}

	$price                    = $fn_inventory->get_price_display(
		array(
			'company' => $this->company,
			'vehicle' => $vehicle,
			'theme'   => 'inventory',
		)
	);
	$vehicle['primary_price'] = $price['primary_price'];
	// setup payments text
	$loan_parameters = array(
		'make'      => $vehicle['make'],
		'model'     => $vehicle['model'],
		'trim'      => $vehicle['trim'],
		'year'      => $vehicle['year'],
		'saleclass' => $vehicle['saleclass'],
	);
	$prices          = $fn_inventory->get_prices( $vehicle );
	$payment         = $fn_inventory->get_loan_value( Option::check_for_option( 'cdp_theme_options', array(
		'theme',
		'inventory_display',
		'payment_calculator'
	) ), $prices['main_price'], $loan_parameters );

	$parameters['saleclass'] = $vehicle['saleclass'];
	$fn_inventory->apply_gravity_form_hooks( $vehicle );
	// swiper.
	wp_enqueue_script( 'cdp_swiper_js', CDP_JS . 'swiper/swiper-bundle.min.js', array(), '12.0.2', true );
	wp_enqueue_style( 'cdp_swiper_css', CDP_CSS . 'swiper/swiper-bundle.min.css', array(), '12.0.2' );

	wp_enqueue_script( 'cdp_theme_slider_js', CDP_JS . 'templates/inventory/hedgehog/image_slider.js', array( 'jquery' ), CDP_VERSION, true );
	wp_enqueue_style( 'cdp_theme_slider_css', CDP_CSS . 'templates/inventory/hedgehog/slider.css', array(), CDP_VERSION );
	wp_enqueue_style( 'cdp_theme_css', CDP_CSS . 'templates/inventory/hedgehog/detail.css', array(), CDP_VERSION );


	// Map.
	if ( $this->theme_options['theme']['inventory_display']['detail']['maps']['key'] ) {
		$map_output = $ext_functions->extender_map_generator( $vehicle['dealer_id'] );
	} else {
		$map_output = null;
	}
	$ext_functions->load_parameters( $this->parameters );

	helpers::output_wp_kses( $ext_functions->extender_google_tag_script( $vehicle ) );

	$content = "<div id='inventory-wrapper'>";
	if ( $this->theme_options['theme']['inventory_display']['general']['saved_vehicles']['display'] ) {
		$content .= $ext_functions->extender_saved_vehicle_container();
	}
	$content .= "<div id='inventory-detail' class='inventory-main-container'>";
	$content .= "<a id='page-link' class='vehicle-save' style='display: none;' data-name='link' href='" . esc_url( $page_url ) . "'></a>";
	$content .= "<div id='inventory-detail-navigation'>";
	$content .= "<div class='navigation-container previous-link' onclick='window.history.go(-1); return false;'>";
	$content .= "<span class='previous-icon banner-icon'></span>";
	$content .= "<span class='previous-text banner-text'>Previous Search</span>";
	$content .= "</div>";
	$content .= "<div class='navigation-container friendly-print' onclick='window.open('?print_page','popup','width=800,height=900,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=yes,status=no,left=0,top=0'); return false'>";
	$content .= "<span class='print-icon banner-icon'></span>";
	$content .= "<span class='print-text banner-text'>Print</span>";
	$content .= "</div>";
	$content .= "<div class='navigation-container dealer-number'>";
	$content .= "<a href='tel:" . esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ) . "'>";
	$content .= "<span class='phone-icon banner-icon'></span>";
	$content .= "<span class='phone-text banner-text'>Sales: " . esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ) . "</span>";
	$content .= "  </a>";
	$content .= " </div>";
	if ( 'on' === $this->theme_options['theme']['inventory_display']['detail']['general']['social']['facebook']['enable'] ) {
		$content .= "<div class='navigation-container facebook-share'>";
		$content .= "<div class='fb-share-button' data-href='" . esc_url( $this->page_url ) . "' data-layout='button' data-size='small' data-mobile-iframe='true'>";
		$content .= "<a class='fb-xfbml-parse-ignore' target='_blank' href='https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&amp;src=sdkpreparse'>Share</a>";
		$content .= "</div>";
		$content .= "</div>";
	}
	$content .= "</div>";
	$banners = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'inventory_display', 'banners' ) );
	if ( ! empty( $banners ) ) {
		$content        .= '<div id="cdp-banner-top-container">';
		$banner_results = $fn_inventory->cdp_get_banners( 'detail', 'top', $banners, $this->parameters );
		foreach ( $banner_results as $banner ) {
			$content .= $banner;
		}
		$content .= '</div>';
	}
	$content    .= $ext_functions->detail_page_sold_vehicle( $vehicle );
	$vin        = $vehicle['vin'];
	$company_id = helpers::company_id();
	$dealer_id  = $vehicle['dealer_id'];
	$content    .= "<div id='$vin' class='inventory-detail-vehicle-container vehicle-container company-$company_id location-$dealer_id'>";

	$content .= empty( $vehicle['headline'] ) ? '' : "<h4 class='inventory-vehicle-headline'>" . $vehicle['headline'] . "</h4>";
	$content .= "<div id='title-container' class='detail-wrapper'>";
	$content .= "<div class='title-data'>";
	if ( $vehicle['certified'] ) {
		$saleclass = 'Certified';
	} else {
		$saleclass = ucfirst( $vehicle['saleclass'] );
	}
	$content .= "<div class='title-item header-saleclass-container'>" . esc_html( $saleclass ) . "</div>";
	$content .= "<div class='title-item header-vehicle-title-container'>";
	$content .= "<span class='header-year vehicle-save form-value' data-name='year'>" . $vehicle['year'] . "</span>";
	$content .= "<span class='header-make form-value vehicle-save' data-name='make'>" . $vehicle['make'] . "</span>";
	$content .= "<span class='header-model form-value vehicle-save' data-name='model'>" . $vehicle['model'] . "</span>";
	$content .= "<span class='header-trim form-value vehicle-save' data-name='trim'>" . $vehicle['trim'] . "</span>";
	$content .= " </div>"; // end title-data
	$content .= "</div>"; // end title-container

	$contact = $ext_functions->contact_wrapper( $vehicle, $company, 'detail' );
	if ( ! empty( $contact ) ) {
		$content .= "<div id='contact-container' class='dealer-contact-wrapper'>";
		$content .= $contact;
		$content .= "</div>";
	}
	$content .= "</div>"; // end title-wrapper

	$content .= "<div id='top-slider-container' class=''>";
	$content .= ( new Image_Display() )->sliders_per_view( $vehicle );
	$content .= "</div>";

	$content .= "<div id='middle-container'>";
	//$content .= "<div id='left-middle-container' class='left-middle'>";

	$tab_default   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'general',
			'default_image_tab',
		)
	);
	$video_checked = $photo_checked = '';
	if ( $tab_default == 'Video' && $vehicle['video'] ) {
		$video_checked = 'checked';
	} else {
		$photo_checked = 'checked';
	}

	$content .= "<div id='photo-video-container' class='tabs grid-detail'>";
	$content .= $vehicle['video'] ? "<input type='radio' name='tabset' id='tab-video' class='tab-button' $video_checked>" : '';
	$content .= "<input type='radio' name='tabset' id='tab-photo' class='tab-button' $photo_checked>";
	$content .= '<div class="tab-labels mobile-hide">';
	if ( $vehicle['video'] ) {
		$content .= '<label for="tab-video">Video</label>';
		$content .= '<label for="tab-photo">Photos</label>';
	}
	$content .= '</div>';

	/* Tab panels */
	$content .= '<div id="video" class="tab-content">';
	$content .= ( new Image_Display() )->video_display( $vehicle );
	$content .= '</div>';

	$content .= '<div id="photo" class="tab-content mobile-hide">';
	$content .= ( new Image_Display() )->swiper_carousel( $vehicle );
	$content .= '</div>';

	$content .= '</div>'; //end tabs photo-video
	// video mobile only

	$content .= "<fieldset id='vehicle-attributes-container' class='vehicle-attributes bordered-box grid-detail'>";
	$content .= "<legend class='box-title'>Details</legend>";
	$content .= "<div class='vehicle-attributes-container'>";
	$content .= Vehicle_Attributes::call(
		array(
			'vehicle' => $vehicle,
			'layout'  => 'list_icons',
			'page'    => 'detail',
		)
	);
	$content .= " </div>";
	$content .= " </fieldset>"; // end vehicle-attributes

	$extra_boxes = ( new Extra_Boxes() )->boxes_horizontal( $vehicle, 'detail' );
	if ( ! empty( $extra_boxes ) ) {
		$content .= "<div id='extra-boxes-container' class='extra-boxes grid-detail'>";
		$content .= $extra_boxes;
		$content .= "</div>";
	}

	if ( ! empty( $vehicle['description'] ) ) {
		$content .= "<fieldset id='description-container' class='description bordered-box grid-detail'>";
		$content .= "<legend class='box-title'>Description</legend>";
		$content .= "<div class='vehicle-description'><p>" . wp_kses( $vehicle['description'], 'post' ) . "</p></div>";
		$content .= "</fieldset>";
	}

	//$content .= "</div>"; // end left-middle-container

	//$content .= " <div id='right-middle-container' class='right-middle mobile-hide'>";

	$content .= "<div id='price-container' class='price-container box-shadow-style bordered-box grid-detail'>";
	$content .= ( new Prices( $vehicle ) )->price_box();
	$content .= "</div>";

	$payment_calculator_form[] = $ext_functions->extender_detail_loan( $vehicle );

	if ( intval( $vehicle['primary_price'] ) != 0 && ! empty( $payment_calculator_form[0] ) ) {
		$content .= "<div id='payment-container' class='box-shadow-style bordered-box grid-detail'>";
		$content .= "<div class=''>";
		$content .= "<span class=''>Finance</span>";
		$content .= $payment;
		$content .= "</div>";
		$content .= "</div>";
	}

	$forms[] = $ext_functions->extender_detail_forms( $vehicle );

	if ( ! empty( $forms[0] ) || ! empty( $payment_calculator_form[0] ) ) {
		$content .= "<div id='forms-container' class='grid-detail'>";
		$content .= ( new Forms_Display() )->buttons( $forms, 'forms' );
		$content .= intval( $vehicle['primary_price'] ) != 0 ? ( new Forms_Display() )->buttons( $payment_calculator_form, 'pc' ) : '';
		$content .= "</div>";
	}
	//$content .= " </div>"; // end right middle container

	//$content .= "</div>"; // end middle-container

	//$content .= "<div id='bottom-container'>";
	if ( ! empty( $vehicle['dealer_options'] ) ) {
		$content .= "<fieldset id='dealer-options-container' class='dealer-options detail-wrapper bordered-box grid-detail'>";
		$content .= "<legend class='box-title'>Features</legend>";
		$content .= "<div class='dealer-options-container'>";
		$content .= "<div class='dealer-options-list'><ul>";
		foreach ( $vehicle['dealer_options'] as $option ) {
			$content .= "<li class='value'>$option</li>";
		}
		$content .= "</div></ul>";
		$content .= "</div>";
		$content .= "</fieldset>";
	}
	if ( Option::check_for_option( 'cdp_theme_options', array(
		'theme',
		'inventory_display',
		'detail',
		'general',
		'show_standard_eq'
	) ) ) {
		$equipment = $fn_inventory->display_equipment( $vehicle['standard_equipment'], $vehicle['saleclass'] );
		if ( ! empty( $equipment ) ) {
			$content .= "<div id='standard-features-container' class='standard-equipment-container detail-wrapper grid-detail'>";
			$content .= "<div class='section-heading-title'>Standard Equipment</div>";
			$content .= $equipment;
			$content .= "</div>";
		}
	}
	if ( $this->theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'] ) {
		$similar = $ext_functions->extender_similar_vehicle_pull( $vehicle );
		if ( ! str_contains( $similar, 'No Similar Vehicles Found' ) ) {
			$content .= "<fieldset id='similar-vehicles-container' class='similar-vehicles detail-wrapper bordered-box grid-detail'>";
			$content .= "<legend class='box-title'>Similar Vehicles</legend>";
			$content .= $similar;
			$content .= "</fieldset>";
		}
	}

	$content .= empty( $map_output ) ? '' : "<div id='map-container' class='map-container detail-wrapper grid-detail'></div>";
	wp_register_script( 'cdp_injector_js', CDP_JS . 'templates/inventory/injector.js', 'jquery', CDP_VERSION, true ); //injector.js is a placeholder file.
	wp_enqueue_script( 'cdp_injector_js' );
	wp_add_inline_script( 'cdp_injector_js', $map_output );
	$content .= " </div>"; // end middle-container
	$content .= "<fieldset id='disclaimer-container' class='disclaimer detail-wrapper bordered-box'>";
	$content .= "<legend class='box-title'>Disclaimer</legend>";
	$content .= wp_kses( $fn_inventory->get_disclaimer(), 'post' );
	$content .= "</fieldset>";
	$content .= Schema_Island::island( $vehicle, $company, $price );
	$content .= "</div>"; // end vin container
	//$content .= "</div>"; // end bottom-container
	$content .= "</div>"; // end inventory-detail
	$content .= " </div>"; // end inventory-wrapper

	echo $content;
}