<?php

namespace WoWPress\Frontend\Widgets;

use Peast\Syntax\Node\Class_;
use WoWPress\Frontend\ToggleButton;
use WoWPress\Models\Character;

class CharacterSearch extends Widget
{

	protected $template_path = "char_search.php";
	protected $title = "Charaktersuche";
	protected $base_id = "wowpress_char_search";
	public $name = "Charaktersuche";
	protected $restricted = false;

	protected $classes = [
		'Druid' => [
			'Feral',
			'Balance',
			'Guardian',
			'Restoration',
		],
		'Death Knight' => [
			'Frost',
			'Unholy',
			'Blood'
		],


		'Evoker' => [
			'Augmentation',
			'Devastation',
			'Preservation',
		],
		'Warrior' => [
			'Arms',
			'Fury',
			'Protection',
		],
		'Paladin' => [
			'Holy',
			'Protection',
			'Retribution',
		],
		'Hunter' => [
			'Beast Mastery',
			'Marksmanship',
			'Survival',
		],
		'Rogue' => [
			'Assassination',
			'Outlaw',
			'Subtlety',
		],
		'Priest' => [
			'Discipline',
			'Shadow',
			'Holy',
		],
		'Shaman' => [
			'Elemental',
			'Enhancement',
			'Restoration',
		],
		'Mage' => [
			'Arcane',
			'Fire',
			'Frost',
		],
		'Warlock' => [
			'Affliction',
			'Demonology',
			'Destruction',
		],

		'Monk' => [
			'Brewmaster',
			'Mistweaver',
			'Windwalker',
		],
		'Demon Hunter' => [
			'Havoc',
			'Vengeance',
		],

	];

	public function hasSpec($specs)
	{
		foreach ($specs as $search) {
				if ($search) {
					return true;
				}
			}
		return false;
	}


	public function form($instance)
	{

?>
		<link rel="stylesheet" type="text/css" href="<?= get_template_directory_uri() ?>/style.css">
		<style>
			input[type=radio] {
				display: none !important;
			}
		</style>

		<div class="p-auto">
			<label for="<?php echo esc_attr($this->get_field_id("text")); ?>"><?php echo esc_html__('Freitext', 'wowpress'); ?></label>
			<textarea class="w-full" name="<?php echo esc_attr($this->get_field_name("text")); ?>" id="<?php echo esc_attr($this->get_field_id("text")); ?>" rows="10"><?=$instance['text']?></textarea>
		</div>

		<div class="flex flex-col gap-2 p-auto">
			<?php
			foreach ($this->classes as $class => $specs) {
				$classname = sanitize($class);

			?>
				<div class="bg-<?= $classname ?> bg-opacity-25 p-auto flex flex-col gap-2">
					<div class="text-center flex flex-col justify-center items-center p-auto">

						<img src="<?= Character::classIconLink($class) ?>" alt="" class="h-8">
						<h2 class="text-xl font-bold text-<?= $classname ?>"><?= translate_string($class) ?></h2>
					</div>
					<?php
					foreach ($specs as $spec) {
						$specname = sanitize($spec);
					?>
						<div class="grid grid-cols-5 gap-2 p-auto border border-<?= $classname ?>">

							<div class="col-span-5">
								<h3 class="text-lg font-bold text-<?= $classname ?>"><?= translate_string($spec) ?></h3>
							</div>

							<img src="<?= Character::specIconLink($class, $spec) ?>" alt="" class="">

							<?php
							(new ToggleButton([
								'type' => 'radio',
								'name' => esc_attr($this->get_field_name("classes[$class][$spec]]")),
								'id' => esc_attr($this->get_field_id("classes[$class][$spec]]")) . "_0",
								'value' => 0,
								'text' => "Kein Bedarf",
								'color' => $classname,
								'icon_yes' => 'o-check',
								'checked' => empty($instance['classes'][$class][$spec]),
								'row' => false,
								'fullHeight' => true,
							]))->render();
							?>
							<?php
							(new ToggleButton([
								'type' => 'radio',
								'name' => esc_attr($this->get_field_name("classes[$class][$spec]]")),
								'id' => esc_attr($this->get_field_id("classes[$class][$spec]]")) . "_1",
								'value' => 1,
								'text' => "Niedrig",
								'color' => $classname,
								'icon_yes' => 'o-check',
								'checked' => $instance['classes'][$class][$spec] == 1,
								'row' => false,
								'fullHeight' => true,
							]))->render();
							?>
							<?php
							(new ToggleButton([
								'type' => 'radio',
								'name' => esc_attr($this->get_field_name("classes[$class][$spec]]")),
								'id' => esc_attr($this->get_field_id("classes[$class][$spec]]")) . "_2",
								'value' => 2,
								'text' => "Mittel",
								'color' => $classname,
								'icon_yes' => 'o-check',
								'checked' => $instance['classes'][$class][$spec] == 2,
								'row' => false,
								'fullHeight' => true,
							]))->render();
							?>
							<?php
							(new ToggleButton([
								'type' => 'radio',
								'name' => esc_attr($this->get_field_name("classes[$class][$spec]]")),
								'id' => esc_attr($this->get_field_id("classes[$class][$spec]]")) . "_3",
								'value' => 3,
								'text' => "Hoch",
								'color' => $classname,
								'icon_yes' => 'o-check',
								'checked' => $instance['classes'][$class][$spec] == 3,
								'row' => false,
								'fullHeight' => true,
							]))->render();
							?>
						</div>

					<?php
					}
					?>
				</div>
			<?php
			}
			?>
		</div>


<?php
	}

	public function update($new_instance, $old_instance)
	{
		$instance          = array();
		$instance['text'] = (!empty($new_instance['text'])) ? strip_tags($new_instance['text']) : '';
		foreach ($this->classes as $class => $specs) {
			foreach ($specs as $spec) {
				$instance['classes'][$class][$spec] = (!empty($new_instance['classes'][$class][$spec])) ? strip_tags($new_instance['classes'][$class][$spec]) : '';
			}
		}

		return $instance;
	}
}