<?php
/**
 * Handles form data and saving for PowerWidget Settings 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;
use Ilabs\Firevert\Modules\PowerWidget\Services\Type\PositionType;

/**
 * Class SettingsFormHandler
 */
class SettingsFormHandler extends AbstractFormHandler implements DefaultSettingsProviderInterface {

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

	/**
	 * Fields and types used in this form.
	 *
	 * @var array<string, string>
	 */
	protected array $fields = array(
		'firevert_power_widget_visible_admin_only' => 'bool',
		'firevert_power_widget_display_position'   => 'text',
		'firevert_power_widget_display_start'      => 'text',
		'firevert_power_widget_display_end'        => 'text',
		'firevert_power_widget_display_timer'      => 'bool',
		'firevert_power_widget_display_desktop'    => 'bool',
		'firevert_power_widget_display_mobile'     => 'bool',
		'firevert_power_widget_expand_mobile'      => 'bool',
	);

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

	/**
	 * Returns default settings data.
	 *
	 * @return array<string, mixed>
	 */
	public function get_default_settings(): array {
		$now       = current_time( 'Y-m-d\TH:i' );
		$timestamp = time();
		$end_time  = gmdate( 'Y-m-d\TH:i', $timestamp + WEEK_IN_SECONDS );

		return array(
			'firevert_power_widget_visible_admin_only' => true,
			'firevert_power_widget_display_position'   => PositionType::BOTTOM_RIGHT,
			'firevert_power_widget_display_start'      => $now,
			'firevert_power_widget_display_end'        => $end_time,
			'firevert_power_widget_display_timer'      => true,
			'firevert_power_widget_display_desktop'    => true,
			'firevert_power_widget_display_mobile'     => true,
			'firevert_power_widget_expand_mobile'      => true,
		);
	}
}
