<?php
/**
 * Button Text Manager.
 *
 * Handles plugin settings and button text retrieval.
 *
 * @package WooCommerce_Button_Text_Changer
 */

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


/**
 * Button Text Changer Manager.
 */
final class WBTC_Button_Text_Changer {

	/**
	 * Singleton instance.
	 *
	 * @var WBTC_Button_Text_Changer|null
	 */
	private static $instance = null;


	/**
	 * Option name.
	 *
	 * @var string
	 */
	private $option_name = 'wbtc_settings';


	/**
	 * Default settings.
	 *
	 * @var array
	 */
	private $defaults = array(
		'enabled'     => 'yes',
		'add_to_cart' => '',
		'view_cart'   => '',
		'update_cart' => '',
		'checkout'    => '',
		'place_order' => '',
		'read_more'   => '',
	);


	/**
	 * Get singleton instance.
	 *
	 * @return WBTC_Button_Text_Changer
	 */
	public static function instance() {

		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}


	/**
	 * Constructor.
	 */
	private function __construct() {
		// Settings are loaded on demand.
	}


	/**
	 * Get all settings.
	 *
	 * Missing settings are automatically filled
	 * from the default configuration.
	 *
	 * @return array
	 */
	public function get_settings() {

		$settings = get_option(
			$this->option_name,
			array()
		);

		if ( ! is_array( $settings ) ) {
			$settings = array();
		}

		return wp_parse_args(
			$settings,
			$this->defaults
		);
	}


	/**
	 * Get one setting.
	 *
	 * @param string $key Setting key.
	 * @return string
	 */
	public function get_setting( $key ) {

		$settings = $this->get_settings();

		if ( ! array_key_exists( $key, $settings ) ) {
			return '';
		}

		return (string) $settings[ $key ];
	}


	/**
	 * Check whether plugin is enabled.
	 *
	 * @return bool
	 */
	public function is_enabled() {

		return 'yes' === $this->get_setting( 'enabled' );
	}


	/**
	 * Get button text.
	 *
	 * @param string $button Button key.
	 * @return string
	 */
	public function get_button_text( $button ) {

		$allowed_buttons = array(
			'add_to_cart',
			'view_cart',
			'update_cart',
			'checkout',
			'place_order',
			'read_more',
		);

		if ( ! in_array( $button, $allowed_buttons, true ) ) {
			return '';
		}

		return trim(
			$this->get_setting( $button )
		);
	}


	/**
	 * Update settings.
	 *
	 * Only known settings are saved.
	 *
	 * @param array $settings New settings.
	 * @return bool
	 */
	public function update_settings( $settings ) {

		if ( ! is_array( $settings ) ) {
			return false;
		}

		$current = $this->get_settings();

		$allowed_fields = array(
			'enabled',
			'add_to_cart',
			'view_cart',
			'update_cart',
			'checkout',
			'place_order',
			'read_more',
		);

		foreach ( $allowed_fields as $field ) {

			if ( ! array_key_exists( $field, $settings ) ) {
				continue;
			}

			if ( 'enabled' === $field ) {

				$current[ $field ] =
					'yes' === $settings[ $field ]
						? 'yes'
						: 'no';

				continue;
			}

			$current[ $field ] =
				sanitize_text_field(
					$settings[ $field ]
				);
		}

		return update_option(
			$this->option_name,
			$current
		);
	}


	/**
	 * Reset settings to defaults.
	 *
	 * @return bool
	 */
	public function reset_settings() {

		return update_option(
			$this->option_name,
			$this->defaults
		);
	}


	/**
	 * Get default settings.
	 *
	 * @return array
	 */
	public function get_defaults() {

		return $this->defaults;
	}
}