<?php
namespace Bright_Deposits_For_WooCommerce\Emails\Notifications;

/**
 * @package Bright_Deposits_For_WooCommerce\Emails\Notifications
 * @since 1.0.0
 * @author Bright Plugins
 * Description: This class is responsible for sending the customer payment deposit reminder email.
 */
class PaymentDepositReminder extends \WC_Email {
        
    public function __construct() {
        $this->id             = 'customer_payment_deposit_reminder';
        $this->customer_email = true;
        $this->title          = __( 'BDFWBP - Upcoming Payment Deposit Reminder', 'bright-deposits-for-woocommerce' );
        $this->description    = __( 'Emails are sent to customers who have upcoming due payments.', 'bright-deposits-for-woocommerce' );
        $this->template_html  = 'emails/customer-payment-deposit-reminder.php';
        $this->template_plain = 'emails/plain/customer-payment-deposit-reminder.php';
        $this->template_base = BDFWBP_TEMPLATE_PATH;
        $this->placeholders   = array(
            '{order_date}' => '',
            '{order_number}' => '',
            '{days_remaining}' => '',
            '{customer_name}' => ''
        );
        parent::__construct();
    }

    /**
     * Trigger the email for the customer to notify about an upcoming payment.
     * @since 1.0.0
     * @param int $order_id
     * @param \WC_Order $order
     * @return void
     */
    public function trigger( $order_id, $order = false ) {
        $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->recipient = $this->object->get_billing_email();
            $this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
            $this->placeholders['{order_number}'] = $this->object->get_order_number();
            $this->placeholders['{days_remaining}'] = get_option( 'bdfwbp_deposits_settings[days_before_due]', 3 );
            $this->placeholders['{customer_name}'] = $this->object->get_billing_first_name() . ' ' . $this->object->get_billing_last_name();
        }
        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 the additional content for the email.
     * @since 1.0.0
     * @return string
     */
    public function get_additional_content() {
        $content = $this->get_option( 'additional_content', $this->get_default_additional_content() );
        $content = $this->format_string( $content );
        return apply_filters( 'woocommerce_email_additional_content_' . $this->id, $content, $this );
    }

    /**
     * Get the default subject for the email.
     * @since 1.0.0
     * @return string
     */
    public function get_default_subject() { return __( '{site_title} : Pay Deposit Reminder for order #{order_number}', 'bright-deposits-for-woocommerce' ); }

    /**
     * Get the default heading for the email.
     * @since 1.0.0
     * @return string
     */
    public function get_default_heading() { return __( 'Pay Deposit Reminder for order #{order_number}', 'bright-deposits-for-woocommerce' ); }

    /**
     * Get the default additional content for the email.
     * @since 1.0.0
     * @return string
     */
    public function get_default_additional_content() { return __( 'Hi {customer_name}! This is a reminder of order #{order_number} placed on {order_date}. You have {days_remaining} days left to pay the deposit.', 'bright-deposits-for-woocommerce' ); }

    /**
     * Get the content html for the email.
     * @since 1.0.0
     * @return string
     */
    public function get_content_html() {
        return wc_get_template_html( $this->template_html, array(
            'order' => $this->object, 
            'email_heading' => $this->get_heading(), 
            'additional_content' => $this->get_additional_content(),
            'sent_to_admin' => false, 
            'plain_text' => false, 
            'email' => $this
        ), '', $this->template_base );
    }

    /**
     * Get the content plain for the email.
     * @since 1.0.0
     * @return string
     */
    public function get_content_plain() {
        return wc_get_template_html( $this->template_plain, array(
            'order' => $this->object, 
            'email_heading' => $this->get_heading(), 
            'additional_content' => $this->get_additional_content(),
            'sent_to_admin' => false, 
            'plain_text' => true, 
            'email' => $this
        ), '', $this->template_base );
    }

    /**
     * Init the form fields for the email.
     * @since 1.0.0
     * @return void
     */
    public function init_form_fields() {
        $this->form_fields = array(
            'enabled' => array(
                'title' => __('Enable/Disable','bright-deposits-for-woocommerce'),
                'type' => 'checkbox',
                'label' => __('Enable this email notification','bright-deposits-for-woocommerce'),
                'default' => 'yes'
            ),
            'subject' => array(
                'title' => __('Subject','bright-deposits-for-woocommerce'),
                'type' => 'text',
                'desc_tip' => true,
                // translators: %s: Comma-separated list of available placeholders.
                'description' => sprintf(__('Available placeholders: %s','bright-deposits-for-woocommerce'),'<code>' . implode('</code>, <code>', array_keys($this->placeholders)) . '</code>'),
                'placeholder' => $this->get_subject(),
                'default' => $this->get_subject()
            ),
            'heading' => array(
                'title' => __('Email heading','bright-deposits-for-woocommerce'),
                'type' => 'text',
                'desc_tip' => true,
                // translators: %s: Comma-separated list of available placeholders.
                'description' => sprintf(__('Available placeholders: %s','bright-deposits-for-woocommerce'),'<code>' . implode('</code>, <code>', array_keys($this->placeholders)) . '</code>'),
                'placeholder' => $this->get_heading(),
                'default' => $this->get_heading()
            ),
            'additional_content' => array(
                'title' => __('Additional content','bright-deposits-for-woocommerce'),
                // translators: %s: Comma-separated list of available placeholders.
                'description' => __('Text to appear below the main email content.','bright-deposits-for-woocommerce') . ' ' . sprintf(__('Available placeholders: %s','bright-deposits-for-woocommerce'),'<code>' . implode('</code>, <code>', array_keys($this->placeholders)) . '</code>'),
                'css' => 'width:400px; height: 75px;',
                'placeholder' => $this->get_default_additional_content(),
                'type' => 'textarea',
                'default' => $this->get_default_additional_content(),
                'desc_tip' => true
            ),
            'email_type' => array(
                'title' => __('Email type','bright-deposits-for-woocommerce'),
                'type' => 'select',
                'description' => __('Choose which format of email to send.','bright-deposits-for-woocommerce'),
                'default' => 'html',
                'class' => 'email_type wc-enhanced-select',
                'options' => $this->get_email_type_options(),
                'desc_tip' => true
            )
        );
    }
}