<?php

/**
 * Main plugin class that initializes all plugin components.
 *
 * @package FormEnhancer
 */

namespace FormEnhancer;

use FormEnhancer\FluentForm\Integration as FluentFormIntegration;
use FormEnhancerPro\FluentForm\Fields\EmailEnhanced;

/**
 * Main plugin class that initializes all plugin components.
 */
class Plugin
{
	/**
	 * Admin instance.
	 *
	 * @var Admin
	 */
	private Admin $admin;

	/**
	 * FluentForm integration instance.
	 *
	 * @var FluentFormIntegration
	 */
	private FluentFormIntegration $fluentFormIntegration;

	/**
	 * Settings instance.
	 *
	 * @var Settings
	 */
	private Settings $settings;

	/**
	 * Plugin constructor.
	 * All dependencies are injected via DI container.
	 *
	 * @param Admin $admin Admin instance.
	 * @param FluentFormIntegration $fluentFormIntegration FluentForm integration instance.
	 * @param Settings $settings Settings instance.
	 */
	public function __construct(Admin $admin, FluentFormIntegration $fluentFormIntegration, Settings $settings)
	{
		$this->admin = $admin;
		$this->fluentFormIntegration = $fluentFormIntegration;
		$this->settings = $settings;
	}

	/**
	 * Method called on plugin init.
	 *
	 * @return void
	 *
	 * @throws Exception
	 */
	public function init(): void
	{

		$this->admin->setup();
		$this->settings->setup();
		$this->fluentFormIntegration->setup();
	}

	/**
	 * Method called on plugin activation.
	 *
	 * @param bool $network_wide activated network wide.
	 *
	 * @return void
	 *
	 * @throws Exception
	 */
	public function activate(bool $network_wide): void
	{
		$this->init();

		// Update existing email fields with new settings.
		if (\formenhancer_fs()->is__premium_only() && \formenhancer()->get_settings(Settings::FF_FIELD_EMAIL_ENHANCED)) {
			EmailEnhanced::updateExistingEmailFields();
		}
	}

	/**
	 * Method called on plugin deactivation.
	 *
	 * @param bool $network_wide deactivated network wide.
	 *
	 * @return void
	 */
	public function deactivate(bool $network_wide): void
	{

		// Stuff to do on deactivation.
	}

	/**
	 * Method called on plugin uninstall.
	 *
	 * @return void
	 */
	public function uninstall(): void
	{

		$this->settings->uninstall();
		\delete_option('_formenhancer_ecomail_details');
	}

	/**
	 * Get settings.
	 *
	 * @param string|null $key Settings key to retrieve. If null, returns all settings.
	 * @return mixed
	 */
	public function get_settings($key = null)
	{
		return $this->settings->get_setting($key);
	}
}
