<?php

namespace GriffinForms\Admin;

class UninstallFeedback extends \GriffinForms\Admin\Format
{
    private const SCRIPT_HANDLE = 'griffinforms-uninstall-feedback';

    public function __construct()
    {
        $this->setAll();
        add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']);
        add_action('admin_footer-plugins.php', [$this, 'renderModal']);
        add_action('admin_footer-plugins-network.php', [$this, 'renderModal']);
    }

    private function getReasons(): array
    {
        return [
            'missing_feature' => __('Missing a feature I need', 'griffinforms-form-builder'),
            'found_bug'       => __('I encountered a bug or error', 'griffinforms-form-builder'),
            'too_complex'     => __('Too hard to use', 'griffinforms-form-builder'),
            'performance'     => __('Performance or speed issues', 'griffinforms-form-builder'),
            'switched_plugin' => __('I switched to another plugin', 'griffinforms-form-builder'),
            'temporary'       => __('Just temporary / debugging', 'griffinforms-form-builder'),
            'pricing'         => __('Pricing or licensing', 'griffinforms-form-builder'),
            'other'           => __('Other (please specify)', 'griffinforms-form-builder'),
        ];
    }

    public function enqueueAssets($hook): void
    {
        if (!in_array($hook, ['plugins.php', 'plugins-network.php'], true)) {
            return;
        }

        wp_enqueue_style('wp-jquery-ui-dialog');
        wp_enqueue_script('jquery-ui-dialog');

        $script_url = $this->config->getRootUrl() . 'admin/js/griffinforms-uninstall-feedback.js';
        wp_enqueue_script(
            self::SCRIPT_HANDLE,
            $script_url,
            ['jquery', 'jquery-ui-dialog'],
            $this->config::JS_VER,
            true
        );

        $plugin_basename = plugin_basename($this->config->getRootPath() . 'griffinforms.php');

        wp_localize_script(self::SCRIPT_HANDLE, 'gfUninstallFeedback', [
            'ajax_url'       => admin_url('admin-ajax.php'),
            'nonce'          => wp_create_nonce('griffinforms_uninstall_feedback'),
            'plugin_basename'=> $plugin_basename,
            'plugin_slug'    => 'griffinforms-form-builder',
            'reasons'        => $this->getReasons(),
            'modal_html'     => $this->getModalHtml(),
            'i18n'           => [
                'title'          => __('Quick feedback', 'griffinforms-form-builder'),
                'intro'          => __('We’re sorry to see you go. Could you tell us why you’re deactivating GriffinForms?', 'griffinforms-form-builder'),
                'reason_label'   => __('Reason', 'griffinforms-form-builder'),
                'details_label'  => __('Details (optional)', 'griffinforms-form-builder'),
                'contact_label'  => __('It’s OK to contact me about this.', 'griffinforms-form-builder'),
                'deactivate'     => __('Deactivate', 'griffinforms-form-builder'),
                'skip_deactivate'=> __('Skip & Deactivate', 'griffinforms-form-builder'),
                'cancel'         => __('Cancel', 'griffinforms-form-builder'),
                'required'       => __('Please select a reason before deactivating.', 'griffinforms-form-builder'),
                'sending'        => __('Sending feedback…', 'griffinforms-form-builder'),
            ],
        ]);
    }

    public function renderModal(): void
    {
        echo $this->getModalHtml();
    }

    private function getModalHtml(): string
    {
        $reasons = $this->getReasons();
        ob_start();
        ?>
        <div id="griffinforms-uninstall-feedback-dialog" title="<?php echo esc_attr(__('Quick feedback', 'griffinforms-form-builder')); ?>" style="display:none;">
            <p><?php echo esc_html(__('We’re sorry to see you go. Could you tell us why you’re deactivating GriffinForms?', 'griffinforms-form-builder')); ?></p>
            <div class="notice notice-error inline" id="griffinforms-uninstall-feedback-error" style="display:none;">
                <p><?php echo esc_html__('Please select a reason before deactivating.', 'griffinforms-form-builder'); ?></p>
            </div>
            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">
                        <label for="griffinforms-uninstall-reason"><?php echo esc_html__('Reason', 'griffinforms-form-builder'); ?></label>
                    </th>
                    <td>
                        <select id="griffinforms-uninstall-reason" class="widefat">
                            <option value=""><?php echo esc_html__('Select a reason', 'griffinforms-form-builder'); ?></option>
                            <?php foreach ($reasons as $key => $label) : ?>
                                <option value="<?php echo esc_attr($key); ?>"><?php echo esc_html($label); ?></option>
                            <?php endforeach; ?>
                        </select>
                        <p class="description" style="margin-top:6px;">
                            <?php
                            echo wp_kses(
                                sprintf(
                                    __('Need help? Visit the <a href="%s" target="_blank" rel="noopener">support forum</a>.', 'griffinforms-form-builder'),
                                    esc_url('https://wordpress.org/support/plugin/griffinforms-form-builder/')
                                ),
                                ['a' => ['href' => [], 'target' => [], 'rel' => []]]
                            );
                            ?>
                        </p>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <label for="griffinforms-uninstall-details"><?php echo esc_html__('Details (optional)', 'griffinforms-form-builder'); ?></label>
                    </th>
                    <td>
                        <textarea id="griffinforms-uninstall-details" class="large-text" rows="3"></textarea>
                    </td>
                </tr>
                <tr>
                    <th scope="row"><?php echo esc_html__('Contact', 'griffinforms-form-builder'); ?></th>
                    <td>
                        <label for="griffinforms-uninstall-contact">
                            <input type="checkbox" id="griffinforms-uninstall-contact" value="1" />
                            <?php echo esc_html__('It’s OK to contact me about this.', 'griffinforms-form-builder'); ?>
                        </label>
                        <?php
                        $current_user = wp_get_current_user();
                        $user_email = ($current_user && !empty($current_user->user_email)) ? $current_user->user_email : '';
                        $contact_line = $user_email
                            ? sprintf(__('We will contact you at %s.', 'griffinforms-form-builder'), $user_email)
                            : __('We will contact you at the email on your account.', 'griffinforms-form-builder');
                        ?>
                        <p class="description" id="griffinforms-uninstall-contact-email" style="display:none;margin-top:6px;font-size:12px;">
                            <?php echo esc_html($contact_line); ?>
                        </p>
                    </td>
                </tr>
            </table>
        </div>
        <?php
        return trim(ob_get_clean());
    }
}
