<?php
/**
 * Its Reward Point Module
 *
 * @since: 01/11/2023
 * @package Vite_Reward_Lite\Modules
 */

namespace Vite_Reward_Lite\Modules;

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

use Appsbd_Lite\V5\libs\API_Response;
use Vite_Reward_Lite\Core\Vite_Reward_Module_Lite;
use Vite_Reward_Lite\Libs\Vite_Custom_Field;
use Vite_Reward_Lite\Libs\Vite_Rule_Group;
use Vite_Reward_Lite\Libs\Vite_Rule_Item;
use Vite_Reward_Lite\Models\Database\Mapbd_Reward_Badge;
use Vite_Reward_Lite\Models\Database\Mapbd_Reward_Email_Template;
use Vite_Reward_Lite\Models\Database\Mapbd_Reward_Log;

/**
 * Class Woocom_Reward
 *
 * @package Vite_Reward_Lite\Modules
 */
class Vite_Woocom_Reward extends Vite_Reward_Module_Lite {

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

	/**
	 * The on active is generated by appsbd
	 */
	public function on_active() {
		parent::on_active();
	}

	/**
	 * The on table create is generated by appsbd
	 */
	public function on_table_create() {
		Mapbd_Reward_Log::create_db_table();
		Mapbd_Reward_Email_Template::create_db_table();
		Mapbd_Reward_Badge::create_db_table();
	}

	/**
	 * 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( 'appsbd/vite-reward/action/update-rule-order', array( $this, 'update_order_rule' ) );
		add_action( 'woocommerce_order_status_completed', array( $this, 'action_on_order_status_completed' ), 20, 2 );
		add_action( 'woocommerce_order_refunded', array( $this, 'action_woocommerce_order_refunded' ), 20, 2 );
		if ( 'Y' == Vite_Reward_Settings::get_module_instance()->get_option( 'is_display_point_notification', 'N' ) ) {
			add_action( 'woocommerce_before_cart', array( $this, 'reward_notice' ) );
		}
	}

	/**
	 * 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' ) ) {
			return;
		} else {
			$this->add_reward_points( $order );
		}
	}

	/**
	 * The add reward points is generated by appsbd
	 *
	 * @param mixed $order It is order param.
	 */
	public function add_reward_points( $order ) {
		$found_rule = Vite_Reward_Rules::get_active_rule( 'PO' );
		if ( ! empty( $found_rule ) ) {
			$this->per_order_action( $found_rule, $order );
		}
		$found_rule = Vite_Reward_Rules::get_active_rule( 'OD' );
		if ( ! empty( $found_rule ) ) {
			$cal_condition = $found_rule->get_field_value( 'cal_cond' );
			$rule_points   = 0.00;
			if ( ! empty( $cal_condition ) ) {
				$rule_points = $this->get_rule_points( $cal_condition, $found_rule->points, $order );
			}
			$is_max = $found_rule->get_field_value( 'is_max' );
			if ( 'Y' == $is_max ) {
				$max_amount = appsbd_wc_amount( $found_rule->get_field_value( 'get_max_points' ) );
				if ( $max_amount > 0 ) {
					if ( $rule_points > $max_amount ) {
						$rule_points = $max_amount;
					}
				}
			}
			if ( ! empty( $order->get_customer_id() ) ) {
				Vite_Reward_Settings::update_user_points(
					$order->get_customer_id(),
					'O',
					$order->get_id(),
					$rule_points,
					$this->__( 'From order amounts' )
				);
			}
		}
	}

	/**
	 * The reward notice is generated by appsbd
	 */
	public function reward_notice() {
		/**
		 * Its for check is there any change before process
		 *
		 * @since 1.0
		 */
		$color = apply_filters( 'appsbd/rwd/filters/get-theme-color', array() );
		if ( empty( $color ) ) {
			$color = 'gray';
		}
		$class_name      = 'apbd-reward-notification rwd-' . $color;
		$currency_amount = Vite_Reward_Settings::get_module_instance()->get_option( 'currency_amount' );
		$currency_points = Vite_Reward_Settings::get_module_instance()->get_option( 'currency_points' );
		if ( ! empty( $currency_amount ) && ! empty( $currency_points ) ) {
			$notification_msg = $this->__( 'DISCOUNT RULE FOR APPLYING YOUR REWARD POINTS TO CART TOTAL   ' ) . $currency_points . $this->__( ' POINTS = ' ) . wc_price( $currency_amount );
			echo "<div class='" . esc_attr( $class_name ) . "'>" . wp_kses_post( $notification_msg ) . '</div>';
		}
		$found_rule = Vite_Reward_Rules::get_active_rule( 'OD' );
		if ( ! empty( $found_rule ) ) {
			$cond     = $found_rule->get_field_value( 'cal_cond' );
			$conv_msg = '';
			if ( 'S' == $cond ) {
				$conv_msg = 'CONVERSION RATE: SUBTOTAL * ' . $found_rule->points;
			} elseif ( 'T' == $cond ) {
				$conv_msg = 'CONVERSION RATE: ORDER TOTAL * ' . $found_rule->points;
			} else {
				$conv_msg = 'FIXED ' . $found_rule->points . ' POINTS';
			}
			$notification_msg = $this->__( 'PLACE ORDER AND EARN REWARD POINTS. ' ) . $conv_msg;
			echo "<div class='" . esc_attr( $class_name ) . "'>" . wp_kses_post( $notification_msg ) . '</div>';
		}
		$found_rule = Vite_Reward_Rules::get_active_rule( 'PO' );
		if ( ! empty( $found_rule ) ) {
			$notification_msg = $this->__( 'YOU WILL GET ' ) . $found_rule->points . $this->__( ' POINTS WHEN YOU COMPLETE THE ORDER' );
			echo "<div class='" . esc_attr( $class_name ) . "'>" . wp_kses_post( $notification_msg ) . '</div>';
		}
	}

