<?php

namespace CarDealerPress\templates\partials\inventory;

use CarDealerPress\includes\functions\Extender_Functions;
use CarDealerPress\includes\functions\Fn_Inventory;
use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;

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

// for non ajax themes
class Search_Bar_Filters {

	public static function price_range_list( $args ): string {
		$fn_inventory = new Fn_Inventory();
		$parameters   = $args['parameters'];
		$cdp_theme_options = Option::get_cdp_theme_options();
		$bucket_size = 	$cdp_theme_options['theme']['inventory_display']['list']['search_filters']['general']['price_step'];
		$content      = '<ul>';
		$index        = 0;
		$data         = ( new Extender_Functions() )->get_range_list( 'price' );
		$buckets      = List_Search_Filters::range_buckets( $data['data'], $bucket_size );
		if ( $buckets ) {
			while ( $index < sizeof( $buckets ) - 1 ) {
				$price_from = $buckets[ $index ];;
				$price_from_formatted = '<span>$' . number_format( $price_from, 0, '.', ',' ) . '</span>';
				$price_to             = $buckets[ $index + 1 ];
				$price_to_formatted   = '<span>$' . number_format( $price_to, 0, '.', ',' ) . '</span>';
				$link                 = $fn_inventory->generate_inventory_link( array(
						'params'  => $parameters,
						'include' => array(
							'price_from' => $price_from,
							'price_to'   => $price_to,
						)
					)
				);
				$content              .= "<li><a rel='nofollow' href='$link' class='' >$price_from_formatted - $price_to_formatted</a></li>";
				$index ++;

			}
		}
		$content .= '</ul>';

		return $content;
	}

