<?php
/**
 * Settings storage.
 *
 * @package FailedOrderRescueForWooCommerce
 */

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

class FORD_Settings {
	/**
	 * Default settings.
	 *
	 * @return array
	 */
	public static function defaults() {
		$defaults = array(
			'enabled_statuses'     => array( 'failed', 'pending' ),
			'email_subject'        => __( 'Complete your order at {site_name}', 'failed-order-rescue-for-woocommerce' ),
			'email_heading'        => __( 'Your order is waiting for payment', 'failed-order-rescue-for-woocommerce' ),
			'email_body'           => __( 'Hi {customer_name}, your order #{order_number} was not completed. You can safely retry payment using the button below.', 'failed-order-rescue-for-woocommerce' ),
			'from_name'            => '',
			'from_email'           => '',
			'delete_data_on_uninstall' => 'no',
		);

		if ( class_exists( 'FORD_Pro' ) ) {
			$defaults += FORD_Pro::defaults();
		}

		return $defaults;
	}

	/**
	 * Register settings.
	 *
	 * @return void
	 */
	public static function init() {
		add_action( 'admin_init', array( __CLASS__, 'register' ) );
	}

	/**
	 * Register option.
	 *
	 * @return void
	 */
	public static function register() {
		register_setting(
			'ford_settings_group',
			'ford_settings',
			array(
				'type'              => 'array',
				'sanitize_callback' => array( __CLASS__, 'sanitize' ),
				'default'           => self::defaults(),
			)
		);
	}

	/**
	 * Get all settings.
	 *
	 * @return array
	 */
	public static function get_all() {
		$settings = get_option( 'ford_settings', array() );

		return wp_parse_args( is_array( $settings ) ? $settings : array(), self::defaults() );
	}

	/**
	 * Get a single setting.
	 *
	 * @param string $key Setting key.
	 * @return mixed
	 */
	public static function get( $key ) {
		$settings = self::get_all();

		return isset( $settings[ $key ] ) ? $settings[ $key ] : null;
	}

	/**
	 * Sanitize settings.
	 *
	 * @param array $input Unsanitized settings.
	 * @return array
	 */
	public static function sanitize( $input ) {
		$defaults = self::defaults();
		$input    = is_array( $input ) ? $input : array();
		$context  = isset( $input['settings_context'] ) ? sanitize_key( wp_unslash( $input['settings_context'] ) ) : 'general';
		$output   = self::get_all();

		if ( 'general' === $context ) {
			$allowed_statuses = array( 'failed', 'pending', 'cancelled', 'on-hold' );
			$statuses         = isset( $input['enabled_statuses'] ) && is_array( $input['enabled_statuses'] ) ? $input['enabled_statuses'] : array();
			$output['enabled_statuses'] = array_values( array_intersect( array_map( 'sanitize_key', $statuses ), $allowed_statuses ) );

			if ( empty( $output['enabled_statuses'] ) ) {
				$output['enabled_statuses'] = $defaults['enabled_statuses'];
			}

			$output['email_subject'] = isset( $input['email_subject'] ) ? sanitize_text_field( wp_unslash( $input['email_subject'] ) ) : $defaults['email_subject'];
			$output['email_heading'] = isset( $input['email_heading'] ) ? sanitize_text_field( wp_unslash( $input['email_heading'] ) ) : $defaults['email_heading'];
			$output['email_body']    = isset( $input['email_body'] ) ? wp_kses_post( wp_unslash( $input['email_body'] ) ) : $defaults['email_body'];
			$output['from_name']     = isset( $input['from_name'] ) ? sanitize_text_field( wp_unslash( $input['from_name'] ) ) : '';
			$output['from_email']    = isset( $input['from_email'] ) ? sanitize_email( wp_unslash( $input['from_email'] ) ) : '';
			$output['delete_data_on_uninstall'] = isset( $input['delete_data_on_uninstall'] ) && 'yes' === $input['delete_data_on_uninstall'] ? 'yes' : 'no';
		}

		if ( class_exists( 'FORD_Pro' ) ) {
			return FORD_Pro::sanitize_settings( $output, $input, $context );
		}

		return $output;
	}
}
