<?php

namespace CarDealerPress\Templates\Admin\Theme;

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

use CarDealerPress\Includes\Database\Requests;
use CardealerPress\Includes\Helpers\Option;
use CarDealerPress\templates\partials\admin\Table_Rows;

function keywords(): string {

	$content = '<table>';
	$content .= '<tbody>';
	$content .= '<tr><th><h2>Type Ahead Keywords</h2></th></tr>';
	$content .= keywords_options();
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function keywords_options() {
	$checked = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'keywords',
			'enable',
		)
	);
	$content = table_rows::tr_setting_checkbox(
		array(
			'id'      => 'enable-keywords',
			'label'   => 'Enable Type Ahead:',
			'name'    => 'cdp_theme_options[theme][inventory_display][list][keywords][enable]',
			'checked' => $checked,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'keywords',
			'add',
		)
	);
	$content .= table_rows::tr_setting_textarea(
		array(
			'label' => 'Add Keywords:',
			'id'    => 'add_keywords',
			'name'  => 'cdp_theme_options[theme][inventory_display][list][keywords][add]',
			'notes' => '(comma separated)',
			'value' => $value,
		)
	);

	$content .= exclude_keywords();
	$content .= keywords_pot();

	return $content;
}

function exclude_keywords(): string {
	$exclude_keywords = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'keywords',
			'exclude',
		),
		array()
	);
	$content          = '<tr class="cdp-row">';
	$content          .= '<th><label>Excluded</label></th>';
	$content          .= '<td>';
	$content          .= '<select class="basic-multiple keywords exclude" name="cdp_theme_options[theme][inventory_display][list][keywords][exclude][]" multiple="multiple" data-keyword="exclude">';
	if ( $exclude_keywords ) {
		foreach ( $exclude_keywords as $keyword ) {
			if ( $keyword ) {
				$is_selected = ( in_array( $keyword, $exclude_keywords, true ) ) ? ' selected="selected"' : '';
				$safe_tag = htmlspecialchars( $keyword, ENT_QUOTES, 'UTF-8' );
				$content .= "<option value='$safe_tag'$is_selected>$safe_tag</option>";
			}
		}
	}
	$content .= '</select>';
	$content .= '</td>';
	$content .= '</tr>';

	return $content;
}

function keywords_pot(): string {
	$exclude_keywords = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'keywords',
			'exclude',
		),
		array()
	);
	$keywords_db      = ( new Requests() )->cdp_db_init( 'keywords' );

	$keywords        = array_diff( $keywords_db, $exclude_keywords ); // remove excluded keywords.
	$added_keywords  = append_additional_keywords();
	$keywords_merged = array_merge( $keywords, $added_keywords );

	$content = '<tr class="cdp-row">';
	$content .= '<th><label>Keyword Pot</label></th>';
	$content .= '<td>';
	$content .= '<select class="basic-multiple keywords pot" name="cdp_theme_options[theme][inventory_display][list][keywords][pot][]" 
multiple="multiple" data-keyword="pot" style="width:90%;">';
	if ( $keywords_merged ) {
		foreach ( $keywords_merged as $keyword ) {
			if ( $keyword ) {
				$selected = ! empty( $exclude_keywords ) ? ( in_array( $keyword, $exclude_keywords, true ) ? '' : 'selected' ) : 'selected';
				$content  .= '<label for=' . $keyword . '>';
				$content  .= '<option value="' . $keyword . '" ' . $selected . '/>' . $keyword . '</option>';
			}
		}
	}
	$content .= '</select>';
	$content .= '</td>';
	$content .= '<td>Remove to make excluded.</td>';
	$content .= '</tr>';

	return $content;
}

function append_additional_keywords(): array {
	$keywords   = array();
	$add_string = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'keywords',
			'add',
		)
	);
	if ( ! empty( $add_string ) ) {
		$add = explode( ',', $add_string );
		foreach ( $add as $new ) {
			$keywords[] = trim( $new );
		}
	}

	return $keywords;

}
