<?php

namespace CarDealerPress\templates\partials\inventory;

use CarDealerPress\Includes\Functions\Fn_Inventory;
use DateTime;
use elementor\modules\home\transformations\Filter_Top_Section_By_License;

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

// for non ajax themes.
class Schema_Island {

	public static function island( $vehicle, $company, $price ): string {
		$unitCode = 'SMI'; # miles
		if ( 'CA' === $company['country_code'] ) {
			$unitCode = 'KMT'; # kilometers
		}
		switch ($vehicle['drivetrain']){
			case 'AWD':
				$drivetrain = 'AllWheelDriveConfiguration';
				break;
			case '4WD':
				$drivetrain = 'FourWheelDriveConfiguration';
				break;
			case 'FWD':
				$drivetrain = 'FrontWheelDriveConfiguration';
				break;
			case 'RWD':
				$drivetrain = 'RearWheelDriveConfiguration';
				break;
			default:
				$drivetrain = '';
		}
		$rules          = get_option( 'rewrite_rules' );
		$url_rule       = isset( $rules['^(inventory)'] );
		$link_params    = array(
			'year'  => $vehicle['year'],
			'make'  => $vehicle['make'],
			'model' => $vehicle['model'],
			'state' => $company['state'],
			'city'  => $company['city'],
			'vin'   => $vehicle['vin'] ?? '',
			'vms_id'    => $vehicle['vms_id'] ?? '',
		);
		$home_url = ( new Fn_Inventory() )->generate_inventory_link( array('params' => $link_params, 'type' => 1) );

		$content = '<div class="data-island" style="display:none;">';
			$content .= '<div itemscope itemtype="https://schema.org/Car" id="' . $vehicle['vin'] . '">';
				$content .= '<div itemprop="url" href="'.site_url() . $home_url.'"></div>';
				$content .= '<div itemprop="offers" itemscope itemtype="https://schema.org/Offer">';
					$content .= '<span itemprop="availability" content="InStock"></span>';
					$datetime = new DateTime('tomorrow');
					$tomorrow = $datetime->format('Y-m-d');
					$content .= '<span itemprop="priceValidUntil" content="' . $tomorrow . '"></span>';
					$content .= $price['primary_text'];
				$content .= '</div>';
				if ($vehicle['photos_raw']) {
					$photo   = get_object_vars( $vehicle['photos_raw'][0] )['large'];
					$content .= '<div itemprop="image" content="' . $photo . '"></div>';
				}
				$content .= '<div itemprop="itemCondition" content="'.ucfirst( $vehicle['saleclass'] ).'Condition"></div>';
				$content .= '<div itemprop="availability" content="InStock"></div>';
				$content .= '<div itemprop="vehicleModelDate" content="' . $vehicle['year'] . '"></div>';
				$content .= '<div itemprop="brand">' . $vehicle['make'] . '</div>';
				$content .= '<div itemprop="vehicleIdentificationNumber" content="' . $vehicle['vin'] . '"></div>';
				$content .= '<div itemprop="manufacturer" content="' . $vehicle['make'] . '"></div>';
				$content .= '<div itemprop="model" content="' . $vehicle['model'] . '"></div>';
				$content .= '<div itemprop="vehicleConfiguration" content="' . $vehicle['trim'] . '"></div>';
				$content .= '<div itemprop="sku" content="' . $vehicle['stock'] . '"></div>';
				$content .= '<div itemprop="name" content="' . $vehicle['year'] . ' ' . $vehicle['make'] . ' ' . $vehicle['model'] . '"></div>';
				$content .= '<div itemprop="vehicleTransmission" content="' . $vehicle['transmission'] . '"></div>';
				$content .= '<div itemprop="driveWheelConfiguration" content="' . $drivetrain . '"></div>';
				$content .= '<div itemprop="numberOfDoors" content="' . $vehicle['doors'] . '"></div>';
				$content .= '<div itemprop="vehicleEngine" itemscope itemtype="https://schema.org/EngineSpecification">';
					$content .= '<span itemprop="name">' . $vehicle['engine'] . '</span>';
					$content .= '<span itemprop="fuelType" content="' . $vehicle['fuel_type'] . '"></span>';
				$content .= '</div>';
				$content .= '<div itemprop="bodyType" content="' . $vehicle['body'] . '"></div>';
				$content .= '<div itemprop="color" content="' . $vehicle['exterior'] . '"></div>';
				$content .= '<div itemprop="vehicleInteriorColor" content="' . $vehicle['interior'] . '"></div>';
				$content .= '<div itemprop="mileageFromOdometer" itemscope itemtype="https://schema.org/QuantitativeValue">';
					$content .= '<span itemprop="value" content="' . $vehicle['odometer'] . '"></span>';
					$content .= '<span itemprop="unitCode" content="' . $unitCode . '"></span>';
				$content .= '</div>';
			$content .= '</div>';
		$content .= '</div>';

		return $content;

	}
}