<?php

namespace CarDealerPress\templates\partials\inventory;

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\Includes\Helpers\Option;

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

// for non ajax themes.
class Vehicle_Attributes {

	private static $row_class_count;

	public static function call( $args ): string {
		$layout   = $args['layout'] ?? 'list';
		$page     = $args['page'] ?? 'list';
		$color    = isset( $args['row_color'] );
		$vehicle  = $args['vehicle'];
		$option   = $args['option'] ?? 'cdp_theme_options';
		$location = array( 'theme', 'inventory_display', $page, 'vehicle_attributes' );
		if ( preg_match( '/inventory_list|inventory_slider/', $page ) ) {
			$location = array( $page, 'vehicle_attributes' );
		}
		$content                       = '';
		self::$row_class_count         = 0;
		$display_attribute             = Option::check_for_option(
			$option,
			array_merge( $location, array(
				'display_attribute',
			) )
		);
		$na_message_text               = Option::check_for_option(
			$option,
			array_merge( $location, array(
				'not_found_text',
			) )
		);
		$odometer_show_units           = Option::check_for_option(
			$option,
			array_merge( $location, array(
				'odometer_show_units',
			) )
		);
		//$na_message            = "<span title='Contact Dealer to acquire additional information.' class='data-na'>$na_message_text<sup>*</sup></span>";
		$na_message = "<span class='tooltip-icon'><span class='tooltip' style='display: none;'>$na_message_text</span></span>";
		$vehicle_attributes            = Option::check_for_option(
			$option,
			array_merge( $location, array(
				'attributes',
			) )
		);
		$vehicle_attributes_additional = Option::check_for_option(
			$option,
			array_merge( $location, array(
				'additional'
			) )
		);
		switch ( $layout ) {
			case 'list':
				$content = self::list( $vehicle, $vehicle_attributes, $vehicle_attributes_additional, $na_message, $color, $display_attribute, $odometer_show_units );
				break;
			case 'list_icons':
				$content = self::list_icons( $vehicle, $vehicle_attributes, $vehicle_attributes_additional, $na_message, $color, $display_attribute, $odometer_show_units );
				break;
		}

		return $content;
	}

	public static function extender_row_color_class() {
		if ( self::$row_class_count <= 1 ) { // 0,1 are one row
			self::$row_class_count ++;
			$class = 'row-plain';
		} elseif ( self::$row_class_count <= 3 ) {// 2,3 are one row
			self::$row_class_count ++;
			$class = 'row-color';
		} else {
			self::$row_class_count = 1;
			$class                 = 'row-plain';
		}

		return $class;
	}

	public static function list( $vehicle, $vehicle_attributes, $vehicle_attributes_additional, $na_message, $color, $display_attribute, $odometer_show_units ): string {
		$content             = '';
		$odometer_quantifier = 'miles';
		$company             = ( new Requests() )->cdp_company_request()[0];
		if ( 'CA' === $company['country_code'] ) {
			$odometer_quantifier = 'kilometers';
		}
		foreach ( $vehicle_attributes as $index => $row ) {
			if ( 'on' === $row['desktop'] || 'on' === $row['mobile'] ) {
				$mobile         = ( 'on' === $row['mobile'] ) ? '' : 'mobile-hide';
				$desktop        = ( 'on' === $row['desktop'] ) ? '' : 'desktop-hide';
				$row_color      = ( true === $color ? self::extender_row_color_class() : '' );
				$special_class  = trim( $mobile . ' ' . $desktop . ' ' . $row_color );
				$attribute_name = $row['attribute'];
				if ( ! ( empty( $vehicle[ $attribute_name ] ) && empty( $display_attribute ) ) ) {
					// FUEL.
					if ( 'mpg' === $attribute_name ) {
						$fuel_title = $vehicle_attributes_additional['mpg']['disclaimer'];
						$content    .= '<div class="info-item info-mpg info-mobile ' . $special_class . ' ">';
						$content    .= '<span class="info-label" title="' . $fuel_title . '">';
						$content    .= $row['label'] . '<sup>*</sup></span>';
						if ( ! ( empty( $vehicle['highway'] ) && empty( $vehicle['city'] ) ) ) {
							$content .= '<span class="info-value"><span class="info-city">' . $vehicle['city'] . ' city</span> / <span class="info-hwy">' . $vehicle['highway'] . ' hwy</span>' . '</div>';
						} else {
							$content .= '<span class="info-value">' . $na_message . '</span></div>';
						}
					} else {
						$content   .= '<div class="info-item info-mobile info-' . $attribute_name . ' ' . $special_class . '">';
						$content   .= '<span class="info-label">' . $row['label'] . '</span>';
						$item_prop = '';
						if ( array_key_exists( $attribute_name, $vehicle_attributes_additional ) ) {
							$item_prop = ' itemprop="' . $vehicle_attributes_additional[ $attribute_name ]['item_prop'] . '" ';
						}
						if ( 'odometer' === $attribute_name && 'on' === $odometer_show_units ) {
							$numeric_odometer = is_numeric( $vehicle[ $attribute_name ] ) ? number_format( $vehicle[ $attribute_name ], 0, '.', ',' ) : '0';
							$content          .= '<span class="info-value form-value vehicle-save" data-name="' . $attribute_name . '">' . $numeric_odometer . '<span class="info-quantifier" style="margin-left: 5px;">' . $odometer_quantifier . '</span></span>';
						} else {
							$value = ( empty( $vehicle[ $attribute_name ] ) ? $na_message : ucwords( $vehicle[ $attribute_name ] ) );
							$content .= '<span class="info-value form-value vehicle-save" data-name="' . $attribute_name . '" ' . $item_prop . '>' . $value . '</span>';
						}
						$content .= '</div>';
					}
				}
			}
		}
		return $content;
	}

