<?php
/**
 * Its for Reword Rules.
 *
 * @package Vite_Reward_Lite\Modules
 */

namespace Vite_Reward_Lite\Modules;

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

use Appsbd_Lite\V5\libs\Ajax_Confirm_Response;
use Appsbd_Lite\V5\libs\Ajax_Data_Response;
use Appsbd_Lite\V5\libs\API_Data_Response;
use Appsbd_Lite\V5\libs\API_Response;
use Appsbd_Lite\V5\libs\AppInput;
use Vite_Reward_Lite\Core\Vite_Reward_Module_Lite;
use Vite_Reward_Lite\Libs\Vite_Rule_Group;

/**
 * Class Reward_Rules
 *
 * @package Vite_Reward_Lite\Modules
 */
class Vite_Reward_Rules extends Vite_Reward_Module_Lite {

	/**
	 * The initialize is generated by appsbd
	 */
	public function initialize() {
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
		$this->add_admin_ajax_action( 'get-rules', array( $this, 'get_rules' ) );
		$this->add_admin_ajax_action( 'update-rule', array( $this, 'update_rule' ) );
		$this->add_admin_ajax_action( 'change-email-status', array( $this, 'update_rule_send_email_status' ) );
	}

	/**
	 * The get active rule is generated by appsbd
	 *
	 * @param mixed $id Its rule id.
	 *
	 * @return \Vite_Reward_Lite\Libs\Vite_Rule_Item |null
	 */
	public static function get_active_rule( $id ) {

		/**
		 * Its for check is there any change before process
		 *
		 * @since 0.0
		 */
		$rules      = apply_filters( 'appsbd/vite-reward/filter/rules', array() );
		$found_rule = null;
		foreach ( $rules as $rule ) {
			if ( $id == $rule->id && 'A' == $rule->status ) {
				$found_rule = $rule;
				break;
			}
		}
		return $found_rule;
	}
	/**
	 * The get rules is generated by appsbd
	 */
	public function get_rules() {
		$response = new Ajax_Confirm_Response();
		/**
		 * Its for check is there any change before process
		 *
		 * @since 1.0
		 */
		$data = apply_filters( 'appsbd/vite-reward/filter/rules', array() );
		$response->display_with_response( true, $data );
	}

	/**
	 * The update rule is generated by appsbd
	 */
	public function update_rule() {
		$response = new Ajax_Confirm_Response();
		$group_id = AppInput::post_value( 'group_id' );
		if ( ! empty( $group_id ) ) {
			$data = AppInput::get_posted_data();
			if ( Vite_Rule_Group::save_rules_data( $data ) ) {
				/**
				 * Its for check is there any change before process
				 *
				 * @since 1.0
				 */
				do_action( 'appsbd/vite-reward/action/updated-rule-' . $group_id, $data );
				/**
				 * Its for check is there any change before process
				 *
				 * @since 1.0
				 */
				do_action( 'appsbd/vite-reward/action/updated-ruled', $data );

				$this->add_info( 'Successfully updated' );
				$response->display_with_response( true );
			} else {
				$this->add_info( 'Update failed' );
				$response->display_with_response( false );
			}
		} else {
			$this->add_error( 'Invalid request param' );
			$response->display_with_response( false );
		}
		$response->display_with_response( true );
	}
	/**
	 * The update rule is generated by appsbd
	 */
	public function update_rule_send_email_status() {
		$response = new Ajax_Confirm_Response();
		$group_id = AppInput::post_value( 'group_id' );
		if ( ! empty( $group_id ) ) {
			$data = AppInput::get_posted_data();
			if ( Vite_Rule_Group::change_send_email_status( $data ) ) {
				$this->add_info( 'Successfully updated' );
				$response->display_with_response( true );
			} else {
				$this->add_info( 'Update failed' );
				$response->display_with_response( false );
			}
		} else {
			$this->add_error( 'Invalid request param' );
			$response->display_with_response( false );
		}
		$response->display_with_response( true );
	}
}
