<?php
/**
 * Helper functions for WooCommerce Admin Order Status SMS Notification
 *
 * @package miniorange-sms-order-notification-otp-verification/notifications/wcsmsnotification/helper/notifications
 */

namespace WCSMSOTP\Notifications\WcSMSNotification\Helper\Notifications;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use WCSMSOTP\Notifications\WcSMSNotification\Helper\MoWcAddOnMessages;
use WCSMSOTP\Notifications\WcSMSNotification\Helper\MoWcAddOnUtility;
use WCSMSOTP\Notifications\WcSMSNotification\Helper\WcOrderStatus;
use WCSMSOTP\Helper\MoUtility;
use WCSMSOTP\Objects\SMSNotification;

/**
 * This class is used to handle all the settings and function related
 * to the WooCommerce Admin Order Status SMS Notification. It initializes the
 * notification related settings and implements the functionality for
 * sending the SMS to the user.
 */
if ( ! class_exists( 'WooCommerceAdminOrderstatusNotification' ) ) {
	/**
	 * WooCommerceAdminOrderstatusNotification class
	 */
	class WooCommerceAdminOrderstatusNotification extends SMSNotification {

		/** Global Variable
		 *
		 * @var instance - initiates the instance of the file.
		 */
		public static $instance;
		/** Global Variable
		 *
		 * @var array statuses - defines the order status.
		 */
		public static $statuses;

		/** Declare Default variables */
		protected function __construct() {
			parent::__construct();
			$this->title             = 'Order Status';
			$this->page              = 'wc_admin_order_status_notif';
			$this->is_enabled        = false;
			$this->tool_tip_header   = 'NEW_ORDER_NOTIF_HEADER';
			$this->tool_tip_body     = 'NEW_ORDER_NOTIF_BODY';
			$this->recipient         = MoWcAddOnUtility::get_admin_phone_number();
			$this->sms_body          = MoWcAddOnMessages::showMessage( MoWcAddOnMessages::ADMIN_STATUS_SMS );
			$this->default_sms_body  = MoWcAddOnMessages::showMessage( MoWcAddOnMessages::ADMIN_STATUS_SMS );
			$this->available_tags    = '{site-name},{order-number},{username},{order-date},{payment-method},{total-Amount},{transaction-ID},{order-key},{billing-firstName},{billing-phone},{billing-email},{billing-address},{billing-city},
            {billing-state},{billing-postcode},{billing-country},{shipping-firstName},{shipping-phone},{shipping-address},{shipping-city},{shipping-state},{shipping-postcode},{shipping-country}';
			$this->page_description  = mowc_( 'Order Status updates SMS notifications sent to the admins' );
			$this->notification_type = mowc_( 'Administrator' );
			self::$instance          = $this;
			self::$statuses          = WcOrderStatus::get_all_status();
		}


		/**
		 * Checks if there exists an existing instance of the class.
		 * If not then creates an instance and returns it.
		 */
		public static function getInstance() {
			return null === self::$instance ? new self() : self::$instance;
		}


		/**
		 * Initialize all the variables required to modify the sms template
		 * and send the SMS to the user. Checks if the SMS notification
		 * has been enabled and send SMS to the user. Do not send SMS
		 * if phone number of the customer doesn't exist.
		 *
		 * @param  array $args all the arguments required to send SMS.
		 */
		public function send_sms( array $args ) {
			if ( ! $this->is_enabled ) {
				return;
			}
			if ( ! isset( $args['orderDetails'] ) || ! isset( $args['new_status'] ) ) {
				return;
			}
			$order_details = $args['orderDetails'];
			$new_status    = sanitize_text_field( $args['new_status'] );
			if ( MoUtility::is_blank( $order_details ) || ! is_a( $order_details, 'WC_Order' ) ) {
				return;
			}
			if ( ! is_array( self::$statuses ) || ! in_array( $new_status, self::$statuses, true ) ) {
				return;
			}
			$this->set_notif_in_session( $this->page );
			$customer_id   = $order_details->get_customer_id();
			$userdetails   = ! empty( $customer_id ) ? get_userdata( $customer_id ) : false;
			$site_name     = get_bloginfo( 'name' );
			$username      = ( $userdetails && isset( $userdetails->user_login ) ) ? $userdetails->user_login : '';
			$phone_numbers = maybe_unserialize( $this->recipient );
			if ( false === $phone_numbers ) {
				$phone_numbers = '';
			} elseif ( is_array( $phone_numbers ) ) {
				$phone_numbers = array_map( 'trim', $phone_numbers );
				$phone_numbers = array_filter( $phone_numbers );
			} else {
				$phone_numbers = ! empty( $phone_numbers ) ? array( trim( $phone_numbers ) ) : array();
			}
			$date_created_obj = $order_details->get_date_created();
			$date_created     = ( $date_created_obj && method_exists( $date_created_obj, 'date_i18n' ) ) ? $date_created_obj->date_i18n() : '';
			$order_no         = $order_details->get_order_number();

			/* Adding extra tags */
			$shipping_total = $order_details->get_shipping_total();
			$total_amt      = $order_details->get_total();
			$order_key      = $order_details->get_order_key();

			/* billing details (_b) */
			$first_name_b       = $order_details->get_billing_first_name();
			$last_name_b        = $order_details->get_billing_last_name();
			$phone_customer_b   = $order_details->get_billing_phone();
			$user_email_b       = $order_details->get_billing_email();
			$billing_address_b  = $order_details->get_billing_address_1();
			$billing_city_b     = $order_details->get_billing_city();
			$billing_state_b    = $order_details->get_billing_state();
			$billing_postcode_b = $order_details->get_billing_postcode();
			$billing_country_b  = $order_details->get_billing_country();

			$payment_method = $order_details->get_payment_method_title();
			$transaction_id = $order_details->get_transaction_id();

			/* shipping details (_s) */
			$first_name_s        = $order_details->get_shipping_first_name();
			$last_name_s         = $order_details->get_shipping_last_name();
			$phone_customer_s    = $order_details->get_shipping_phone();
			$shipping_address_s  = $order_details->get_shipping_address_1();
			$shipping_city_s     = $order_details->get_shipping_city();
			$shipping_state_s    = $order_details->get_shipping_state();
			$shipping_postcode_s = $order_details->get_shipping_postcode();
			$shipping_country_s  = $order_details->get_shipping_country();

			$replaced_string = array(
				'site-name'          => sanitize_text_field( $site_name ),
				'username'           => sanitize_text_field( $username ),
				'order-date'         => sanitize_text_field( $date_created ),
				'order-number'       => sanitize_text_field( $order_no ),
				'order-status'       => sanitize_text_field( $new_status ),
				'shipping-total'     => sanitize_text_field( $shipping_total ),
				'total-amount'       => sanitize_text_field( $total_amt ),
				'order-key'          => sanitize_text_field( $order_key ),

				'billing-firstName'  => sanitize_text_field( $first_name_b ),
				'billing-phone'      => sanitize_text_field( $phone_customer_b ),
				'billing-email'      => sanitize_email( $user_email_b ),
				'billing-address'    => sanitize_text_field( $billing_address_b ),
				'billing-city'       => sanitize_text_field( $billing_city_b ),
				'billing-state'      => sanitize_text_field( $billing_state_b ),
				'billing-postcode'   => sanitize_text_field( $billing_postcode_b ),
				'billing-country'    => sanitize_text_field( $billing_country_b ),

				'payment-mode'       => sanitize_text_field( $payment_method ),
				'transaction-ID'     => sanitize_text_field( $transaction_id ),

				'shipping-firstName' => sanitize_text_field( $first_name_s ),
				'shipping-phone'     => sanitize_text_field( $phone_customer_s ),
				'shipping-address'   => sanitize_text_field( $shipping_address_s ),
				'shipping-city'      => sanitize_text_field( $shipping_city_s ),
				'shipping-state'     => sanitize_text_field( $shipping_state_s ),
				'shipping-postcode'  => sanitize_text_field( $shipping_postcode_s ),
				'shipping-country'   => sanitize_text_field( $shipping_country_s ),
			);
			$replaced_string = apply_filters( 'mowc_admin_order_notif_string_replace', $replaced_string );
			$sms_body        = MoUtility::replace_string( $replaced_string, $this->sms_body );

			if ( empty( $phone_numbers ) || ! is_array( $phone_numbers ) ) {
				return;
			}
			foreach ( $phone_numbers as $phone_number ) {
				$phone_number = trim( $phone_number );
				if ( ! MoUtility::is_blank( $phone_number ) && MoUtility::validate_phone_number( $phone_number ) ) {
					MoUtility::send_phone_notif( $phone_number, $sms_body );
				}
			}
		}
	}

}