	public static function list_icons( $vehicle, $vehicle_attributes, $vehicle_attributes_additional, $na_message, $color, $display_attribute, $odometer_show_units ): string {
		$content             = '';
		$odometer_quantifier = 'miles';
		$company             = ( new Requests() )->cdp_company_request()[0];
		if ( 'CA' === $company['country_code'] ) {
			$odometer_quantifier = 'kilometers';
		}
		foreach ( $vehicle_attributes as $index => $row ) {
			if ( 'on' === $row['desktop'] || 'on' === $row['mobile'] ) {
				$mobile         = ( 'on' === $row['mobile'] ) ? '' : 'mobile-hide';
				$desktop        = ( 'on' === $row['desktop'] ) ? '' : 'desktop-hide';
				$row_color      = ( true === $color ? self::extender_row_color_class() : '' );
				$special_class  = trim( $mobile . ' ' . $desktop . ' ' . $row_color );
				$attribute_name = $row['attribute'];
				if ( ! ( empty( $vehicle[ $attribute_name ] ) && empty( $display_attribute ) ) ) {
					// FUEL.
					if ( 'mpg' === $attribute_name ) {
						$fuel_title = $vehicle_attributes_additional['mpg']['disclaimer'];
						$content   .= '<div class="info-item info-mpg info-mobile ' . $special_class . ' ">';
						$content   .= "<div class='attribute-image-icon'>";
						$content   .= "<img src='" . CDP_IMG . "templates/inventory/attributes/$attribute_name.png' alt='$attribute_name icon'>";
						$content   .= "</div>";
						$content   .= "<div class='attribute-label'>";
						$content   .= '<span class="info-label" title="' . $fuel_title . '">';
						$content   .= $row['label'] . '<sup>*</sup></span>';
						if ( ! ( empty( $vehicle['highway'] ) && empty( $vehicle['city'] ) ) ) {
							$content .= '<span class="info-value"><span class="info-city">' . $vehicle['city'] . ' city</span> / <span class="info-hwy">' . $vehicle['highway'] . ' hwy</span>';
						} else {
							$content .= '<span class="info-value">' . $na_message . '</span>';
						}
						$content .= '</div>';
						$content .= '</div>';
					} else {
						$content   .= '<div class="info-item info-mobile info-' . $attribute_name . ' ' . $special_class . '">';
						if (!preg_match('/vin|saleclass|stock/',$attribute_name)) {
							$content .= "<div class='attribute-image-icon'>";
							$content .= "<img src='" . CDP_IMG . "templates/inventory/attributes/$attribute_name.png' alt='$attribute_name icon'></div>";
						}
						$content   .= "<div class='attribute-label'>";
						$content   .= '<span class="info-label">' . $row['label'] . '</span>';
						$item_prop = '';
						if ( array_key_exists( $attribute_name, $vehicle_attributes_additional ) ) {
							$item_prop = ' itemprop="' . $vehicle_attributes_additional[ $attribute_name ]['item_prop'] . '" ';
						}
						if ( 'odometer' === $attribute_name && 'on' === $odometer_show_units ) {
							$numeric_odometer = is_numeric( $vehicle[ $attribute_name ] ) ? number_format( $vehicle[ $attribute_name ], 0, '.', ',' ) : '0';
							$content          .= '<span class="info-value form-value vehicle-save" data-name="' . $attribute_name . '">' . $numeric_odometer . '<span class="info-quantifier" style="margin-left: 5px;">' . $odometer_quantifier . '</span></span>';
						} else {
							$content .= '<span class="info-value form-value vehicle-save" data-name="' . $attribute_name . '" ' . $item_prop . '>' . ( empty( $vehicle[ $attribute_name ] ) ? $na_message : ucwords( $vehicle[ $attribute_name ] ) ) . '</span>';
						}
						$content .= '</div>';
						$content .= '</div>';
					}
				}
			}
		}

		return $content;
	}

}
