<?php
/**
 * The public-facing functionality of the plugin.
 *
 * @link  http://www.powerfulwp.com
 * @since 1.0.0
 *
 * @package    DDFWM
 * @subpackage DDFWM/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    DDFWM
 * @subpackage DDFWM/public
 * @author     powerfulwp <cs@powerfulwp.com>
 */
class DDFWM_Public {


	/**
	 * The ID of this plugin.
	 *
	 * @since  1.0.0
	 * @access private
	 * @var    string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since  1.0.0
	 * @access private
	 * @var    string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since 1.0.0
	 * @param string $plugin_name The name of the plugin.
	 * @param string $version     The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version     = $version;

	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in DDFWM_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The DDFWM_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since 1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in DDFWM_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The DDFWM_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
	}


	/**
	 * Set the driver page.
	 *
	 * @param  mixed $page_template .
	 * @since 1.0.0
	 */
	public function ddfwm_page_template( $page_template ) {
		global $post;

		if ( intval( get_option( 'ddfwm_vendors_page', '' ) ) === $post->ID ) {
					$page_template = WP_PLUGIN_DIR . '/' . DDFWM_FOLDER . '/index.php';
		}
		return $page_template;
	}

		/**
	 * Set the driver page.
	 *
	 * @since 1.0.0
	 */
	public function ddfwm_page_template_redirect() { 
		global $post;
		if ( ! empty( $post ) ) {
			$template_path = '';

			if ( $post->ID === intval( get_option( 'ddfwm_vendors_page', '' ) ) ) {
				$this->ddfwm_initialize_panel_data_globals();
				$template_path = WP_PLUGIN_DIR . '/' . DDFWM_FOLDER . '/index.php';
			}  

			if ( ! empty( $template_path ) && file_exists( $template_path ) ) {
				include( $template_path );
				exit();
			}
		}
		// No return needed as we exit if a template is found and included.
	}

	/**
	 * Initialize the panel data globals.
	 *
	 * @since 1.0.0
	 */
	public function ddfwm_initialize_panel_data_globals() {
		global $ddfwm_screen;
		global $ddfwm_order_id;
		global $ddfwm_reset_key;
		global $ddfwm_page;
		global $ddfwm_reset_login;
		global $ddfwm_dates;
		global $ddfwm_driverid;
		global $ddfwm_status;
		global $ddfwm_content;
		global $ddfwm_vendor_id;
		global $ddfwm_vendor_drivers;
		 
		 
		global $ddfwm_vendor_account;
		global $ddfwm_vendor_drivers;
		global $ddfwm_vendor_name;
		global $ddfwm_orders;
		global $ddfwm_array;
		global $ddfwm_out_for_delivery_counter;
		global $ddfwm_failed_attempt_counter;
		global $ddfwm_delivered_counter;
		global $ddfwm_assign_to_driver_counter;
		global $ddfwm_claim_orders_counter;
		global $ddfwm_vendor_assigned_status_name;
		global $ddfwm_out_for_delivery_status_name;
		global $ddfwm_failed_attempt_status_name;




		$ddfwm_screen      = ( '' !== get_query_var( 'ddfwm_screen' ) ) ? get_query_var( 'ddfwm_screen' ) : 'dashboard';
		$ddfwm_order_id    = get_query_var( 'ddfwm_orderid' );
		$ddfwm_reset_key   = get_query_var( 'ddfwm_reset_key' );
		$ddfwm_page        = ( '' !== get_query_var( 'ddfwm_page' ) ) ? get_query_var( 'ddfwm_page' ) : '1';
		$ddfwm_reset_login = get_query_var( 'ddfwm_reset_login' );
		$ddfwm_dates       = get_query_var( 'ddfwm_dates' );
		$ddfwm_driverid    = get_query_var( 'ddfwm_driverid' );
		$ddfwm_status      = get_query_var( 'ddfwm_status' );
		$ddfwm_content        = '';
		$ddfwm_vendor_id      = '';
		$ddfwm_vendor_drivers = '';
		 
		$ddfwm_user           = wp_get_current_user();
		$ddfwm_vendor_id      = $ddfwm_user->ID;
		$ddfwm_vendor_account = get_user_meta( $ddfwm_vendor_id, 'ddfwm_vendor_account', true );
		$ddfwm_vendor_drivers = get_user_meta( $ddfwm_vendor_id, 'ddfwm_vendor_drivers', true );
		$ddfwm_vendor_name    = $ddfwm_user->display_name;

		// Get the number of orders in each status.
		$ddfwm_orders                   = new DDFWM_Orders();
		$ddfwm_array                    = $ddfwm_orders->ddfwm_orders_count_query( $ddfwm_vendor_id );
		$ddfwm_out_for_delivery_counter = 0;
		$ddfwm_failed_attempt_counter   = 0;
		$ddfwm_delivered_counter        = 0;
		$ddfwm_assign_to_driver_counter = 0;
		$ddfwm_claim_orders_counter     = 0;

		/**
		 * Set current status names
		 */
		$ddfwm_vendor_assigned_status_name  = esc_html( __( 'Driver assigned', 'ddfwm' ) );
		$ddfwm_out_for_delivery_status_name = esc_html( __( 'Out for delivery', 'ddfwm' ) );
		$ddfwm_failed_attempt_status_name   = esc_html( __( 'Failed delivery', 'ddfwm' ) );
		if ( function_exists( 'wc_get_order_statuses' ) ) {
			$result = wc_get_order_statuses();
			if ( ! empty( $result ) ) {
				foreach ( $result as $key => $status_name ) {
					switch ( $key ) {
						case get_option( 'ddfwm_out_for_delivery_status' ):
							if ( $status_name !== $ddfwm_out_for_delivery_status_name ) {
								$ddfwm_out_for_delivery_status_name = $status_name;
							}
							break;
						case get_option( 'ddfwm_failed_attempt_status' ):
							if ( esc_html( __( 'Failed Delivery Attempt', 'ddfwm' ) ) !== $status_name ) {
								$ddfwm_failed_attempt_status_name = $status_name;
							}
							break;
						case get_option( 'ddfwm_vendor_assigned_status' ):
							if ( $status_name !== $ddfwm_vendor_assigned_status_name ) {
								$ddfwm_vendor_assigned_status_name = $status_name;
							}
							break;
					}
				}
			}
		}


		foreach ( $ddfwm_array as $row ) {

			switch ( $row->post_status ) {
				case get_option( 'ddfwm_out_for_delivery_status' ):
					$ddfwm_out_for_delivery_counter = $row->orders;
					break;
				case get_option( 'ddfwm_failed_attempt_status' ):
					$ddfwm_failed_attempt_counter = $row->orders;
					break;
				case get_option( 'ddfwm_delivered_status' ):
					$ddfwm_delivered_counter = $row->orders;
					break;
				case get_option( 'ddfwm_vendor_assigned_status' ):
					$ddfwm_assign_to_driver_counter = $row->orders;
					break;
			}
		}
	}

}
