<?php

namespace Twnt4All\Engine\Adapter;

use Twnt4All\Engine\Support\MarkupBuilder;
use Twnt4All\Engine\Service\BaseMailer;

class DirectMailer extends BaseMailer
{
    /**
     * Sends an email when the start_order request fails
     */
    public static function send_start_order_failed_email( $order_id )
    {
        $subject = __( 'TWINT failed on the checkout', 'pay4all-for-twint' );
        /* translators: %1$s, %2$s dynamic value(s). */
        $message = sprintf( __( 'A transaction for TWINT failed on the checkout for the order %1$s on the website %2$s.', 'pay4all-for-twint' ), $order_id, get_bloginfo( 'title' ) );
        $message .= '<br><br>';
        $message .= static::get_common_messages();

        static::send_admin_email( $subject, $message );
    }


    /**
     * Sends an email when enrollment of the cashier fails.
     *
     * @param $enrollment_response
     */
    public static function send_enrollment_failed_email( $enrollment_response )
    {
        $subject = __( 'TWINT cashier register enrolment failed', 'pay4all-for-twint' );
        /* translators: %1$s dynamic value(s). */
        $message = sprintf( __( 'Cashier register could not be enrolled for website %1$s.', 'pay4all-for-twint' ), get_bloginfo( 'title' ) );
        $message .= '<br><br>';
        $message .= static::get_common_messages();
        $message .= __( 'Error message : ', 'pay4all-for-twint' ) . $enrollment_response[ 'message' ];

        static::send_admin_email( $subject, $message );
    }

    /**
     * Sends an email when the periodic system check fails.
     *
     * @param $response
     */
    public static function send_system_check_failed_email( $response )
    {
        $subject = __( 'TWINT system check failed', 'pay4all-for-twint' );
        /* translators: %1$s dynamic value(s). */
        $message = sprintf( __( 'System check for website %1$s failed.', 'pay4all-for-twint' ), get_bloginfo( 'title' ) );
        $message .= '<br><br>';
        $message .= static::get_common_messages();
        $message .= __( 'Response : ', 'pay4all-for-twint' );
        $message .= '<br>';
        $message .= json_encode( $response );

        static::send_admin_email( $subject, $message );
    }

    /**
     * Sends an email when requestCheckin fails.
     *
     * @param $response
     */
    public static function send_checkin_failed_email( $response )
    {
        $subject = __( 'TWINT failed on the checkout', 'pay4all-for-twint' );
        $message = __( 'Failed to connect to TWINT on the checkout.', 'pay4all-for-twint' );
        $message .= '<br><br>';
        $message .= static::get_common_messages();
        $message .= __( 'Response : ', 'pay4all-for-twint' );
        $message .= '<br>';
        $message .= json_encode( $response );

        static::send_admin_email( $subject, $message );
    }

    /**
     * Sends an email when renewal of the certificate fails.
     *
     * @param $response
     * @param $expiry
     */
    public static function send_certificate_renewal_failed_email( $response, $expiry )
    {
        $subject = __( 'TWINT certificate renewal failed', 'pay4all-for-twint' );
        /* translators: %1$s dynamic value(s). */
        $message = sprintf( __( 'The automatic renewal of the TWINT certificate failed for the website %1$s.', 'pay4all-for-twint' ), get_bloginfo( 'title' ) );
        $message .= __( 'Please try manually renewing the certificate from either the TWINT settings in the WordPress backend or from the TWINT portal. Please make sure that all other websites/shops using the same TWINT account are updated accordingly.', 'pay4all-for-twint' );
        $message .= '<br><br>';
        /* translators: %1$s dynamic value(s). */
        $message .= sprintf( __( 'The old certificates expires %1$s', 'pay4all-for-twint' ), $expiry );
        $message .= '<br><br>';
        $message .= __( 'Response : ', 'pay4all-for-twint' );
        $message .= '<br>';
        $message .= json_encode( $response );

        static::send_admin_email( $subject, $message );
    }

    /**
     * Sends an email when the certificate has expired.
     *
     * @return mixed
     */
    public static function send_certificate_expired_email()
    {
        $subject = __( 'TWINT certificate expired', 'pay4all-for-twint' );
        /* translators: %1$s dynamic value(s). */
        $message = sprintf( __( 'The TWINT certificate for the website %1$s expired. Please renew the certificate from the TWINT settings in the WordPress backend or from the TWINT portal.', 'pay4all-for-twint' ), get_bloginfo( 'title' ) );

        static::send_admin_email( $subject, $message );
    }

    /**
     * Sends a reminder email that the certificate expires soon.
     *
     * @param $days_before_expiring
     */
    public static function send_certificate_expires_soon_email( $days_before_expiring )
    {
        $subject = __( 'TWINT certificate will expire soon.', 'pay4all-for-twint' );
        /* translators: %1$s, %2$s dynamic value(s). */
        $message = sprintf( __( 'The TWINT certificate for the website %1$s will expire in %2$s days. Please renew the certificate from the TWINT settings in the WordPress backend or from the TWINT portal.', 'pay4all-for-twint' ), get_bloginfo( 'title' ), $days_before_expiring );

        static::send_admin_email( $subject, $message );
    }

    /**
     * Sends an email if the order confirmation fails.
     *
     * @return mixed
     */
    public static function send_order_confirmation_failed_email( $order_id )
    {
        $subject = __( 'TWINT payment was successful but not confirmed', 'pay4all-for-twint' );
        /* translators: %1$s dynamic value(s). */
        $message = sprintf( __( 'The TWINT payment for order %1$s was successful but not confirmed. Please confirm the payment in the order edit screen of the WP backend.', 'pay4all-for-twint' ), $order_id );
        static::send_admin_email( $subject, $message );
    }

    /**
     * Returns the admin email address.
     *
     * @return string
     */
    protected static function get_email_address()
    {
        return get_option( 't4a_twint_settings_email_on_enroll_failure' );
    }

    /**
     * @return string
     */
    private static function get_common_messages()
    {
        $message = __( 'Possible solutions:', 'pay4all-for-twint' );
        $message .= '<ul>';
        $message .= '<li>' . __( 'Please check if the payment gateway works on the WooCommerce checkout page. You can check the connection to TWINT in the WooCommerce TWINT plugin settings (WooCommerce > Settings > TWINT) by clicking the "Enroll" button.', 'pay4all-for-twint' ) . '</li>';
        $message .= '<li>' . __( 'If the enrollment fails, please start the setup assistant on the page „WooCommerce > Settings > TWINT“ and check if you can complete the setup. If this is not possible, please upload the p12 certificate again (you can download it from the TWINT portal on the page „Settings“). If you get a red error message after uploading the certificate, then most probably the password is not correct. The password might have been overwritten, e.g. by the browser or a browser extension.', 'pay4all-for-twint' ) . '</li>';
        $message .= '<li>' . __( 'If you forgot the certificate password you can directly contact TWINT to create a new certificate: support@twint.ch', 'pay4all-for-twint' ) . '</li>';
        $message .= '<li>' . __( 'Please also check the logs in the WP backend on the page <strong>WooCommerce > Settings > TWINT > Logs</strong>', 'pay4all-for-twint' ) . '</li>';
        $message .= '</ul>';

        return $message;
    }
}
