<?php
/**
 * CommerceBird Email: Admin new quote request notification
 *
 * @package CommerceBird\Includes\Quotes\Emails
 */

defined( 'ABSPATH' ) || exit;

/**
 * CMBIRD_Email_New_Quote Class
 */
class CMBIRD_Email_New_Quote extends WC_Email {

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->id             = 'cmbird_new_quote';
		$this->title          = __( 'New quote', 'commercebird' );
		$this->description    = __( 'Send email notification to admin when a new quote request is received.', 'commercebird' );
		$this->template_html  = 'emails/new-quote.php';

		add_action( 'woocommerce_order_status_pending_to_on-quote_notification', array( $this, 'trigger' ), 10, 2 );

		parent::__construct();

		$this->template_base = CMBIRD_PATH . 'templates/';
		$this->recipient     = $this->get_option( 'recipient', get_option( 'admin_email' ) );
	}

	/**
	 * Get default email subject
	 *
	 * @return string
	 */
	public function get_default_subject(): string {
		return __( 'A new quote request on {site_title} is received!', 'commercebird' );
	}

	/**
	 * Get default email heading
	 *
	 * @return string
	 */
	public function get_default_heading(): string {
		return __( 'New quotation request.', 'commercebird' );
	}

	/**
	 * Trigger the sending of this email
	 *
	 * @param int      $order_id Order ID.
	 * @param WC_Order $order    Order object.
	 */
	public function trigger( int $order_id, $order = false ): void {
		$this->setup_locale();

		if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
			$order = wc_get_order( $order_id );
		}

		if ( is_a( $order, 'WC_Order' ) ) {
			$this->object                         = $order;
			$this->placeholders['{order_date}']   = wc_format_datetime( $this->object->get_date_created() );
			$this->placeholders['{order_number}'] = $this->object->get_order_number();
		}

		if ( $this->is_enabled() && $this->get_recipient() ) {
			$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
		}

		$this->restore_locale();
	}

	/**
	 * Get HTML email content
	 *
	 * @return string
	 */
	public function get_content_html(): string {
		return wc_get_template_html(
			$this->template_html,
			array(
				'order'              => $this->object,
				'email_heading'      => $this->get_heading(),
				'additional_content' => $this->get_additional_content(),
				'blogname'           => $this->get_blogname(),
				'sent_to_admin'      => true,
				'plain_text'         => false,
				'email'              => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Default additional content
	 *
	 * @return string
	 */
	public function get_default_additional_content(): string {
		return '';
	}

	/**
	 * Initialize settings form fields
	 */
	public function init_form_fields(): void {
		/* translators: %s: list of placeholders */
		$placeholder_text  = sprintf( __( 'Available placeholders: %s', 'commercebird' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' );
		$this->form_fields = array(
			'enabled'            => array(
				'title'   => __( 'Enable/Disable', 'commercebird' ),
				'type'    => 'checkbox',
				'label'   => __( 'Enable this email notification', 'commercebird' ),
				'default' => 'yes',
			),
			'recipient'          => array(
				'title'       => __( 'Recipient(s)', 'commercebird' ),
				'type'        => 'text',
				/* translators: %s: WP admin email */
				'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to %s.', 'commercebird' ), '<code>' . esc_attr( get_option( 'admin_email' ) ) . '</code>' ),
				'placeholder' => '',
				'default'     => '',
				'desc_tip'    => true,
			),
			'subject'            => array(
				'title'       => __( 'Subject', 'commercebird' ),
				'type'        => 'text',
				'desc_tip'    => true,
				'description' => $placeholder_text,
				'placeholder' => $this->get_default_subject(),
				'default'     => '',
			),
			'heading'            => array(
				'title'       => __( 'Email heading', 'commercebird' ),
				'type'        => 'text',
				'desc_tip'    => true,
				'description' => $placeholder_text,
				'placeholder' => $this->get_default_heading(),
				'default'     => '',
			),
			'additional_content' => array(
				'title'       => __( 'Additional content', 'commercebird' ),
				'description' => __( 'Text to appear below the main email content.', 'commercebird' ) . ' ' . $placeholder_text,
				'css'         => 'width:400px; height: 75px;',
				'placeholder' => __( 'N/A', 'commercebird' ),
				'type'        => 'textarea',
				'default'     => $this->get_default_additional_content(),
				'desc_tip'    => true,
			),
			'email_type'         => array(
				'title'       => __( 'Email type', 'commercebird' ),
				'type'        => 'select',
				'description' => __( 'Choose which format of email to send.', 'commercebird' ),
				'default'     => 'html',
				'class'       => 'email_type wc-enhanced-select',
				'options'     => $this->get_email_type_options(),
				'desc_tip'    => true,
			),
		);
	}
}
