<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\UI\Widgets;

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

use Signocore\Multilanguage\Application\Constants;
use Signocore\Multilanguage\Features\LanguageSwitcher as LanguageSwitcherFeature;
use WP_Widget;

/**
 * Classic widget for the language switcher.
 */
final class LanguageSwitcher extends WP_Widget
{
	public function __construct()
	{
		parent::__construct(
			'sml_language_switcher',
			__('Signocore Language Switcher', 'signocore-multilanguage'),
			[
				'description' => __('Display a language switcher for visitors to change languages.', 'signocore-multilanguage'),
				'classname' => 'widget_sml_language_switcher',
			]
		);
	}

	/**
	 * Output the widget content.
	 *
	 * @param array<string, string> $args
	 * @param array{title?: string, style?: string, show_flags?: string, show_names?: string} $instance
	 */
	public function widget($args, $instance): void
	{
		echo $args['before_widget'] ?? '';

		$title = !empty($instance['title']) ? $instance['title'] : '';
		$title = apply_filters('widget_title', $title, $instance, $this->id_base);

		if ($title !== '') {
			echo ($args['before_title'] ?? '') . esc_html($title) . ($args['after_title'] ?? '');
		}

		$switcher = LanguageSwitcherFeature::load();

		echo $switcher->render([
			'style' => $instance['style'] ?? 'dropdown',
			'show_flags' => !isset($instance['show_flags']) || $instance['show_flags'] === '1',
			'show_names' => !isset($instance['show_names']) || $instance['show_names'] === '1',
		]);

		echo $args['after_widget'] ?? '';
	}

	/**
	 * Output the widget settings form in admin.
	 *
	 * @param array{title?: string, style?: string, show_flags?: string, show_names?: string} $instance
	 */
	public function form($instance): void
	{
		$title = $instance['title'] ?? '';
		$style = $instance['style'] ?? 'dropdown';
		$showFlags = !isset($instance['show_flags']) || $instance['show_flags'] === '1';
		$showNames = !isset($instance['show_names']) || $instance['show_names'] === '1';

		$styles = [
			'dropdown' => __('Dropdown', 'signocore-multilanguage'),
			'list' => __('List', 'signocore-multilanguage'),
			'flags' => __('Flags Only', 'signocore-multilanguage'),
			'inline' => __('Inline', 'signocore-multilanguage'),
		];

		?>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
				<?php echo esc_html__('Title:', 'signocore-multilanguage'); ?>
			</label>
			<input class="widefat"
				   id="<?php echo esc_attr($this->get_field_id('title')); ?>"
				   name="<?php echo esc_attr($this->get_field_name('title')); ?>"
				   type="text"
				   value="<?php echo esc_attr($title); ?>">
		</p>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id('style')); ?>">
				<?php echo esc_html__('Style:', 'signocore-multilanguage'); ?>
			</label>
			<select class="widefat"
					id="<?php echo esc_attr($this->get_field_id('style')); ?>"
					name="<?php echo esc_attr($this->get_field_name('style')); ?>">
				<?php foreach ($styles as $value => $label) : ?>
					<option value="<?php echo esc_attr($value); ?>" <?php selected($style, $value); ?>>
						<?php echo esc_html($label); ?>
					</option>
				<?php endforeach; ?>
			</select>
		</p>
		<p>
			<input class="checkbox"
				   type="checkbox"
				   id="<?php echo esc_attr($this->get_field_id('show_flags')); ?>"
				   name="<?php echo esc_attr($this->get_field_name('show_flags')); ?>"
				   value="1"
				   <?php checked($showFlags); ?>>
			<label for="<?php echo esc_attr($this->get_field_id('show_flags')); ?>">
				<?php echo esc_html__('Show flags', 'signocore-multilanguage'); ?>
			</label>
		</p>
		<p>
			<input class="checkbox"
				   type="checkbox"
				   id="<?php echo esc_attr($this->get_field_id('show_names')); ?>"
				   name="<?php echo esc_attr($this->get_field_name('show_names')); ?>"
				   value="1"
				   <?php checked($showNames); ?>>
			<label for="<?php echo esc_attr($this->get_field_id('show_names')); ?>">
				<?php echo esc_html__('Show language names', 'signocore-multilanguage'); ?>
			</label>
		</p>
		<?php
	}

	/**
	 * Sanitize widget form values.
	 *
	 * @param array<string, string> $newInstance
	 * @param array<string, string> $oldInstance
	 * @return array<string, string>
	 */
	public function update($newInstance, $oldInstance): array
	{
		return [
			'title' => sanitize_text_field($newInstance['title'] ?? ''),
			'style' => in_array($newInstance['style'] ?? '', ['dropdown', 'list', 'flags', 'inline'], true)
				? $newInstance['style']
				: 'dropdown',
			'show_flags' => !empty($newInstance['show_flags']) ? '1' : '0',
			'show_names' => !empty($newInstance['show_names']) ? '1' : '0',
		];
	}
}
