<?php
/**
 * Handles form data and saving for PowerWidget Appearance tab.
 *
 * @package Ilabs\Firevert\Modules\PowerWidget\Services\Form
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Modules\PowerWidget\Services\Form;

use Ilabs\Firevert\Core\Settings\Form\DefaultSettingsProviderInterface;

/**
 * AppearanceFormHandler class.
 */
class AppearanceFormHandler extends AbstractFormHandler implements DefaultSettingsProviderInterface {

	/**
	 * Current section slug.
	 *
	 * @var string
	 */
	protected string $section_slug = 'appearance';

	/**
	 * Fields and types used in this form.
	 *
	 * @var array<string, string>
	 */
	protected array $fields = array(
		'firevert_power_widget_text'               => 'html',
		'firevert_power_widget_type'               => 'text',
		'firevert_power_widget_image'              => 'int',
		'firevert_power_widget_shadow'             => 'bool',
		'firevert_power_widget_bg_color'           => 'text',
		'firevert_power_widget_primary_color'      => 'text',
		'firevert_power_widget_primary_text_color' => 'text',
		'firevert_power_widget_counter_bg'         => 'text',
		'firevert_power_widget_counter_text'       => 'text',
		'firevert_power_widget_toggle_open'        => 'text',
		'firevert_power_widget_toggle_close'       => 'text',
		'firevert_power_widget_border_radius'      => 'int',
	);

	/**
	 * Returns translatable fields used in this form.
	 *
	 * @return array<string>
	 */
	public function get_translatable_fields(): array {
		return array(
			'firevert_power_widget_text',
			'firevert_power_widget_toggle_open',
			'firevert_power_widget_toggle_close',
		);
	}

	/**
	 * Returns data to render in the appearance view.
	 *
	 * @return array<string, mixed>
	 */
	public function get_data(): array {
		$data = $this->get_all_field_data();

		$image_id                                = (int) $data['firevert_power_widget_image'];
		$data['firevert_power_widget_image_id']  = $image_id;
		$data['firevert_power_widget_image_url'] = $image_id
			? wp_get_attachment_url( $image_id )
			: '';

		return $data;
	}

	/**
	 * Returns default settings data.
	 *
	 * @return array<string, mixed>
	 */
	public function get_default_settings(): array {
		return array(
			'firevert_power_widget_text'               => '<strong>00%</strong> discount on all products',
			'firevert_power_widget_type'               => 'text',
			'firevert_power_widget_shadow'             => true,
			'firevert_power_widget_bg_color'           => '#191A1F',
			'firevert_power_widget_primary_color'      => '#107EF3',
			'firevert_power_widget_primary_text_color' => '#FFFFFF',
			'firevert_power_widget_counter_bg'         => '#107EF3',
			'firevert_power_widget_counter_text'       => '#FFFFFF',
			'firevert_power_widget_toggle_open'        => 'Expand',
			'firevert_power_widget_toggle_close'       => 'Collapse',
			'firevert_power_widget_border_radius'      => 20,
		);
	}
}