	/**
	 * The per order action is generated by appsbd
	 *
	 * @param mixed     $found_rule Founded Rule.
	 * @param \WC_Order $order Order.
	 */
	public function per_order_action( $found_rule, &$order ) {
		$rule_points = $found_rule->points;
		if ( $rule_points > 0 ) {
			if ( ! empty( $order->get_customer_id() ) ) {
				Vite_Reward_Settings::update_user_points(
					$order->get_customer_id(),
					'O',
					$order->get_id(),
					$rule_points,
					$this->__( 'From order' ),
					'I',
					'POP'
				);
			}
		}
	}

	/**
	 * The action woocommerce order refunded is generated by appsbd
	 *
	 * @param mixed $order_id order id.
	 * @param mixed $refund_id refund id.
	 */
	public function action_woocommerce_order_refunded( $order_id, $refund_id ) {
		$found_rule = Vite_Reward_Rules::get_active_rule( 'OR' );

		return;
	}

	/**
	 * The get rule points is generated by appsbd
	 *
	 * @param mixed     $rule its rule.
	 * @param mixed     $points its the cal point.
	 * @param \WC_Order $order Wc order.
	 *
	 * @return float|mixed
	 */
	public function get_rule_points( $rule, $points, &$order ) {
		$get_point = 0.0;
		if ( 'S' == $rule ) {
			$get_point = $order->get_subtotal() * $points;
		} elseif ( 'T' == $rule ) {
			$get_point = $order->get_total() * $points;
		} else {
			$get_point = $points;
		}

		return $get_point;
	}

	/**
	 * The update rule is generated by appsbd
	 *
	 * @param API_Response $response Its response.
	 */
	public function update_order_rule( &$response ) {
		$this->add_info( 'Successfully updated order rule' );
		$response->set_response( true );
	}

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

		$per_ord_point        = new Vite_Rule_Item(
			'PO',
			$this->__( 'Per Order Points' ),
			$this->__( 'Award points based on the number of orders placed by customers.' )
		);
		$per_ord_amount_point = new Vite_Rule_Item(
			'OD',
			$this->__( 'Per Order Amount Points' ),
			$this->__( 'Reward points are based on the total amount spent on each order.' )
		);
		$per_ord_amount_point->add_field(
			Vite_Custom_Field::get_dropdown(
				'cal_cond',
				$this->__( 'Calculation' ),
				'',
				'N',
				$this->__( 'Select' ),
				'Y',
				array(
					array(
						'val'   => 'S',
						'title' => $this->__( 'Point Multiply with Subtotal' ),
					),
					array(
						'val'   => 'T',
						'title' => $this->__( 'Point Multiply with Order Total' ),
					),
					array(
						'val'   => 'F',
						'title' => $this->__( 'Fixed Point' ),
					),
				)
			)
		);
		$per_ord_amount_point->add_field(
			Vite_Custom_Field::get_switch( 'is_max', $this->__( 'Is Set Max Point' ), 'N', 'R' )
		);
		$per_ord_amount_point->add_field(
			Vite_Custom_Field::get_numeric(
				'get_max_points',
				$this->__( 'Max Point' ),
				0,
				'R',
				'Y',
				$this->__( 'Enter Max Point' ),
				'',
				'',
				'A',
				'is_max'
			)
		);
		$ord_range = new Vite_Rule_Item(
			'AR',
			$this->__( 'On Order Amount Range' ),
			$this->__( 'Define point rewards based on specific order amount ranges to encourage larger purchases.' ),
			'R'
		);
		$ord_range->add_field(
			Vite_Custom_Field::get_numeric(
				'MN',
				$this->__( 'Min Amount' ),
				'',
				'N',
				'',
				$this->__( 'Min Amount' ),
				'',
				'',
				'A'
			)
		);
		$ord_range->add_field(
			Vite_Custom_Field::get_numeric(
				'MX',
				$this->__( 'Max Amount' ),
				'',
				'N',
				'',
				$this->__( 'Max Amount' ),
				'',
				'',
				'A'
			)
		);
		$group = new Vite_Rule_Group( 'order', $this->__( 'Order Rules' ) );
		$group->add_rule_item( $per_ord_point );
		$group->add_rule_item( $per_ord_amount_point );
		$group->add_rule_item( $ord_range );
		$rules = array_merge( $rules, $group->get_rules() );

		return $rules;
	}
}
