<?php

/**
 * Mysterx Admin Class
 *
 * @file
 * @package    Mysterx
 * @subpackage Mysterx/admin
 */
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
    die;
}
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://wpgenie.org
 * @since      1.0.0
 *
 * @package    Mysterx
 * @subpackage Mysterx/admin
 */
class Mysterx_Admin {
    /**
     * 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 this 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 admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_styles() {
        wp_enqueue_style(
            $this->plugin_name,
            plugin_dir_url( __FILE__ ) . 'css/mysterx-admin.css',
            array(),
            $this->version,
            'all'
        );
    }

    /**
     * Register the JavaScript for the admin area.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts() {
        wp_enqueue_script(
            $this->plugin_name,
            plugin_dir_url( __FILE__ ) . 'js/mysterx-admin.js',
            array('jquery', 'jquery-ui-sortable'),
            $this->version,
            false
        );
        $localize_data = array(
            'search_product' => esc_html__( 'Search for a product&hellip;', 'mysterx-mystery-boxes-for-woocommerce' ),
        );
        wp_localize_script( $this->plugin_name, 'MYSTERX_ADMIN', $localize_data );
    }

    /**
     * Add Mysterx product class.
     *
     * @param  mixed $classname classname.
     * @param  mixed $product_type product type.
     * @return string
     */
    public function mysterx_product_class( $classname, $product_type ) {
        if ( 'mysterx' === $product_type ) {
            return 'Mysterx_Product';
        }
        return $classname;
    }

    /**
     * Mysterx admin menu
     *
     * @return void
     */
    public function mysterx_admin_menu() {
        $wmysterx_icon = plugin_dir_url( __FILE__ ) . '/img/icon.png';
        add_menu_page(
            'Mystery Box',
            'Mystery Box',
            'manage_woocommerce',
            'mysterx',
            false,
            $wmysterx_icon,
            56
        );
        add_submenu_page(
            'mysterx',
            'All boxes',
            'All boxes',
            'manage_woocommerce',
            'mysterx',
            'Mysterx_Admin::mm_mysterx_all_callback',
            10
        );
        add_submenu_page(
            'mysterx',
            'Add new',
            'Add new',
            'manage_woocommerce',
            'mysterx-add',
            'Mysterx_Admin::mm_mysterx_add_callback',
            20
        );
    }

    /**
     * Call back for mm_mysterx_all
     *
     * @return void
     */
    public static function mm_mysterx_all_callback() {
        wp_safe_redirect( admin_url( 'edit.php?post_type=product&product_type=mysterx&mysterxmenu=1' ) );
        exit;
    }

    /**
     * Call back for mm_mysterx_add
     *
     * @return void
     */
    public static function mm_mysterx_add_callback() {
        wp_safe_redirect( admin_url( 'post-new.php?post_type=product&product_type=mysterx&mysterxmenu=1' ) );
        exit;
    }

    /**
     * Adds the mysterxmenu param to admin URLs
     *
     * @param  mixed $url url.
     * @param  mixed $post_id post id.
     * @param  mixed $context context.
     * @return mixed url
     */
    public function mysterx_add_menu_param( $url, $post_id, $context ) {
        if ( isset( $_GET['mysterxmenu'] ) && '1' === $_GET['mysterxmenu'] ) {
            $url = add_query_arg( 'mysterxmenu', '1', $url );
        }
        return $url;
    }

    /**
     * Sets the parent file for mysterx products
     *
     * @param  mixed $parent_file parent file.
     * @return mixed parent file
     */
    public function mysterx_parent_file( $parent_file ) {
        $screen = get_current_screen();
        if ( !$screen ) {
            return $parent_file;
        }
        if ( 'edit-product' === $screen->id && isset( $_GET['product_type'] ) && 'mysterx' === $_GET['product_type'] ) {
            return 'mysterx';
        }
        if ( 'product' === $screen->id && 'post' === $screen->base && isset( $_GET['product_type'] ) && 'mysterx' === $_GET['product_type'] ) {
            return 'mysterx';
        }
        return $parent_file;
    }

