<?php
namespace Bright_Deposits_For_WooCommerce\Emails;

use Bright_Deposits_For_WooCommerce\Helpers\Helper;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\NewDepositOrder;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\PaidDepositOrder;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\PaymentDepositReminder;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\FullyPaidDepositOrder;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\AdminNewDepositOrder;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\AdminPaidDepositOrder;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\AdminPaymentDepositReminder;
use Bright_Deposits_For_WooCommerce\Emails\Notifications\AdminFullyPaidDepositOrder;

/**
 * @package Bright_Deposits_For_WooCommerce\Emails
 * @since 1.0.0
 * @author Bright Plugins
 * Description: This class is responsible for managing the emails for deposit orders.
 */
class DepositEmail {

    public function __construct() {
        add_filter( 'woocommerce_email_classes', [$this, 'emailClasses'] );
        add_action( 'woocommerce_email_order_meta', [$this, 'customerDepositDetails'], 10, 1 );
        add_action( 'bdfwbp_email_show_deposit_details', [$this, 'depositTable'], 20, 1 );
		add_filter( 'woocommerce_email_enabled_new_order', [$this, 'disableDefaultEmailNewOrder'], 10, 2 );
		add_filter( 'woocommerce_email_enabled_customer_completed_order', [$this, 'disableDefaultEmailCustomerCompletedOrder'], 10, 2 );
		add_filter( 'woocommerce_email_enabled_customer_processing_order', [$this, 'disableDefaultEmailCustomerProcessingOrder'], 10, 2 );
    }

	
	/**
	 * Disable the default new order email for deposit orders.
	 * @since 1.0.0
	 * @param  bool     $enabled
	 * @param  \WC_Order $order
	 * @return bool
	 */
	public function disableDefaultEmailNewOrder( bool $enabled, $order ): bool {
		if ( ! $order instanceof \WC_Order ) {
			return $enabled;
		}

		/**
		 * Get the order id and type. 
		 * If the order is a deposit order or a deposit child order, return false.
		 */
		$is_deposit_order = Helper::isDepositOrder( $order->get_id() );
		$is_deposit_child_order = 'shop_deposit' === $order->get_type();

		if ( $is_deposit_order || $is_deposit_child_order ) {
			return false;
		}

		return $enabled;
	}

	/**
	 * Disable the default customer completed order email for deposit orders.
	 * @since 1.0.0
	 * @param  bool     $enabled
	 * @param  \WC_Order $order
	 * @return bool
	 */
	public function disableDefaultEmailCustomerCompletedOrder( bool $enabled, $order ): bool {
		if ( ! $order instanceof \WC_Order ) {
			return $enabled;
		}

		/**
		 * Get the order id and type. 
		 * If the order is a deposit order or a deposit child order, return false.
		 */
		$is_deposit_order = Helper::isDepositOrder( $order->get_id() );
		$is_deposit_child_order = 'shop_deposit' === $order->get_type();

		if ( $is_deposit_order || $is_deposit_child_order ) {
			return false;
		}

		return $enabled;
	}

	/**
	 * Disable the default customer processing order email for deposit orders.
	 * @since 1.0.0
	 * @param  bool     $enabled
	 * @param  \WC_Order $order
	 * @return bool
	 */
	public function disableDefaultEmailCustomerProcessingOrder( bool $enabled, $order ): bool {
		if ( ! $order instanceof \WC_Order ) {
			return $enabled;
		}

		/**
		 * Get the order id and type. 
		 * If the order is a deposit order, return false.
		 */
		$is_deposit_order = Helper::isDepositOrder( $order->get_id() );

		if ( $is_deposit_order ) {
			return false;
		}

		return $enabled; 
	}

