<?php

namespace CarDealerPress\Includes\Functions;

use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;

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

class Fn_Views {
	public static function admin_get_custom_dealers( $dealer ) {
		$content = '';
		$company = helpers::company_id();
		if ( $dealer ) {
			$content .= '<div id="import-dealer-' . $dealer['dealer_id'] . '" class="import-inner-td-full">';
			$content .= '<div class="import-dealer-headline">';
			$content .= '<div class="import-dealer-id">VMS ID: ' . $dealer['dealer_id'] . '</div>';
			$content .= '<div class="import-dealer-name">' . $dealer['name'] . '</div>';
			if ( $dealer['dealer_id'] !== $company ) {
				$content .= '<div data-value="' . $dealer['dealer_id'] . '" class="import-remove-dealer-button">Delete Dealer [X]</div>';
			}
			$content .= '</div>';
			$content .= '<div class="import-dealer-content">';
			$content .= '<div class="import-dealer-custom-imports">';
			$content .= self::admin_get_custom_imports( $dealer['dealer_id'] );
			$content .= '</div>';
			$content .= '</div>';
			$content .= '</div>';
		}

		return $content;
	}

	public static function admin_get_custom_imports( $id = 0 ) {
		$content = '';
		if ( $id ) {
			$imports = Option::get_cdp_admin_options()['admin']['advanced']['import_filters'];
			if ( $imports ) {
				foreach ( $imports as $key => $import ) {
					$content .= '<div id="import-custom-content-' . $key . '" class="import-dealer-custom-content">';
					$content .= '<div class="import-saleclass-glance" data-value="' . $import['saleclass'] . '">Saleclass: ' . ( empty( $import['saleclass'] ) ? 'All' : ucfirst( $import['saleclass'] ) ) . '</div>';
					$content .= '<div class="import-make-glance">';
					$content .= 'Make: ';
					$content .= '<span class="import-make-glance-symbol" data-value="' . $import['make_qualifier'] . '">' . $import['make_qualifier'] . ' </span>';
					$content .= '<span class="import-make-glance-value" data-value="' . implode( ', ', $import['makes'] ) . '">' . ( empty( $import['makes'] ) ? 'All' : implode( ', ', $import['makes'] ) ) . '</span>';
					$content .= '</div>';
					$content .= '<div class="import-year-glance">';
					$content .= 'Year: ';
					$content .= '<span class="import-year-glance-symbol" data-value="' . $import['year_qualifier'] . '">' . $import['year_qualifier'] . ' </span>';
					$content .= '<span class="import-year-glance-value" data-value="' . $import['years'] . '">' . ( empty( $import['years'] ) ? 'All' : $import['years'] ) . '</span>';
					$content .= '</div>';
					$content .= '</div>';
				}
			} else {
				$content .= '<div class="import-dealer-custom-content not-found">Currently no custom imports have been created for this dealer.</div>';
			}
		}

		return $content;
	}

	public static function check_for_custom_import_filters( $args ) {
		$id = $args['id'] ?? 0;
		if ( $id ) {
			$imports = Option::get_cdp_admin_options()['admin']['advanced']['import_filters'];
			if ( $imports ) {
				foreach ( $imports as $key => $import ) {
					if ( $import['dealer_id'] === $id ) {
						return array(
							'string'  => '<div class="custom-filter-notice"><span>**</span>Custom Filter Detected<span>**</span></div>',
							'key'     => $key,
							'filters' => $import,
						);
					}
				}
			} else {
				return array( 'string' => null );
			}
		}
	}

	public static function array_multi_search( array $array, array $pairs ) {
		$found = array();
		foreach ( $array as $a_key => $a_val ) {
			$coincidences = 0;
			foreach ( $pairs as $p_key => $pVal ) {
				if ( array_key_exists( $p_key, $a_val ) && $a_val[ $p_key ] === $pVal ) {
					$coincidences ++;
				}
			}
			if ( $coincidences === count( $pairs ) ) {
				$found[ $a_key ] = $a_val;
			}
		}

		return $found;
	}


}