	public static function price_range_dropdown( $args ): string {
		$parameters  = $args['parameters'];
		$price_range = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'list',
				'search_filters',
				'general',
				'price_step',
			)
		);
		$data        = ( new Extender_Functions() )->get_range_list( 'price' );
		$buckets     = List_Search_Filters::range_buckets( $data['data'], $price_range );
		$content     = '<select class="inventory-select" onchange="document.location = this.value;">';
		$text        = isset( $parameters['price_from'] ) ? 'Remove Price Range' : 'Select Price Range';
		$content     .= '<option value=' . esc_url( remove_query_arg(
					array(
						'price_from',
						'price_to',
					) )
			) . '>' . $text . '</option>';
		$index       = 0;
		if ( $buckets ) {
			while ( $index < sizeof( $buckets ) - 1 ) {
				$price_from = $buckets[ $index ];;
				$price_from_formatted = '$' . number_format( $price_from, 0, '.', ',' );
				$price_to             = $buckets[ $index + 1 ];
				$price_to_formatted   = '$' . number_format( $price_to, 0, '.', ',' );
				$value                = esc_url( add_query_arg(
						array(
							'price_from' => $price_from,
							'price_to'   => $price_to,
						) )
				);
				$selected             = isset( $parameters['price_from'] ) ? ( $parameters['price_from'] === $price_from ? 'selected' : '' ) : '';
				$content              .= '<option value=' . $value . " $selected >$price_from_formatted - $price_to_formatted</option>";
				$index ++;

			}
		}
		$content .= '</select>';

		return $content;
	}

	public static function sale_class_list( $args ): string {
		$parameters = $args['parameters'];
		$all_link   = $args['all_link'];
		$new_link   = $args['new_link'];
		$used_link  = $args['used_link'];
		$cert_link  = $args['cert_link'];
		$saleclass  = $parameters['saleclass'];

		$content            = '<ul>';
		$hide_certified     = 'on' === Option::check_for_option(
				'cdp_theme_options',
				array(
					'theme',
					'inventory_display',
					'list',
					'search_filters',
					'general',
					'hide_certified_saleclass',
				)
			);
		$override_saleclass = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'overrides',
				'saleclass',
			)
		);
		switch ( $override_saleclass ) {
			case 'all':
				$content .= '<li><span class="no-style"><a href="' . $new_link . '" title="View New Inventory" class="list-button ' . ( 'new' === strtolower( $saleclass ) ? 'disabled' : null ) . '">New</a></span></li>';
				$content .= '<li><span class="no-style"><a href="' . $used_link . '" title="View Used Inventory" class="list-button ' . ( 'used' === strtolower( $saleclass ) ? 'disabled' : null ) . '">Used</a></span></li>';
				$content .= ! $hide_certified ? '<li><span class="no-style"><a href="' . $cert_link . '" title="View Certified Used Inventory" class="list-button ' . ( 'used' === strtolower( $saleclass ) ? 'disabled' : null ) . '">Certified</a></span></li>' : '';
				break;
			case 'new':
				$content .= '<li><span class="no-style"><a href="' . $new_link . '" title="View New Inventory" class="list-button ' . ( 'new' === strtolower( $saleclass ) ? 'disabled' : null ) . '">New</a></span></li>';
				break;
			case 'used':
				$content .= '<li><span class="no-style"><a href="' . $used_link . '" title="View Used Inventory" class="list-button ' . ( 'used' === strtolower( $saleclass ) ? 'disabled' : null ) . '">Used</a></span></li>';
				$content .= ! $hide_certified ? '<li><span class="no-style"><a href="' . $cert_link . '" title="View Certified Used Inventory" class="list-button ' . ( 'used' === strtolower( $saleclass ) ? 'disabled' : null ) . '">Certified</a></span></li>' : '';
				break;
			case 'certified':
				$content .= '<li><span class="no-style"><a href="' . $cert_link . '" title="View Certified Used Inventory" class="list-button ' . ( 'used' === strtolower( $saleclass ) ? 'disabled' : null ) . '">Certified</a></span></li>';
				break;
		}
		$content .= '</ul>';

		return $content;
	}

	public static function sale_class_dropdown( $args ): string {
		$parameters = $args['parameters'];
		$all_link   = $args['all_link'];
		$new_link   = $args['new_link'];
		$used_link  = $args['used_link'];
		$cert_link  = $args['cert_link'];
		$saleclass  = $parameters['saleclass'];

		$content            = '<select id="inventory-saleclass" class="inventory-select" onchange="document.location = this.value;">';
		$hide_certified     = 'on' === Option::check_for_option(
				'cdp_theme_options',
				array(
					'theme',
					'inventory_display',
					'list',
					'search_filters',
					'general',
					'hide_certified_saleclass',
				)
			);
		$override_saleclass = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'overrides',
				'saleclass',
			)
		);
		switch ( $override_saleclass ) {
			case 'all':
				$content .= '<option data-name="" value="' . $all_link . '" ' . ( 'all' === strtolower( $saleclass ) ? 'selected' : null ) . ' >All Vehicles</option>';
				$content .= '<option data-name="new" value="' . $new_link . '" ' . ( 'new' === strtolower( $saleclass ) ? 'selected' : null ) . ' >New Vehicles</option>';
				$content .= '<option data-name="used" value="' . $used_link . '" ' . ( 'used' === strtolower( $saleclass ) && empty( $certified ) ? 'selected' : null ) . ' >Pre-Owned Vehicles</option>';
				$content .= ! $hide_certified ? '<option data-name="certified" value="' . $cert_link . '" ' . ( 'used' === strtolower( $saleclass ) && ! empty( $certified ) ? 'selected' : null ) . ' >Certified Pre-Owned</option>' : '';
				break;
			case 'new':
				$content .= '<option data-name="new" value="' . $new_link . '" selected >New Vehicles</option>';
				break;
			case 'used':
				$content .= '<option data-name="used" value="' . $used_link . '" ' . ( 'used' === strtolower( $saleclass ) && empty( $certified ) ? 'selected' : null ) . ' >Pre-Owned Vehicles</option>';
				$content .= ! $hide_certified ? '<option data-name="certified" value="' . $cert_link . '" ' . ( 'used' === strtolower( $saleclass ) && ! empty( $certified ) ? 'selected' : null ) . ' >Certified Pre-Owned</option>' : '';
				break;
			case 'certified':
				$content .= '<option data-name="certified" value="' . $cert_link . '" selected >Certified Pre-Owned</option>';
				break;
		}
		$content .= '</select>';

		return $content;
	}

	// wip to replace named dropdowns
	// public static function range_dropdown( $args ): string {
	// $parameters = $args['parameters'];
	// $name = $args['name'];
	// $data = explode(',', Option::check_for_option('cdp_theme_options', array('theme','inventory_display','list','search_filters','general','' . $name . '_range')));
	// $content = '';
	// $dataR = array_reverse($data);
	// From
	// $content .= '<div class="search-list search-' . $name . '" >';
	// $content .= '<div class="search-label">' . ucfirst($name) . ' From</div>';
	// $content .= '<div class="search-content from">';
	// $content .= "<select class='input-action select-$name-from' onchange='document.location = this.value;'>";
	// $text = isset( $parameters[$name.'_from'] ) ? 'Remove '.ucfirst($name).' Range' : 'Select '.ucfirst($name).' Range';
	// $content .= "<option value=" . remove_query_arg( array( $name.'_from', $name.'_to' ) ) . ">" . $text . "</option>";
	//
	// if ($data) {
	// foreach ($data as $value) {
	// $number = $value;
	// $value = add_query_arg( array( 'price_from' => $value ) );
	// $selected = isset( $parameters['price_from'] ) ? ( $parameters['price_from'] == $number ? 'selected' : '' ) : '';
	// $content .= '<option value="' . $value . '" '.$selected.'>$' . number_format($number, 0, '.', ',') . '</option>';
	// }
	// }
	// $content .= '</select>';
	// $content .= '</div>';
	// To
	// $content .= '<div class="search-label">' . ucfirst($name) . ' To</div>';
	// $content .= '<div class="search-content to">';
	// $content .= "<select class='input-action select-$name-to' onchange='document.location = this.value;'>";
	// $text = isset( $parameters[ $name.'_to'] ) ? 'Remove '.ucfirst($name).' Range' : 'Select '.ucfirst($name).' Range';
	// $content .= "<option value=" . remove_query_arg( array( $name.'_from', $name.'_to' ) ) . ">" . $text . "</option>";
	//
	// if ($dataR) {
	// foreach ($dataR as $value) {
	// $number = $value;
	// $value = add_query_arg( array( 'price_to' => $value ) );
	// $selected = isset( $parameters['price_to'] ) ? ( $parameters['price_to'] == $number ? 'selected' : '' ) : '';
	// $content .= '<option value="' . $value . '" '.$selected.'>$' . number_format($number, 0, '.', ',') . '</option>';
	// }
	// }
	// $content .= '</select>';
	// $content .= '</div>';
	// $content .= '</div>';
	//
	// return $content;
	// }
	//
	// function range_option($name, $value)
	// {
	// switch ($name) {
	// case 'odometer':
	// $content = '<option value="' . $value . '">' . number_format($value, 0, '', ',') . '</option>';
	// break;
	// case 'price':
	// $content = '<option value="' . $value . '">$' . number_format($value, 0, '.', ',') . '</option>';
	// break;
	// }
	// return $content;
	// }

	public static function vehicleclass_dropdown() {
		$results = ( new Extender_Functions() )->get_list( 'vehicleclass' );
		$content = '<label class="inventory-label">Vehicle Class: </label>';
		$content .= '<select id="inventory-vehicleclass" class="inventory-select" onchange="document.location = this.value;">';
		$content .= '<option data-name="" value="' . esc_url( remove_query_arg( array( 'vehicleclass' ) ) ) . '">All</option>';
		if ( count( $results['data'] ) > 0 ) {
			foreach ( $results['data'] as $value ) {
				if ( $value ) {
					$content .= '<option data-name="' . $value . '"' . ( $results['needle'] === $value ? 'selected' : '' ) . ' value="' . esc_url( add_query_arg( array( 'vehicleclass' => $value ) ) ) . '">';
					$content .= $value;
					$content .= '</option>';
				}
			}
		}
		$content .= '</select>';

		return $content;
	}

	public static function vehicleclass_list( $args ) {
		$parameters = $args['parameters'];
		$url_rule   = $args['url_rule'];
		$results    = ( new Extender_Functions() )->get_list( 'vehicleclass' );
		$content    = '<ul>';
		if ( count( $results['data'] ) > 0 ) {
			foreach ( $results['data'] as $value ) {
				if ( $value ) {
					$link = ( new Fn_Inventory() )->generate_inventory_link( array( 'params'  => $parameters,
					                                                                'include' => array( 'vehicleclass' => rawurlencode( $value ) )
					) );
					// $class = $value == $value ? 'class="active"' : 'class';
					$class   = '';
					$content .= '<li><a href=' . $link . " $class>$value</a></li>";
				}
			}
		}
		$content .= '</ul>';

		return $content;
	}

	public static function geo_dropdown( $args ) {
		$fn_inventory = new Fn_Inventory();
		$dealer_geo   = $args['dealer_geo'];
		$geo_params   = $args['geo_params'];
		$geo_output   = $fn_inventory->build_geo_dropdown(
			$dealer_geo,
			$geo_params,
			Option::check_for_option(
				'cdp_theme_options',
				array(
					'inventory_display',
					'list',
					'search_filters',
					'geo_search',
					'add_geo_zip',
				)
			)
		);
		$content      = '<div id="geo-wrapper">';
		$content      .= ! empty( $geo_output['search'] ) ? $geo_output['search'] : '';
		$content      .= ! empty( $geo_output['dropdown'] ) ? $geo_output['dropdown'] : '';
		$content      .= ! empty( $geo_output['back_link'] ) ? $geo_output['back_link'] : '';
		$content      .= '</div>';

		return $content;

	}

}