    /**
     * Sets the submenu file for mysterx products
     *
     * @param  mixed $submenu_file submenu file.
     * @return mixed submenu file
     */
    public function mysterx_submenu_file( $submenu_file ) {
        $screen = get_current_screen();
        if ( !$screen ) {
            return $submenu_file;
        }
        if ( 'edit-product' === $screen->id && isset( $_GET['product_type'] ) && 'mysterx' === $_GET['product_type'] ) {
            return 'mysterx-all';
        }
        if ( 'product' === $screen->id && 'post' === $screen->base && isset( $_GET['product_type'] ) && 'mysterx' === $_GET['product_type'] ) {
            return 'mysterx-add';
        }
        return $submenu_file;
    }

    /**
     * Change admin title contextually
     *
     * @param  mixed $current_screen current screen.
     * @return void
     */
    public function mysterx_change_admin_title_contextually( $current_screen ) {
        if ( ('edit-product' === $current_screen->id || 'product' === $current_screen->id) && isset( $_GET['product_type'] ) && 'mysterx' === $_GET['product_type'] && isset( $_GET['mysterxmenu'] ) && '1' === $_GET['mysterxmenu'] ) {
            $product_post_type_object = get_post_type_object( 'product' );
            if ( is_object( $product_post_type_object ) ) {
                $product_post_type_object->labels->name = __( 'Mystery Box', 'mysterx-mystery-boxes-for-woocommerce' );
                $product_post_type_object->labels->singular_name = __( 'Mystery Box', 'mysterx-mystery-boxes-for-woocommerce' );
                $product_post_type_object->labels->edit_item = __( 'Edit Mystery Box', 'mysterx-mystery-boxes-for-woocommerce' );
                $product_post_type_object->labels->add_new_item = __( 'Add new Mystery Box', 'mysterx-mystery-boxes-for-woocommerce' );
            }
        }
    }

    /**
     * Add to product type selector
     *
     * @param  mixed $types types.
     * @return mixed types
     */
    public function add_to_product_type_selector( $types ) {
        $types['mysterx'] = __( 'Mystery box', 'mysterx-mystery-boxes-for-woocommerce' );
        return $types;
    }

    /**
     * Product write panel tab
     *
     * @param  mixed $product_data_tabs product data tabs.
     * @return mixed product data tabs
     */
    public function product_write_panel_tab( $product_data_tabs ) {
        $product_data_tabs['mysterx_tab'] = array(
            'label'  => __( 'Mysterx Boxes', 'mysterx-mystery-boxes-for-woocommerce' ),
            'target' => 'mysterx_tab',
            'class'  => array(
                'mysterx_tab',
                'show_if_mysterx',
                'hide_if_grouped',
                'hide_if_external',
                'hide_if_variable',
                'hide_if_simple'
            ),
        );
        return $product_data_tabs;
    }

