<?php

/**
 * The class that contains helper function for the plugin
 *
 * @link       https://autoserve.io
 * @since      1.0.0
 *
 * @package    Messageflow
 * @subpackage Messageflow/includes
 */

/**
 * The class that contains helper function for the plugin
 *
 * @since      1.0.0
 * @package    Messageflow
 * @subpackage Messageflow/includes
 * @author     Autoserve <support@autoserve.io>
 */
class Messageflow_Utils
{
	// Function to send Twilio programmable SMS
	public function twilio_send_programmable_sms($account_sid, $auth_token, $from_phone_number, $to_phone_number, $message) {
		try {
			$api_url = 'https://tygh3ygdt7.execute-api.us-east-1.amazonaws.com/Prod/message/';

			$args = array(
				'phones'        => [ $to_phone_number ],
				'message'       => $message,
				'accountSid'    => $account_sid,
				'authToken'     => $auth_token,
				'fromNumber'    => $from_phone_number
			);

			$response = wp_remote_post( $api_url, array(
				'body'          => json_encode( $args ),
			) );

			return $response;
		} catch (Exception $e) {

			echo 'Error calling Twilio API. Error message: ' . $e->getMessage();
			return [];
		}
	}

	// Function to get all WooCommerce order statuses
	public function woocommerce_get_all_order_statuses() {
		if ( is_plugin_active( 'woocommerce-order-status-manager/woocommerce-order-status-manager.php' ) ) {
			$order_statuses = get_posts( array('post_type'=>'wc_order_status', 'post_status'=>'publish', 'numberposts'=>-1) );

			$statuses = array();
			foreach ( $order_statuses as $status ) {
				$statuses[ $status->post_name ] = $status->post_title;
			}

			return $statuses;
		} else {
			$statuses = wc_get_order_statuses();
		 	$status_slugs = array_keys( $statuses );
		    $formatted_statuses = array();

		    foreach ( $status_slugs as $slug ) {
				$formatted_slug = str_replace( 'wc-', '', $slug );
				$formatted_statuses[ $formatted_slug ] = $statuses[ $slug ];
			}

			return $formatted_statuses;
		}
	}

	// Function to check whether Woocommerce shipment tracking plugin is enabled
	public function woocommerce_is_shipment_tracking_enabled() {
		if ( ! function_exists( 'is_plugin_activve' ) ) {
			include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
		}

		if ( is_plugin_active( 'woocommerce-shipment-tracking/woocommerce-shipment-tracking.php' ) ) {
			return true;
		} else {
			return false;
		}
	}

	// Function to get the site name
	public function get_site_name() {
		return ( is_multisite() ) ? get_blog_details()->blogname : get_bloginfo( 'name' );
	}

	// Function to get all the WordPress roles
	public function get_all_roles() {
		global $wp_roles;

		$roles = $wp_roles->roles;

		return $roles;
	}
}
