<?php
/**
 * Its Reward Point Module
 *
 * @package Vite_Reward_Lite\Modules
 */

namespace Vite_Reward_Lite\Modules;

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

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;
use Vite_Reward_Lite\Libs\Vite_Rule_Item;

/**
 * Class User_Reward
 *
 * @package Vite_Reward_Lite\Modules
 */
class Vite_Product_Reward 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();
		add_filter( 'appsbd/vite-reward/filter/rules', array( $this, 'set_rules' ) );
		add_action( 'woocommerce_order_status_completed', array( $this, 'action_on_order_status_completed' ), 20, 2 );

		/**
		 * It's for check
		 * add_action( 'appsbd/vite-reward/action/update-rule-user', array( $this, 'update_rule' ) );
		 * */
	}

	/**
	 * The action on order status completed is generated by appsbd
	 *
	 * @param mixed     $order_id order id.
	 * @param \WC_Order $order order object.
	 */
	public function action_on_order_status_completed( $order_id, $order ) {
		if ( ! $order->meta_exists( '_is_vitepos' ) ) {
			$this->add_global_product_reward( $order );
		}
	}

	/**
	 * The add global product reward is generated by appsbd
	 *
	 * @param mixed $order It is order param.
	 */
	public function add_global_product_reward( $order ) {
		$found_rule = Vite_Reward_Rules::get_active_rule( 'GP' );
		if ( ! empty( $found_rule ) ) {
			$this->global_product_action( $found_rule, $order );
		}
	}
	/**
	 * The global product action is generated by appsbd
	 *
	 * @param mixed     $found_rule Founded rule.
	 * @param \WC_Order $order Order.
	 */
	public function global_product_action( $found_rule, &$order ) {
		$rule_points = $found_rule->points;
		if ( $rule_points > 0 ) {
			if ( ! empty( $order->get_customer_id() ) ) {
				$item_count = $order->get_item_count();
				if ( $item_count > 0 ) {
					$rule_points = $rule_points * $item_count;
				}
				Vite_Reward_Settings::update_user_points(
					$order->get_customer_id(),
					'O',
					$order->get_id(),
					$rule_points,
					$this->__( 'Order items reward' )
				);
			}
		}
	}

	/**
	 * The update rule is generated by appsbd
	 *
	 * @param API_Response $response Its the response.
	 */
	public function update_rule( $response ) {
		$data = AppInput::get_posted_data();
		Vite_Rule_Group::save_rules_data( $data );
		$this->add_info( 'Successfully updated' );
		$response->set_response( true );
	}

	/**
	 * The set rules is generated by appsbd
	 *
	 * @param mixed $rules Its the rules.
	 *
	 * @return mixed
	 */
	public function set_rules( $rules ) {

		$global_pro = new Vite_Rule_Item(
			'GP',
			$this->__( 'Global Assign Product Points' ),
			$this->__( 'It will add points for every product in an order. For example, if there are 3 products, it will add 3 multiplied by defined points.' ),
			'N'
		);

		$group = new Vite_Rule_Group( 'product', $this->__( 'Product' ) );
		$group->add_rule_item( $global_pro );
		$rules = array_merge( $rules, $group->get_rules() );

		return $rules;
	}
}