	/**
	 * Display the deposit table on the email.
	 * @since 1.0.0
	 * @param  \WC_Order $order
	 * @return void
	 */
	public function depositTable( \WC_Order $order ): void {

		/**
		 * BDFWBP_CUSTOM_ACTION_HOOK: Allows developers to add custom content before the deposit table.
		 * @since 1.0.0
		 * @param  \WC_Order $order
		 * @return void
		 */
		do_action( 'bdfwbp_before_deposit_table_email', $order );
		?>
            <table border="0" cellpadding="20" cellspacing="0" style="width:100%; margin-bottom:15px">
                <thead>
                <tr>
                    <th class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;"><?php echo esc_html( Helper::getOptionValue( 'bdfw_payment_id_label' ) ); ?> </th>
                    <th class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;"><?php echo esc_html( Helper::getOptionValue( 'bdfw_status_label' ) ); ?> </th>
                    <th class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;"><?php echo esc_html( Helper::getOptionValue( 'bdfw_amount_label' ) ); ?> </th>
                    <th class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;"><?php echo esc_html( Helper::getOptionValue( 'bdfw_order_label' ) ); ?> </th>
                </tr>
                </thead>
                <tbody>
                    <tr class="order_item">
                        <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;">
                        <?php echo '<strong>#' . esc_html( $order->get_meta( '_deposit_id', true ) ) . '</strong>'; ?>
                        </td>
                        <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;">
                        <?php $depositStatus = $order->get_status(); // order status ?>
                        <?php echo esc_html( wc_get_order_status_name( $depositStatus ) ); ?>
                        </td>
                        <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;">
                        <?php echo wp_kses_post( wc_price( $order->get_total() ) ); ?>
                        </td>
                        <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px;">
                        <?php
						$parentOrder = wc_get_order( $order->get_parent_id() );
						if ( $parentOrder ) {
							echo '<a href="' . esc_url( $parentOrder->get_view_order_url() ) . '">' . esc_html( $order->get_parent_id() ) . '</a>';
						} else {
							echo esc_html( $order->get_id() );
						}
						?>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                </tfoot>
            </table>
    	<?php 
		/**
		 * BDFWBP_CUSTOM_ACTION_HOOK: Allows developers to add custom content after the deposit table.
		 * @since 1.0.0
		 * @param  \WC_Order $order
		 * @return void
		 */
		do_action( 'bdfwbp_after_deposit_table_email', $order );
	}

	/**
	 * Display the deposit summary on the email.
	 * @since 1.0.0
	 * @param  \WC_Order $order
	 * @return void
	 */
	public function customerDepositDetails( \WC_Order $order ): void {
		if ( !Helper::isDepositOrder( $order->get_id() ) ) {
			return;
		}
		wc_get_template( 'emails/deposit-summary.php', array( 'order' => $order ), '', BDFWBP_TEMPLATE_PATH );
	}

	/**
	 * Register new email classes. 
	 * @since 1.0.0
	 * @param  array  $classes
	 * @return array
	 */
	public function emailClasses( array $classes ): array {
		$classes['WC_Customer_New_Deposit_Order']        = new NewDepositOrder();
		$classes['WC_Customer_Paid_Deposit_Order']       = new PaidDepositOrder();
		$classes['WC_Customer_Payment_Deposit_Reminder'] = new PaymentDepositReminder();
		$classes['WC_Customer_Fully_Paid_Deposit']       = new FullyPaidDepositOrder();

		$classes['WC_Admin_New_Deposit_Order']        = new AdminNewDepositOrder();
		$classes['WC_Admin_Paid_Deposit_Order']       = new AdminPaidDepositOrder();
		$classes['WC_Admin_Payment_Deposit_Reminder'] = new AdminPaymentDepositReminder();
		$classes['WC_Admin_Fully_Paid_Deposit']       = new AdminFullyPaidDepositOrder();

		/**
		 * BDFWBP_CUSTOM_FILTER_HOOK: Allows developers to add custom email classes.
		 * @since 1.0.0
		 * @param  array  $classes
		 * @return array
		 */
		return apply_filters( 'bdfwbp_classes_deposit_email', $classes );
	}
}