<?php

namespace CarDealerPress\Templates\Admin\Theme;

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

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

function tags(): string {

	$option = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'inventory_display', 'tags', 'custom' ) );
	$count  = Generic::get_count_of( $option );

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}

	$content = '<section id="tags">';
	$content .= '<div><h3>Tag Settings</h3></div>';
	$content .= '<table>';
	$content .= '<tbody id="" class="tags">';
	$checked = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'tags',
			'display_tags',
		)
	);
	$content .= table_rows::tr_setting_checkbox(
		array(
			'id'      => 'display-tags',
			'label'   => 'Display Tags:',
			'name'    => 'cdp_theme_options[theme][inventory_display][tags][display_tags]',
			'checked' => $checked,
		)
	);
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</section>';

	$content .= exclude_tags();
	$content .= '<hr>';
	if ( ! empty( $option ) ) {
		$content .= current_tags( $option );
		$content .= '<hr>';
	}
	$content .= create_tag( 'Create or override default tag image', $row_id );

	return $content;
}

function exclude_tags(): string {
	$exclude_tags = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'tags',
			'exclude',
		),
		array()
	);

	$tags = ( new Requests() )->cdp_db_init( 'tags', array() );

	$content = '<section id="exclude_tags">';
	$content .= '<div><h3>Exclude Tags</h3></div>';
	$content .= '<table>';
	$content .= '<tbody id="" class="exclude-tags">';

	$content .= '<tr>';
	$content .= '<th><label>Exclude Tags</label></th>';
	$content .= '<td>';
	$content .= '<select id="excludeTagsSelect2" class="basic-multiple" name="cdp_theme_options[theme][inventory_display][tags][exclude][]" multiple="multiple">';
	if ( ! empty( $tags ) && is_array( $tags ) ) {
		foreach ( $tags as $tag ) {
			$is_selected = ( ! empty( $exclude_tags ) && in_array( $tag, $exclude_tags, true ) ) ? ' selected="selected"' : '';
			$safe_tag = htmlspecialchars( $tag, ENT_QUOTES, 'UTF-8' );
			$content .= "<option value='$safe_tag'$is_selected>$safe_tag</option>";
		}
	}
	$content .= '</select>';
	$content .= '</td>';
	$content .= '</tr>';

	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</section>';

	return $content;
}

function current_tags( $option ) {
	$content = '<section id="current">';
	$content .= '<div><h3>Current Tags</h3></div>';
	$content .= '<div id="tagRows" class="inner-table-content">';
	$content .= '<table id="current-tags-table">';
	foreach ( $option as $row_id => $item ) {
		$name    = $item['name'];
		$content .= "<tr id='outer-$row_id' data-tag='outer-$row_id' class='outer-row' data-label-name='$name' data-option='theme' data-tab='inventory_display' data-section='tags' data-section1='custom'></tr>";
		$content .= "<td>$name";
		$content .= "<input id='edit-$row_id' class='edit-row' data-row-id='$row_id' type='button' value='Edit'/>";
		$content .= "<input id='removeTableRow' class='delete-row' data-row-id='$row_id' type='button' value='Delete'/><td>";
		$content .= '</tr>';
		$content .= "<tr id='inner-$row_id' tag='inner-$row_id' class='detail inner-row'>";
		$content .= '<td>';
		$content .= '<div class="">';
		$content .= '<table class="table-wrap current">';
		$content .= '<tbody>';
		$content .= tag_row( $row_id, true );
		$content .= '</tbody>';
		$content .= '</table>';
		$content .= '</div>';
		$content .= '</td>';
		$content .= '</tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</div>';
	$content .= '</section>';

	return $content;

}

function create_tag( $title, $row_id ): string {
	$content = "<section id='new-tag'>";
	$content .= "<h3>$title</h3>";
	$content .= "<input id='addNewTableButton' class='add-row toggle-row' type='button' value='Add +'/>";
	$content .= "<div class='table-content' style='display:none;'></div>";
	$script  = '<script>jQuery("#addNewTableButton").click(function (e) { jQuery(".table-content").append("' . table_tag( $row_id ) . '"); });</script>';
	$content .= $script;
	$content .= '</section>';

	return $content;
}

function table_tag( $row_id ) {
	$content = "<table id='addNewTable' class='table-wrap add'>";
	$content .= '<tbody>';
	$content .= tag_row( $row_id );
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function tag_row( $row_id, $show = false ) {
	$content      = '';
	$exclude_tags = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'tags',
			'exclude',
		),
		array()
	);
	$tags         = ( new Requests() )->cdp_db_init( 'tags', array() );

	foreach ( $tags as $tag ) {
		if ( $tag && ! in_array( $tag, $exclude_tags, true ) ) {
			$not_excluded[] = $tag;
		}
	}
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'tags',
			'custom',
			$row_id,
			'name',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Tag',
			'id'    => "tags-$row_id-page",
			'name'  => 'cdp_theme_options[theme][inventory_display][tags][custom][' . $row_id . '][name]',
			'value' => $value,
			'list'  => $not_excluded,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'tags',
			'custom',
			$row_id,
			'order',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Order',
			'id'    => "sort-value-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][tags][custom][' . $row_id . '][order]',
			'value' => $value,
			'type'  => 'number',
			'class' => 'input-medium',
			'min'   => '0',
		)
	);

	$url = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'tags',
			'custom',
			$row_id,
			'url',
		)
	);

	$content .= "<tr class='cdp-row'><th><label><a id='media-$row_id' href='#' class='custom_media_upload tag-$row_id'>Upload:</a></label></th>";
	$content .= "<td><input name='cdp_theme_options[theme][inventory_display][tags][custom][$row_id][url]' type='text' class='cdp-input custom_media_url media-' value='$url'/></td>";
	$content .= '</tr>';

	$content .= "<tr class='cdp-row'>";
	$content .= '<th><label>Icon</label></th>';
	// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
	$content .= "<td><img class='custom_media_image tag media-$row_id' src='$url' alt='No Image Selected'/></td>";
	$content .= '</tr>';

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'tags',
			'custom',
			$row_id,
			'link',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Link',
			'id'    => "custom-$row_id-link",
			'name'  => 'cdp_theme_options[theme][inventory_display][tags][custom][' . $row_id . '][link]',
			'value' => $value,
			'class' => 'input-long',
		)
	);

	$content .= table_rows::edit_update( $show, $row_id );

	return $content;
}