    /**
     * Product write panel
     *
     * @return void
     */
    public function product_write_panel() {
        global $post;
        $product = wc_get_product( $post->ID );
        $rewards = mysterx_get_items_for_product( $post->ID );
        echo '<div id="mysterx_tab" class="panel woocommerce_options_panel">';
        wp_nonce_field( 'mysterx_meta_nonce', 'mysterx_meta_nonce_field' );
        ?>

		<div class="options_group">

			<p class="description"><strong><?php 
        esc_html_e( 'Configure Reward Products', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></strong></p>

			<p class="description"><?php 
        esc_html_e( 'Each row represents an item that can be awarded. Quantity is how many total of that item can be won (0 = unlimited).', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></p>

			<div class="wc-crates-repeater">
				<div class="wc-crates-header">
					<div class="col-handle"></div>
					<div class="col-product"><?php 
        esc_html_e( 'Product', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></div>
					<?php 
        ?>
					<div class="col-qty"><?php 
        esc_html_e( 'Limit Stock', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></div>
					<div class="col-stock"><?php 
        esc_html_e( 'Available Stock', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></div>
					<div class="col-action"></div>
				</div>

				<div id="wc-crates-repeater-body">
					<?php 
        foreach ( $rewards as $idx => $it ) {
            $rew_id = ( isset( $it['reward_id'] ) ? intval( $it['reward_id'] ) : 0 );
            $weight = ( isset( $it['weight'] ) ? intval( $it['weight'] ) : 1 );
            $qty = ( isset( $it['qty'] ) ? intval( $it['qty'] ) : 0 );
            $prod = ( $rew_id ? wc_get_product( $rew_id ) : false );
            $stock = ( $prod ? ( $prod->managing_stock() ? $prod->get_stock_quantity() : '∞' ) : '—' );
            ?>
						<div class="row" data-index="<?php 
            echo esc_attr( $idx );
            ?>">
							<div class="col-handle"><span class="dashicons dashicons-menu"></span></div>

							<div class="col-product">
								<select class="wc-product-search" style="width: 100%;" name="mysterx_items[<?php 
            echo esc_attr( $idx );
            ?>][product_id]">
									<?php 
            if ( $prod ) {
                echo '<option value="' . esc_attr( $rew_id ) . '" selected>' . esc_html( $prod->get_name() ) . '</option>';
            }
            ?>
								</select>
							</div>

							<?php 
            ?>

							<div class="col-qty">
								<input type="number" name="mysterx_items[<?php 
            echo esc_attr( $idx );
            ?>][quantity]" value="<?php 
            echo esc_attr( $qty );
            ?>" min="0" />
							</div>

							<div class="col-stock">
								<?php 
            echo esc_html( $stock );
            ?>
							</div>

							<div class="col-action">
								<button type="button" class="remove">×</button>
							</div>
						</div>
					<?php 
        }
        ?>
				</div>
			</div>

			<p>
				<button type="button" class="button button-primary" id="wc-crates-add-row">+ <?php 
        esc_html_e( 'Add Item', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></button>
			</p>


			<p class="description"><?php 
        esc_html_e( 'Weight determines relative chance (higher means higher chance), PRO feature.', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></p>

			<p class="description"><?php 
        esc_html_e( 'Rarity classes are determined by relative chance: common - 50, uncommon - 30, rare - 15, epic - 4, legendary - 1, PRO feature.', 'mysterx-mystery-boxes-for-woocommerce' );
        ?></p>
			

			<input type='hidden' id="mysterx_items_json" name='mysterx_items_json' >
		</div>
		</div>
		<?php 
    }

    /**
     * Product save data
     *
     * @param  mixed $post_id post id.
     * @param  mixed $post post.
     * @return void
     */
    public function product_save_data( $post_id, $post ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
        if ( !current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
        if ( !isset( $_POST['mysterx_meta_nonce_field'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['mysterx_meta_nonce_field'] ) ), 'mysterx_meta_nonce' ) ) {
            return;
        }
        global $wpdb;
        $items = array();
        if ( isset( $_POST['mysterx_items_json'] ) ) {
            $safe = sanitize_text_field( wp_unslash( $_POST['mysterx_items_json'] ) );
            $decoded = json_decode( $safe, true );
            $current_reward_ids = array();
            if ( is_array( $decoded ) ) {
                foreach ( $decoded as $row ) {
                    if ( isset( $row['product_id'] ) ) {
                        $current_reward_ids[] = intval( $row['product_id'] );
                    }
                }
                if ( !empty( $current_reward_ids ) ) {
                    $db_items = $wpdb->get_col( $wpdb->prepare( 'SELECT reward_id FROM %i WHERE product_id = %d', MYSTERX_TBL_ITEMS, $post_id ) );
                    $to_delete = array_diff( $db_items, $current_reward_ids );
                    if ( !empty( $to_delete ) ) {
                        foreach ( $to_delete as $id_to_del ) {
                            $wpdb->delete( MYSTERX_TBL_ITEMS, array(
                                'product_id' => $post_id,
                                'reward_id'  => $id_to_del,
                            ), array('%d', '%d') );
                        }
                    }
                } else {
                    $wpdb->delete( MYSTERX_TBL_ITEMS, array(
                        'product_id' => $post_id,
                    ), array('%d') );
                }
                foreach ( $decoded as $idx => $row ) {
                    $reward_id = ( isset( $row['product_id'] ) ? intval( $row['product_id'] ) : 0 );
                    $weight = ( isset( $row['weight'] ) ? max( 0, intval( $row['weight'] ) ) : 1 );
                    $quantity = ( isset( $row['quantity'] ) ? max( 0, intval( $row['quantity'] ) ) : 0 );
                    $tbl = $wpdb->prefix . 'mysterx_items';
                    $exists = $wpdb->get_var( $wpdb->prepare(
                        'SELECT id FROM %i WHERE product_id=%d AND reward_id = %d  LIMIT 1',
                        $tbl,
                        $post_id,
                        $reward_id
                    ) );
                    if ( $reward_id ) {
                        if ( $exists ) {
                            $wpdb->update(
                                $tbl,
                                array(
                                    'reward_id' => $reward_id,
                                    'weight'    => $weight,
                                    'qty'       => $quantity,
                                    'order'     => $idx,
                                ),
                                array(
                                    'product_id' => $post_id,
                                    'reward_id'  => $reward_id,
                                ),
                                array(
                                    '%d',
                                    '%d',
                                    '%d',
                                    '%d'
                                ),
                                array('%d', '%d')
                            );
                        } else {
                            $wpdb->insert( $tbl, array(
                                'product_id' => $post_id,
                                'reward_id'  => $reward_id,
                                'weight'     => $weight,
                                'qty'        => $quantity,
                                'order'      => $idx,
                            ), array(
                                '%d',
                                '%d',
                                '%d',
                                '%d',
                                '%d'
                            ) );
                        }
                    }
                }
            }
        }
    }

    /**
     * Mystery Box order
     *
     * Checks for mysterx product in order and assign order id to mysterx product
     *
     * @param int $order_id Order ID.
     * @return void
     */
    public function order( $order_id ) {
        global $wpdb;
        $log = $wpdb->get_row( $wpdb->prepare( 'SELECT 1 FROM ' . $wpdb->prefix . 'mysterx_log WHERE order_id=%d LIMIT 1', $order_id ) );
        if ( !is_null( $log ) ) {
            return;
        }
        $order = new WC_Order($order_id);
        if ( $order ) {
            if ( $order->get_meta( 'mysterx_proccesed' ) ) {
                return;
            }
            $parent_order_id = wp_get_post_parent_id( $order_id );
            if ( 0 !== intval( $parent_order_id ) ) {
                return;
            }
            $order->update_meta_data( 'mysterx_processed', time() );
            $order->save();
            $order_items = $order->get_items();
            if ( $order_items ) {
                foreach ( $order_items as $item_id => $item ) {
                    $item_meta = wc_get_order_item_meta( $item_id, '' );
                    $product_id = $this->get_main_wpml_product_id( $item_meta['_product_id'][0] );
                    $product_data = wc_get_product( $product_id );
                    if ( $product_data && $product_data->get_type() === 'mysterx' ) {
                        update_post_meta( $order_id, '_mysterx', '1' );
                        delete_post_meta( $product_id, '_order_hold_on', $order_id );
                        $log_ids = array();
                        if ( apply_filters(
                            'mysterx_from_order',
                            true,
                            $item,
                            $order_id,
                            $product_id
                        ) ) {
                            $qty = intval( $item_meta['_qty'][0] );
                            $log_ids = $this->log_participant(
                                $product_id,
                                $order->get_user_id(),
                                $order_id,
                                $item,
                                $qty
                            );
                            do_action(
                                'mysterx_participate_added',
                                $product_id,
                                $order->get_user_id(),
                                $order_id,
                                $log_ids,
                                $item,
                                $item_id
                            );
                        } else {
                            do_action(
                                'mysterx_participate_not_added',
                                $product_id,
                                $order->get_user_id(),
                                $order_id,
                                $log_ids,
                                $item,
                                $item_id
                            );
                        }
                        do_action(
                            'mysterx_participate',
                            $product_id,
                            $order->get_user_id(),
                            $order_id,
                            $log_ids,
                            $item,
                            $item_id
                        );
                    }
                }
            }
        }
    }

    /**
     * Mystery Box order canceled
     *
     * Checks for mysterx product in order and assign order id to mysterx product
     *
     * @param int $order_id Order ID.
     * @return void
     */
    public function order_canceled( $order_id ) {
        global $wpdb;
        $log = $wpdb->get_row( $wpdb->prepare( 'SELECT 1 FROM ' . $wpdb->prefix . 'mysterx_log WHERE order_id=%d LIMIT 1', $order_id ) );
        if ( is_null( $log ) ) {
            return;
        }
        $order = new WC_Order($order_id);
        if ( $order ) {
            $order_items = $order->get_items();
            if ( $order_items ) {
                foreach ( $order_items as $item_id => $item ) {
                    $item_meta = wc_get_order_item_meta( $item_id, '' );
                    $product_id = $this->get_main_wpml_product_id( $item_meta['_product_id'][0] );
                    $product_data = wc_get_product( $product_id );
                    if ( $product_data ) {
                        if ( 'mysterx' === $product_data->get_type() ) {
                            delete_post_meta( $product_id, '_order_hold_on', $order_id );
                            $log_ids = array();
                            if ( apply_filters(
                                'mysterx_remove_from_user',
                                true,
                                $item,
                                $order_id,
                                $product_id
                            ) ) {
                                for ($i = 0; $i < $item_meta['_qty'][0]; $i++) {
                                    $log_ids[] = $this->delete_log_participant( $product_id, $order->get_user_id(), $order_id );
                                }
                                do_action(
                                    'mysterx_remove_box_from_user',
                                    $product_id,
                                    $order->get_user_id(),
                                    $order_id,
                                    $log_ids,
                                    $item,
                                    $item_id
                                );
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * Mystery Box order failed
     *
     * Checks for mysterx product in failed order
     *
     * @access public
     * @param int $order_id Order ID.
     * @return void
     */
    public function order_failed( $order_id ) {
        global $wpdb;
        $order = new WC_Order($order_id);
        if ( $order ) {
            $order_items = $order->get_items();
            if ( $order_items ) {
                foreach ( $order_items as $item_id => $item ) {
                    $item_meta = wc_get_order_item_meta( $item_id, '' );
                    $product_id = $this->get_main_wpml_product_id( $item_meta['_product_id'][0] );
                    $product_data = wc_get_product( $product_id );
                    if ( $product_data ) {
                        $product_data_type = ( method_exists( $product_data, 'get_type' ) ? $product_data->get_type() : $product_data->product_type );
                        if ( 'mysterx' === $product_data->get_type() ) {
                            delete_post_meta( $product_id, '_order_hold_on', $order_id );
                        }
                    }
                    do_action(
                        'mysterx_order_failed',
                        $product_id,
                        $order->get_user_id(),
                        $order_id,
                        $log_ids = null,
                        $item,
                        $item_id
                    );
                }
            }
        }
    }

    /**
     * Log Mystery Box participant
     *
     * @param  int      $product_id Product ID.
     * @param  int      $current_user_id Current user ID.
     * @param  int      $order_id Order ID.
     * @param  mixed    $item Order item.
     * @param  int|bool $qty Quantity.
     * @return int|array Log ID or array of log IDs
     */
    public function log_participant(
        $product_id,
        $current_user_id,
        $order_id,
        $item,
        $qty = false
    ) {
        global $wpdb;
        if ( $qty ) {
            $values = array();
            $place_holders = array();
            $first_id = 0;
            $last_id = 0;
            $log_ids = array();
            $wpdb->query( 'START TRANSACTION' );
            for ($i = 0; $i < intval( $qty ); $i++) {
                $wpdb->insert( $wpdb->prefix . 'mysterx_log', array(
                    'user_id'    => $current_user_id,
                    'product_id' => $product_id,
                    'order_id'   => $order_id,
                    'date'       => current_time( 'mysql' ),
                ), array(
                    '%d',
                    '%d',
                    '%d',
                    '%s'
                ) );
                if ( 0 === $i ) {
                    $first_id = $wpdb->insert_id;
                }
                if ( intval( $qty ) - 1 === $i ) {
                    $last_id = $wpdb->insert_id;
                }
            }
            $wpdb->query( 'COMMIT' );
            if ( 1 === $qty ) {
                return array($first_id);
            } else {
                $auto_increment_increment = ($last_id - $first_id) / ($qty - 1);
                $log_ids = range( $first_id, $last_id, $auto_increment_increment );
            }
            return $log_ids;
        } else {
            $wpdb->insert( $wpdb->prefix . 'mysterx_log', array(
                'user_id'    => $current_user_id,
                'product_id' => $product_id,
                'order_id'   => $order_id,
                'date'       => current_time( 'mysql' ),
            ), array(
                '%d',
                '%d',
                '%d',
                '%s'
            ) );
            return $wpdb->insert_id;
        }
    }

    /**
     * Delete Mystery Box participant log entry
     *
     * @param  int $product_id Product ID.
     * @param  int $current_user_id Current user ID.
     * @param  int $order_id Order ID.
     * @return int Log ID
     */
    public function delete_log_participant( $product_id, $current_user_id, $order_id ) {
        global $wpdb;
        $log_id = $wpdb->get_var( $wpdb->prepare(
            'SELECT id FROM ' . $wpdb->prefix . 'mysterx_log  WHERE user_id= %d AND product_id=%d AND order_id=%d LIMIT 1',
            $current_user_id,
            $product_id,
            $order_id
        ) );
        if ( $log_id ) {
            $wpdb->query( $wpdb->prepare(
                'DELETE FROM ' . $wpdb->prefix . 'mysterx_log WHERE user_id= %d AND product_id=%d AND order_id=%d',
                $current_user_id,
                $product_id,
                $order_id
            ) );
        }
        return $log_id;
    }

    /**
     * Get main product id for multilanguage purpose
     *
     * @param int $id Product ID.
     * @return int
     */
    public function get_main_wpml_product_id( $id ) {
        return intval( apply_filters(
            'wpml_object_id',
            $id,
            'product',
            false,
            apply_filters( 'wpml_default_language', null )
        ) );
    }

    /**
     * Force virtual property for mysterx products
     *
     * @param  mixed $is_virtual is virtual.
     * @param  mixed $product product.
     * @return mixed is virtual
     */
    public function mysterx_force_virtual_property( $is_virtual, $product ) {
        if ( 'mysterx' === $product->get_type() ) {
            return true;
        }
    }

    /**
     * Force downloadable property for mysterx products
     *
     * @param  mixed $is_downloadable is downloadable.
     * @param  mixed $product product.
     * @return mixed is downloadable
     */
    public function mysterx_force_downloadable_property( $is_downloadable, $product ) {
        if ( 'mysterx' === $product->get_type() ) {
            return true;
        }
    }

    /**
     * Register custom order status for rewards
     *
     * @return void
     */
    public function register_reward_order_status() {
        register_post_status( 'wc-reward', array(
            'label'                     => _x( 'Reward', 'Order status', 'mysterx-mystery-boxes-for-woocommerce' ),
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 
                /* translators: %s: Reward count. */
                'Reward <span class="count">(%s)</span>',
                'Reward <span class="count">(%s)</span>',
                'mysterx-mystery-boxes-for-woocommerce'
             ),
        ) );
    }

    /**
     * Add reward order status to order status list
     *
     * @param  mixed $order_statuses order statuses.
     * @return mixed order statuses.
     */
    public function add_reward_to_order_statuses( $order_statuses ) {
        $new_order_statuses = array();
        foreach ( $order_statuses as $key => $status ) {
            $new_order_statuses[$key] = $status;
            if ( 'wc-processing' === $key ) {
                $new_order_statuses['wc-reward'] = _x( 'Reward', 'Order status', 'mysterx-mystery-boxes-for-woocommerce' );
            }
        }
        return $new_order_statuses;
    }

    /**
     * Add reward order status to stock reduction list
     *
     * @param  mixed $statuses statuses.
     * @return mixed statuses
     */
    public function add_reward_status_to_stock_reduction( $statuses ) {
        $statuses[] = 'reward';
        return $statuses;
    }

    /**
     * Add reward email class to WooCommerce email classes
     *
     * @param  mixed $email_classes email classes.
     * @return mixed email classes
     */
    public function add_reward_email_to_woocommerce( $email_classes ) {
        include_once 'emails/class-mysterx-email-new-reward.php';
        include_once 'emails/class-mysterx-email-customer-new-reward.php';
        $email_classes['Mysterx_Email_New_Reward'] = new Mysterx_Email_New_Reward();
        $email_classes['Mysterx_Email_Customer_New_Reward'] = new Mysterx_Email_Customer_New_Reward();
        return $email_classes;
    }

    /**
     * Trigger reward status email when order status changes to reward
     *
     * @param  mixed $order_id order id.
     * @param  mixed $order order.
     * @return void
     */
    public function trigger_reward_status_email( $order_id, $order ) {
        WC()->mailer()->get_emails()['Mysterx_Email_New_Reward']->trigger( $order_id, $order );
        WC()->mailer()->get_emails()['Mysterx_Email_Customer_New_Reward']->trigger( $order_id, $order );
    }

}
