<?php
/** Options
 *
 * @package BwWinnersGlobalSite
 */

namespace BwWinnersGlobalSite;

if ( ! class_exists( __NAMESPACE__ . '\Options' ) ) {
	/**
	 * Manages options used to configure the plugin.
	 */
	class Options {
		/**
		 * WordPress option name for BwWinnersGlobalSite options
		 *
		 * @var array
		 */
		public static $option = 'bw_winners_v2_options_site';

		/**
		 * BwWinnersGlobalSite allowed options
		 *
		 * @var array
		 */
		protected static $keys = [
			'import_award_as' => [
				'double_gold',
				'gold',
				'silver',
				'bronze'
			],
			'display_score_as' => [
				'double_gold',
				'gold',
				'silver',
				'bronze'
			],
			'database_version',
			'google_sheets',
			'enable_auto_sync'
		];

		/**
		 * BwWinnersGlobalSite option default values
		 *
		 * @var array
		 */
		public static $defaults = [
			'import_award_as' => [
				'double_gold' => 96,
				'gold' => 94,
				'silver' => 92,
				'bronze' => 90
			],
			'display_score_as' => [
				'double_gold' => 96,
				'gold' => 94,
				'silver' => 92,
				'bronze' => 86
			],
			'database_version' => '0.0.0',
			'google_sheets' => [],
			'enable_auto_sync' => false
		];
		/**
		 * Gets the BwWinnersGlobalSite options.
		 *
		 * @return array BwWinnersGlobalSite options.
		 */
		public static function get_options() {
			$options = get_option( self::$option, null );

			if ( ! is_array( $options ) ) {
				return self::$defaults;
			}

			foreach ( self::$defaults as $key => $value ) {
				if ( ! isset( $options[ $key ] ) ) {
					$options[ $key ] = $value;
				}
			}
			return $options;
		}

		/**
		 * Updates the BwWinnersGlobalSite options
		 *
		 * @param array $update Options to update.
		 *
		 * @return array BwWinnersGlobalSite options.
		 */
		public static function update_options( $update ) {
			$options = self::get_options();


			foreach ( self::$keys as $index => $key ) {

				if ( is_array( $key ) ) {
					foreach ( $key as $innerKey ) {
						if ( isset( $update[ $index ][ $innerKey ] ) ) {
							$options[ $index ][ $innerKey ] = $update[ $index ][ $innerKey ];
						}
					}
				} else if ( isset( $update[ $key ] ) ) {
					$options[ $key ] = $update[ $key ];
				}
			}

			update_option( self::$option, $options );
			return $options;
		}

		/**
		 * Gets a single BwWinnersGlobalSite option
		 *
		 * @param string $key  The option name.
		 *
		 * @return mixed
		 */
		public static function get_option( $key, $default = null ) {
			if ( ! in_array( $key, self::$keys ) ) {
				return $default;  // Invalid key protection
			}
			$options = self::get_options();
			return isset( $options[ $key ] ) ? $options[ $key ] : $default;
		}

		/**
		 * Updates a single BwWinnersGlobalSite option
		 *
		 * @param string $key  The option name.
		 * @param mixed  $value The option value.
		 */
		public static function update_option( $key, $value ) {
			$options         = self::get_options();
			$options[ $key ] = $value;
			self::update_options( $options );
		}

		/**
		 * Deletes a single BwWinnersGlobalSite option
		 *
		 * @param string $key  The option name.
		 */
		public static function delete_option( $key ) {
			$options = self::get_options();
			unset( $options[ $key ] );
			self::update_options( $options );
		}
	}
}
