<?php
/**
 * Contact Form-7 Submission Notifications helper
 *
 * @package miniorange-sms-order-notification-otp-verification/notifications/formsmsnotification/helper/notifications
 */

namespace WCSMSOTP\Notifications\FormSMSNotification\Helper\Notifications;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use WCSMSOTP\Notifications\FormSMSNotification\Helper\FormSMSNotificationMessages;
use WCSMSOTP\Helper\MoUtility;
use WCSMSOTP\Objects\SMSNotification;
use WCSMSOTP\Notifications\FormSMSNotification\Helper\FormSMSNotificationUtility;

/**
 * This class is used to handle all the settings and function related
 * to the Contact Form-7 SMS Notification. It initializes the
 * notification related settings and implements the functionality for
 * sending the SMS to the user.
 *
 * @param mixed $instance.
 */
if ( ! class_exists( 'CF7FormSubmitionNotification' ) ) {
	/**
	 * CF7FormSubmitionNotification class
	 */
	class CF7FormSubmitionNotification extends SMSNotification {

		/**
		 * Instance.
		 *
		 * @var mixed $insatance Instance.
		 */
		public static $instance;
		/**
		 * Initializes values
		 */
		protected function __construct() {
			parent::__construct();
			$this->title             = 'Contact Form- 7';
			$this->page              = 'mo_contact_form7_notif';
			$this->is_enabled        = false;
			$this->tool_tip_body     = 'NEW_FORMS_NOTIF_BODY';
			$this->admin_recipient   = mowc_get_fmsn_option( 'moform_notif_admin_recipient' );
			$this->sms_body          = FormSMSNotificationMessages::showMessage(
				FormSMSNotificationMessages::NEW_CONTACT_FORM_SMS
			);
			$this->default_sms_body  = FormSMSNotificationMessages::showMessage(
				FormSMSNotificationMessages::NEW_CONTACT_FORM_SMS
			);
			$this->available_tags    = '{site-name},{username},{accountpage-url},{login-url},{email},{fullname}';
			$this->page_description  = mowc_( 'SMS notifications template send on submission of Contact form-7' );
			$this->phone_input       = 'Name of Phone field';
			$this->notification_type = mowc_( 'Admin and Customer' );
			self::$instance          = $this;
		}


		/**
		 * 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( $args ) {
			if ( ! $this->is_enabled ) {
				return;
			}
			$this->set_notif_in_session( $this->page );
			$data = MoUtility::mowc_sanitize_array( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No need for nonce verification as the function is called on third party plugin hook.

			if ( ! isset( $data[ $this->recipient ] ) || MoUtility::is_blank( $data[ $this->recipient ] ) ) {
				return;
			}
			$customer_phone = $data[ $this->recipient ];

			$admin_phone_numbers = maybe_unserialize( $this->admin_recipient );
			if ( false === $admin_phone_numbers ) {
				$admin_phone_numbers = '';
			} elseif ( is_array( $admin_phone_numbers ) ) {
				$admin_phone_numbers = implode( ';', array_map( 'sanitize_text_field', $admin_phone_numbers ) );
			} else {
				$admin_phone_numbers = sanitize_text_field( $admin_phone_numbers );
			}
			$phone_numbers = ! empty( $admin_phone_numbers ) ? explode( ';', $admin_phone_numbers ) : array();
			$phone_numbers = array_map( 'trim', $phone_numbers );
			$phone_numbers = array_filter( $phone_numbers );

			if ( ! MoUtility::is_blank( $customer_phone ) ) {
				$phone_numbers[] = $customer_phone;
			}

			$username = isset( $data['your-name'] ) ? $data['your-name'] : '';
			$email    = isset( $data['your-email'] ) ? $data['your-email'] : '';

			$replaced_string = array(
				'site-name' => sanitize_text_field( get_bloginfo( 'name' ) ),
				'username'  => sanitize_text_field( $username ),
				'email'     => sanitize_email( $email ),
			);
			$replaced_string = apply_filters( 'mowc_contact_form7_notif_string_replace', $replaced_string );
			$sms_body        = MoUtility::replace_string( $replaced_string, $this->sms_body );

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