<?php
/**
 * Its Rule Group.
 *
 * @since: 01/11/2023
 * @package Vite_Reward_Lite\Libs
 */

namespace Vite_Reward_Lite\Libs;

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

/**
 * Class Rule_Item
 *
 * @package Vite_Reward_Lite\Libs
 */
class Vite_Rule_Group {
	/**
	 * Its property rules_data
	 *
	 * @var array
	 */
	protected $rules_data = array();
	/**
	 * Its property group_title
	 *
	 * @var string
	 */
	protected $group_title = '';
	/**
	 * Its property group_id
	 *
	 * @var string
	 */
	protected $group_id = '';

	/**
	 * Its property rules
	 *
	 * @var Vite_Rule_Item[]
	 */
	protected $rules = array();

	/**
	 * Rule_Group constructor.
	 *
	 * @param string $group_id Its group_id param.
	 * @param string $group_title Its group_title param.
	 */
	public function __construct( $group_id, $group_title ) {
		$this->group_title = $group_title;
		$this->group_id    = $group_id;
		$this->load_rules_data();
	}

	/**
	 * The load rules data is generated by appsbd
	 */
	public function load_rules_data() {
		$this->rules_data = (array) get_option( 'aprp_ru' . $this->group_id, array() );
	}

	/**
	 * The load rules data is generated by appsbd
	 *
	 *  @param array $data Its data param.
	 */
	public static function save_rules_data( $data = array() ) {
		if ( ! empty( $data['group_id'] ) && ! empty( $data['id'] ) && ! empty( $data['status'] ) && ! empty( $data['points'] ) ) {
			$group_id = $data['group_id'];
			$id       = $data['id'];
			unset( $data['group_id'] );
			unset( $data['id'] );
			$final_data        = get_option( 'aprp_ru' . $group_id, array() );
			$final_data[ $id ] = $data;
			return update_option( 'aprp_ru' . $group_id, $final_data );
		}
		return false;
	}
	/**
	 * The load rules data is generated by appsbd
	 *
	 *  @param array $data Its data param.
	 */
	public static function change_send_email_status( $data = array() ) {
		if ( ! empty( $data['group_id'] ) && ! empty( $data['id'] ) ) {
			$group_id = $data['group_id'];
			$id       = $data['id'];
			unset( $data['group_id'] );
			unset( $data['id'] );
			$final_data        = get_option( 'aprp_ru' . $group_id, array() );
			$tmp_data = $final_data[ $id ];
			if ( isset( $tmp_data['send_email'] ) ) {
				$tmp_data['send_email'] = $data['send_email'];
			} else {
				$tmp_data['send_email'] = $data['send_email'];
			}
			$final_data[ $id ] = $tmp_data;
			return update_option( 'aprp_ru' . $group_id, $final_data );
		}
		return false;
	}
	/**
	 * The add rule item is generated by appsbd
	 *
	 * @param Vite_Rule_Item $rule Its rule param.
	 */
	public function add_rule_item( $rule ) {
		$rule->set_group( $this->group_id, $this->group_title );
		$rule_data = ! empty( $this->rules_data[ $rule->id ] ) ? $this->rules_data[ $rule->id ] : array();
		$rule->set_field_values( $rule_data );
		$this->rules[] = $rule;
	}

	/**
	 * The   toString is generated by appsbd
	 *
	 * @return false|string
	 */
	public function __toString() {
		return wp_json_encode( $this->rules );
	}

	/**
	 * The get rules is generated by appsbd
	 *
	 * @return Vite_Rule_Item[]
	 */
	public function get_rules() {
		return $this->rules;
	}
}
