<?php

namespace Bright_Deposits_For_WooCommerce\Setup;

/**
 * @package Bright_Deposits_For_WooCommerce\Setup
 * @since 1.0.0
 * @author Bright Plugins
 * Description: This class is responsible for setting up the deposit setup for the plugin.
 */
class DepositSetup {
	public function __construct() {
		add_action( 'init', [$this, 'registerOrderType'], 10 );
		//add_action( 'admin_init', [$this, 'blockAccessAddShopDeposit'] );
		add_action( 'admin_enqueue_scripts', [$this, 'hideButtonAddShopDeposit'] );
		add_action( 'woocommerce_register_shop_order_post_statuses', [$this, 'registerStatusDepositOrders'], 10, 1 );
		add_filter( 'wc_order_statuses', [$this, 'showsOrderStatus'] );
	}

	/**
	 * Register the order type for the plugin.
	 * @since 1.0.0
	 * @return void
	 */
	public function registerOrderType() {
		/**
		 * BDFWBP_CUSTOM_ACTION_HOOK: Allows developers to modify the arguments for the register deposit order type.
		 * @since 1.0.0
		 * @param array $args
		 * @return array
		 */
		$args = apply_filters( 'bdfwbp_register_order_type_args', array(
			'labels'                           => array(
				'name'      => __( 'Deposit Payments', 'bright-deposits-for-woocommerce' ),
				'menu_name' => _x( 'Deposit Payments', 'Admin menu name', 'bright-deposits-for-woocommerce' ),
			),
			'description'                      => __( 'This is where store Deposit Payments are stored.', 'bright-deposits-for-woocommerce' ),
			'public'                           => false,
			'show_ui'                          => true,
			'capability_type'                  => 'shop_order',
			'map_meta_cap'                     => true,
			'publicly_queryable'               => false,
			'exclude_from_search'              => true,
			'show_in_menu'                     => current_user_can( 'manage_woocommerce' ) ? 'woocommerce' : true,
			'hierarchical'                     => false,
			'show_in_nav_menus'                => false,
			'capabilities'                     => array(
				'create_posts' => 'do_not_allow',
			),
			'query_var'                        => false,
			'supports'                         => array( 'title', 'custom-fields' ),
			'has_archive'                      => false,
			'exclude_from_orders_screen'       => true,
			'add_order_meta_boxes'             => true,
			'exclude_from_order_count'         => true,
			'exclude_from_order_views'         => true,
			'exclude_from_order_webhooks'      => true,
			'exclude_from_order_reports'       => true,
			'exclude_from_order_sales_reports' => false,
            'class_name'                       => 'Bright_Deposits_For_WooCommerce\Core\OrderDeposit',
        ) );

		wc_register_order_type( 'shop_deposit', $args );
	}

	/**
	 * Add the order status for the plugin.
	 * @since 1.0.0
	 * @param array $statuses
	 * @return array
	 */
	public function registerStatusDepositOrders( $statuses ) {
		$statuses['wc-partially-paid'] = [
			'label'                     => _x( 'Partially Paid', 'Order status', 'bright-deposits-for-woocommerce' ),
			'public'                    => false,
			'exclude_from_search'       => false,
			'show_in_admin_all_list'    => true,
			'show_in_admin_status_list' => true,
			// translators: %s: number of partially paid orders.
			'label_count'               => _n_noop( 'Partially Paid <span class="count">(%s)</span>', 'Partially Paid <span class="count">(%s)</span>', 'bright-deposits-for-woocommerce' ),
		];

		/**
		 * BDFWBP_CUSTOM_ACTION_HOOK: Allows developers to register other order statuses to the plugin.
		 * @since 1.0.0
		 * @param array $statuses
		 * @return array
		 */
		return apply_filters( 'bdfwbp_register_shop_order_post_statuses', $statuses );
	}

	/**
	 * Add the order status for the plugin.
	 * @since 1.0.0
	 * @param array $order_statuses
	 * @return array
	 */
	public function showsOrderStatus( $order_statuses ) {
		$order_statuses['wc-partially-paid'] = _x( 'Partially Paid', 'Order status', 'bright-deposits-for-woocommerce' );
		
		/**
		 * BDFWBP_CUSTOM_ACTION_HOOK: Allows developers to add the order statuses to the plugin.
		 * @since 1.0.0
		 * @param array $order_statuses
		 * @return array
		 */
		return apply_filters( 'bdfwbp_order_statuses', $order_statuses );
	}

	/**
	 * Hide the button to add a new deposit order.
	 * @since 1.0.0
	 * @param string $hook_suffix
	 * @return void
	 */
	public function hideButtonAddShopDeposit( $hook_suffix ) {
		if ( $hook_suffix == 'woocommerce_page_wc-orders--shop_deposit' ) {
			wp_enqueue_style(
				'bdfwbp-admin-style',
				BDFWBP_ASSETS . '/css/admin-style.css',
				array(),
				'1.0'
			);
		}	
    } 

	/**
	 * Block the access to add a new deposit order.
	 * @since 1.0.0
	 * @return void
	 */
	/*public function blockAccessAddShopDeposit() {
		global $pagenow;

		$page = ( isset( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
		$action = ( isset( $_GET['action'] ) ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';

		if ( $pagenow === 'admin.php' && !empty( $page ) && $page === 'wc-orders--shop_deposit' && !empty( $action ) && $action === 'new' ) {
			wp_redirect( admin_url() );
			exit;
		}
	}*/

}