<?php

/**
 * Data validation utilities
 *
 * @link       https://revivedesignstudios.com
 * @since      1.0.5
 *
 * @package    Rds_Bt50_Scorecard
 * @subpackage Rds_Bt50_Scorecard/includes
 */

/**
 * Validation of admin settings
 *
 * @package    Rds_Bt50_Scorecard
 * @subpackage Rds_scBt50_Scorecard/includes
 * @author     Revive Design Studios <connect@revivedesignstudios.com>
 */
class Rds_Bt50_Scorecard_Validations {
	public static $apiParamsWhitelist = [
		'rds_bt50_scorerange_splits',
		'rds_bt50_scorerange_colors',
		'score_title_color',
		'score_primary_color',
		'score_secondary_color',
		'legislator_page_name_color',
		'show_presence_score',
		'sidebar_promo_title',
		'sidebar_promo_description',
		'promo_legislator_page',
		'score_subtitle',
		'score_info',
		'force_https_urls',
		'display_promo_sidebar',
		'display_recent_wp_post',
		'display_recent_bt50_bills',
		'recent_wp_post_categories',
		'score_type_to_display',
		'enable_legislator_pagination',
	];

	/**
	*
	* @since    1.0.5
	*/
	public function __construct() {
	}

	public static function getAdminParamsValidationErrors($params) {
		$validationErrors = array();
		// independent settings validation
		foreach ($params as $key => $value) {
			if (array_search($key, self::$apiParamsWhitelist) === false) {
				$validationErrors[$key] = $key . ' is not a valid setting';
			}
			$error = self::getValidationErrorForKey($key, $value);
			if ($error) {
				$validationErrors[$key] = $key . ' '. $error;
				$error = false;
			}
		}

		// dependent settings
		if ($params['rds_bt50_scorerange_splits'] || $params['rds_bt50_scorerange_colors']) {
			if (!($params['rds_bt50_scorerange_splits'] && $params['rds_bt50_scorerange_colors'])) {
				$validationErrors['scorerange'] ='Both score range splits and colors must be specified ';
			} else {
				$splits = $params['rds_bt50_scorerange_splits'];
				$colors = $params['rds_bt50_scorerange_colors'];
				if ((sizeof($splits) + 1) != sizeof($colors)) {
					$validationErrors['scorerange'] = 'Must specify one more range color than range splits';
				}
			}
		}
		return $validationErrors;
	}

	/**
	* Validate individual options. Returns error explanation or false for no errors
	*/
	public static function getValidationErrorForKey($key, $value) {
		$commaSeparatedIntsRegex = '/([\d]{1,}(,|$)){1,}/i';
		$commaSeparatedHexcolorsRegex = '/(#[a-f\d]{6}(,|$)){1,}/i';
		$hexcolorsRegex = '/#[a-f\d]{3,6}/i';

		// if (!isset($value) || empty($value)) {
		// 	return 'Value must be specified';
		// }
		switch ($key) {
			case 'rds_bt50_scorerange_splits':
				if (preg_match($commaSeparatedIntsRegex, implode(',', $value)) == 0) {
					return 'Score range splits must be one or more integers';
				}
				break;
			case 'rds_bt50_scorerange_colors':
				if (preg_match($commaSeparatedHexcolorsRegex, implode(',', $value)) == 0) {
					return 'Score range colors must be one or more colors';
				}
				break;
			// wysiwyg fields
			case 'score_info':
			case 'score_subtitle':
			case 'promo_legislator_page':
			// boolean fields
			case 'force_https_urls':
			case 'display_promo_sidebar':
			case 'display_recent_wp_post':
			case 'display_recent_bt50_bills':
			case 'enable_legislator_pagination':
			// other
			case 'score_type_to_display':
				break;
			// wp category id fields
			case 'recent_wp_post_categories':
				// array of integers (currently strings though)
				break;
			// hex color fields
			case 'score_title_color':
			case 'score_primary_color':
			case 'score_secondary_color':
			case 'legislator_page_name_color':
				if (preg_match($hexcolorsRegex, $value) == 0) {
					return 'Colors must be a hex value ('. $key . ')';
				}
				break;
		}
		return false;
	}
}
