<?php

namespace PWH_DCFH\App\Admin\Pages;

use PWH_DCFH\App\Frontend\Controllers\PWH_DCFH_Email_Handler;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Send_Email')) {
    class PWH_DCFH_Send_Email
    {
        private $_post_type;

        private $_post_id;

        private $email_templates;

        /**
         * Initializer Of The Class
         * Add/Remove Necessary Actions/Filters
         */
        public function init()
        {
            $_page = pwh_dcfh_helpers()::current_page();
            $this->_post_id = isset($_GET['post_id']) ? sanitize_key($_GET['post_id']) : null; // phpcs:ignore
            $this->_post_type = 'pwh_dcfh';
            add_action('admin_menu', [$this, 'menu']);
            if ('send_email' === $_page && !empty($this->_post_id)) {
                $this->email_templates = pwh_dcfh_email_tpl_handler()::get_templates();
                if (null === get_post($this->_post_id)) {
                    wp_safe_redirect(add_query_arg(['post_type' => $this->_post_type], admin_url('edit.php')));
                    exit();
                }
                add_action('view_send_email', [$this, 'display_form'], 10, 1);
                add_action('admin_init', [$this, 'admin_hooks']);
                add_filter('pwh_dcfh_f_admin_localizations', [$this, 'load_email_templates']);
            }
        }

        /**
         * Register Admin Hooks
         */
        public function admin_hooks()
        {
            ob_start();
            add_action('wp_mail_failed', [$this, 'wp_mailed_failed_error']);
        }

        /**
         * Filter Admin Localizations
         *
         * @param $localizations
         *
         * @return array
         */
        public function load_email_templates($localizations)
        {
            $localizations['emailTemplates'] = wp_json_encode($this->email_templates);

            return $localizations;
        }

        /**
         * Register Wp Admin Menu
         */
        public function menu()
        {
           add_submenu_page('edit.php?post_type=pwh_dcfh', __('Send Email', 'pwh-dcfh'), null, 'perform_entry_actions_pwh_dcfh', 'send_email', [$this, 'process_form']);
        }

        /**
         * Process Form Actions
         */
        public function process_form()
        {
            $post_request = [
                'email_from' => get_bloginfo('admin_email'),
                'email_from_name' => htmlspecialchars_decode(get_bloginfo('name')),
                'email_to' => '',
                'email_subject' => '',
                'email_body' => '',
            ];
            if (isset($_POST['btn_send_email'], $_POST['_wpnonce'])) {
                if (!wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), 'action_send_email')) {
                    wp_die(esc_html__('Unable to submit this form, please refresh and try again.', 'pwh-dcfh'));
                }
                $post_request = $_POST;
                $email_from = isset($_POST['email_from']) ? sanitize_text_field($_POST['email_from']) : null;
                $email_from_name = isset($_POST['email_from_name']) ? sanitize_text_field($_POST['email_from_name']) : null;
                $email_to = isset($_POST['email_to']) ? sanitize_text_field($_POST['email_to']) : null;
                $email_subject = isset($_POST['email_subject']) ? sanitize_text_field(htmlspecialchars_decode(stripslashes(sanitize_text_field($_POST['email_subject'])), ENT_QUOTES)) : null; // phpcs:ignore
                $email_body = isset($_POST['email_body']) ? wpautop(htmlspecialchars_decode(stripslashes($_POST['email_body']), ENT_QUOTES)) : null;                                           // phpcs:ignore
                $validate_request = true;
                if (empty($email_from)) {
                    pwh_dcfh_admin_notice()::add_error_message(__('Email from is required.', 'pwh-dcfh'));
                    $validate_request = false;
                }
                if (empty($email_from_name)) {
                    pwh_dcfh_admin_notice()::add_error_message(__('Email from name is required.', 'pwh-dcfh'));
                    $validate_request = false;
                }
                if (empty($email_subject)) {
                    pwh_dcfh_admin_notice()::add_error_message(__('Email subject is required.', 'pwh-dcfh'));
                    $validate_request = false;
                }
                if (empty($email_body)) {
                    pwh_dcfh_admin_notice()::add_error_message(__('Email body is required.', 'pwh-dcfh'));
                    $validate_request = false;
                }
                if ($validate_request) {
                    $post_obj = get_post($this->_post_id);
                    if (is_serialized($post_obj->post_excerpt)) {
                        $form_entries = pwh_dcfh_helpers()::maybe_unserialize($post_obj->post_excerpt);
                        $email_keywords = pwh_dcfh_db()::get_send_email_static_keywords_values($post_obj);
                        if (!empty($form_entries) && is_array($form_entries)) {
                            foreach ($form_entries as $form_entry) {
                                $field_id = pwh_dcfh_helpers()::sanitize_key($form_entry['id']);
                                $email_keywords['%%'.$field_id.'%%'] = $form_entry['value'];
                            }
                        }
                        // Email Body
                        if (!empty($email_keywords)) {
                            $email_body = str_replace(array_keys($email_keywords), array_values($email_keywords), $email_body);
                        }
                        $email_handler = PWH_DCFH_Email_Handler::instance();
                        add_filter('wp_mail_content_type', function () {
                            return 'text/html';
                        });
                        $email_handler->set_mail_from($email_from);
                        $email_handler->set_mail_from_name($email_from_name);
                        // Send Email
                        if (wp_mail($email_to, $email_subject, $email_body)) {
                            // Update Replies
                            $last_log_count = pwh_dcfh_logger()->get_log_count('sent_email_log', $this->_post_id);
                            pwh_dcfh_logger()->add_log('email-sent-log-'.$last_log_count, maybe_serialize([
                                'email_from' => $email_from,
                                'email_from_name' => $email_from_name,
                                'email_to' => $email_to,
                                'email_body' => $email_body,
                            ]), 'sent_email_log', $this->_post_id);
                            // Redirect
                            wp_safe_redirect(add_query_arg([
                                'post' => $this->_post_id,
                                'action' => 'edit',
                                'success' => true,
                                '_wpnonce' => wp_create_nonce('entry_action'),
                                'email_sent_to' => $email_to,
                            ], admin_url('post.php')));
                        } else {
                            wp_safe_redirect(add_query_arg([
                                'post' => $this->_post_id,
                                'action' => 'edit',
                                'success' => false,
                                '_wpnonce' => wp_create_nonce('entry_action'),
                                'email_sent_to' => $email_to,
                            ], admin_url('post.php')));
                        }
                        exit();
                    }
                }
            }
            do_action('view_send_email', $post_request);
        }

        /**
         * Display Form HTML Content
         *
         * @param $post_request
         */
        public function display_form($post_request)
        { ?>
            <div class="wrap pwh-dcfh-page" id="page-send-email">
                <h1 class="wp-heading-inline"><?php esc_html_e('Send Email', 'pwh-dcfh'); ?></h1>
                <a href="<?php echo esc_url(add_query_arg(['post_type' => $this->_post_type], admin_url('edit.php'))); ?>" class="page-title-action"><?php esc_html_e('Entries',
                        'pwh-dcfh'); ?></a>
                <a href="<?php echo esc_url(add_query_arg(['post_type' => $this->_post_type, 'page' => 'create_email_template'],
                    admin_url('edit.php'))); ?>" class="page-title-action"><?php esc_html_e('Add New Template', 'pwh-dcfh'); ?></a>
                <hr class="wp-header-end">
                <?php pwh_dcfh_admin_notice()::show_admin_notice(); ?>
                <span class="spinner"></span>
                <div id="poststuff">
                    <div id="post-body" class="metabox-holder columns-2">
                        <div class="postbox-container">
                            <div class="postbox">
                                <div class="inside">
                                    <form method="POST" autocomplete="off" enctype="application/x-www-form-urlencoded">
                                        <table class="form-table">
                                            <tbody>
                                            <?php if (!empty($this->email_templates)) { ?>
                                                <tr>
                                                    <th><?php esc_html_e('Choose Template:', 'pwh-dcfh'); ?></th>
                                                    <td>
                                                        <select title="" name="email_template" id="email_template" class="form-control jsEmailTemplate">
                                                            <option value=""><?php esc_html_e('Select Template', 'pwh-dcfh'); ?></option>
                                                            <?php foreach ($this->email_templates as $key => $value) { ?>
                                                                <option value="<?php esc_html_e($key, 'pwh-dcfh'); ?>"><?php esc_html_e($value['tpl_name'], 'pwh-dcfh'); ?></option>
                                                            <?php } ?>
                                                        </select>
                                                        <p class="helper"><?php esc_html_e('Choose predefined template from list.', 'pwh-dcfh'); ?></p>
                                                    </td>
                                                </tr>
                                            <?php } ?>
                                            <tr>
                                                <th><?php esc_html_e('Email From:', 'pwh-dcfh'); ?>
                                                    <span class="required">*</span></th>
                                                <td>
                                                    <input title="" type="email" name="email_from" id="email_from" class="form-control" value="<?php esc_html_e($post_request['email_from'], 'pwh-dcfh'); ?>" placeholder="<?php
                                                    esc_html_e('Email From', 'pwh-dcfh'); ?>">
                                                    <p class="helper"><?php esc_html_e('Enter email address for from.', 'pwh-dcfh'); ?></p>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('Email From Name:', 'pwh-dcfh'); ?>
                                                    <span class="required">*</span></th>
                                                <td>
                                                    <input title="" type="text" name="email_from_name" id="email_from_name" class="form-control" value="<?php esc_html_e($post_request['email_from_name'], 'pwh-dcfh'); ?>" placeholder="<?php
                                                    esc_html_e('Email From Name', 'pwh-dcfh'); ?>">
                                                    <p class="helper"><?php esc_html_e('Enter email from name.', 'pwh-dcfh'); ?></p>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('Email Subject:', 'pwh-dcfh'); ?>
                                                    <span class="required">*</span></th>
                                                <td>
                                                    <input title="" type="text" name="email_subject" id="email_subject" class="form-control" value="<?php esc_html_e($post_request['email_subject'], 'pwh-dcfh');
                                                    ?>" placeholder="<?php
                                                    esc_html_e('Subject', 'pwh-dcfh'); ?>">
                                                    <p class="helper"><?php esc_html_e('Enter subject for email.', 'pwh-dcfh'); ?></p>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('Reply To:', 'pwh-dcfh'); ?>
                                                    <span class="required">*</span></th>
                                                <td colspan="4">
                                                    <select title="" name="email_to" id="email_to" class="form-control">
                                                        <?php foreach (pwh_dcfh_user_handler()::get_user_emails($this->_post_id) as $key => $email) { ?>
                                                            <option value="<?php esc_html_e($key, 'pwh-dcfh'); ?>" <?php selected($key, $post_request['email_to']); ?>><?php esc_html_e($email, 'pwh-dcfh'); ?></option>
                                                        <?php } ?>
                                                    </select>
                                                    <p class="helper"><?php esc_html_e('Choose user email address to reply back.', 'pwh-dcfh'); ?></p>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('Email Data Tags:', 'pwh-dcfh'); ?></th>
                                                <td>
                                                    <div id="merge-data-tags-list">
                                                        <?php pwh_dcfh_db()::get_contact_form_send_email_data($this->_post_id); ?>
                                                    </div>
                                                    <?php
                                                    $create_template_url = esc_url(add_query_arg(['post_id' => $this->_post_id, 'page' => 'create_email_template'], admin_url('edit.php?post_type=pwh_dcfh')));
                                                    echo sprintf('<p class="helper">%1$s <a href="%2$s">%3$s</a></p>', esc_html(' Or use these keywords to create email template.'),
                                                        esc_url($create_template_url), esc_html('Click Here'))
                                                    ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th><?php esc_html_e('Email Message:', 'pwh-dcfh'); ?>
                                                    <span class="required">*</span></th>
                                                <td>
                                                    <?php wp_editor($post_request['email_body'], 'email_body', [
                                                        'wpautop' => true,
                                                        'media_buttons' => false,
                                                        'textarea_name' => 'email_body',
                                                        'textarea_rows' => 6
                                                    ]);
                                                    ?>
                                                    <p class="helper"><?php esc_html_e('Enter email body. Please use correct keywords.', 'pwh-dcfh'); ?></p>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                        <?php
                                        wp_nonce_field('action_send_email', '_wpnonce');
                                        submit_button(__('Send Email', 'pwh-dcfh'), 'primary large', 'btn_send_email');
                                        ?>
                                    </form>
                                </div>
                            </div>
                        </div>
                        <div id="postbox-container-1" class="postbox-container">
                            <?php
                            require_once dirname(__DIR__)."/template/documentation-widget.php";
                            require_once dirname(__DIR__)."/template/support-widget.php";
                            ?>
                        </div>
                    </div>
                    <br class="clear">
                </div>
            </div>
            <?php
        }

        /**
         * Get WP Email Faield Errors
         *
         * @param $wp_error
         *
         * @return void
         */
        public function wp_mailed_failed_error($wp_error)
        {
            $wp_mail_failed = $wp_error->errors['wp_mail_failed'] ?? null;
            if (!empty($wp_mail_failed)) {
                $error_messages = implode(', ', $wp_mail_failed);
                delete_transient('pwh_dcfh_email_failed');
                set_transient('pwh_dcfh_email_failed', $error_messages, MINUTE_IN_SECONDS);
            }
        }

    }
}