<?php
/**
 * YeePDF Forminator Integration main class.
 */
if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

final class Forminator_Yeepdf extends Forminator_Integration
{
    /**
     * @var self|null
     */
    protected static $instance = null;

    /**
     * Slug. Also determines the settings classes names:
     * Forminator_Yeepdf_Form_Settings, and the post meta key
     * forminator_addon_yeepdf_form_settings.
     *
     * @var string
     */
    protected $_slug = 'yeepdf';

    /**
     * @var string
     */
    protected $_version = YEEPDF_FORMINATOR_INTEGRATION_VERSION;

    /**
     * @var string
     */
    protected $_min_forminator_version = '1.16';

    /**
     * @var string
     */
    protected $_short_title = 'PDF Templates';

    /**
     * @var string
     */
    protected $_title = 'YeePDF Templates';

    /**
     * @var string
     */
    protected $_documentation = 'https://pdf.add-ons.org/document/';

    /**
     * Forminator_Yeepdf constructor.
     */
    public function __construct()
    {
        // late init to allow translation.
        $this->_description = esc_html__('Generate a PDF from your drag and drop PDF template and attach it to the email notifications of this form.', 'pdf-for-wpforms');
    }

    /**
     * No external API involved, always authorized.
     *
     * @return bool
     */
    public function is_authorized()
    {
        return true;
    }

    /**
     * Global settings wizard: a single "Activate" step.
     *
     * @return array
     */
    public function settings_wizards()
    {
        return array(
            array(
                'callback' => array($this, 'setup_connect'),
                'is_completed' => array($this, 'is_connected'),
            ),
        );
    }

    /**
     * Activate the integration.
     *
     * @param array $submitted_data Submitted data.
     * @param int   $form_id Form Id.
     *
     * @return array
     */
    public function setup_connect($submitted_data, $form_id = 0)
    {
        $error_message = '';
        $has_errors = false;
        $show_success = false;
        $buttons = array();
        $is_submit = !empty($submitted_data);

        if ($is_submit) {
            try {
                $connect = isset($submitted_data['connect']) ? $submitted_data['connect'] : '';
                if (empty($connect)) {
                    throw new Forminator_Integration_Exception(esc_html__('Please activate PDF Templates', 'pdf-for-wpforms'));
                }

                if (!forminator_addon_is_active($this->_slug)) {
                    $activated = Forminator_Integration_Loader::get_instance()->activate_addon($this->_slug);
                    if (!$activated) {
                        throw new Forminator_Integration_Exception(Forminator_Integration_Loader::get_instance()->get_last_error_message());
                    }
                }
                // no form_id its on global settings.
                if (empty($form_id)) {
                    $show_success = true;
                }
            } catch (Forminator_Integration_Exception $e) {
                $error_message = $e->getMessage();
                $has_errors = true;
            }
        }

        if ($show_success) {
            $html = $this->success_authorize();
        } else {
            if ($this->is_connected()) {
                $buttons['disconnect'] = array(
                    'markup' => self::get_button_markup(esc_html__('Disconnect', 'forminator'), 'sui-button-ghost forminator-addon-disconnect forminator-integration-popup__close'),
                );
            } else {
                $buttons['submit'] = array(
                    'markup' => self::get_button_markup(esc_html__('Activate', 'forminator'), 'forminator-addon-connect forminator-integration-popup__close'),
                );
            }

            $description = esc_html__('Activate PDF Templates to start attaching generated PDF files to your form email notifications.', 'pdf-for-wpforms');

            $html = '<div class="forminator-integration-popup__header">';
            $html .= '<h3 id="forminator-integration-popup__title" class="sui-box-title sui-lg" style="overflow: initial; white-space: normal; text-overflow: initial;">'
                . esc_html__('Activate PDF Templates', 'pdf-for-wpforms') . '</h3>';
            $html .= '</div>';
            if ($this->is_connected() && class_exists('Forminator_Admin')) {
                $html .= Forminator_Admin::get_green_notice(esc_html__('PDF Templates is already active.', 'pdf-for-wpforms'));
            } elseif (!empty($error_message) && class_exists('Forminator_Admin')) {
                $html .= Forminator_Admin::get_red_notice(esc_html($error_message));
            }
            $html .= '<p id="forminator-integration-popup__description" class="sui-description" style="margin: 0; text-align: center;">' . $description . '</p>';
            $html .= '<form><input type="hidden" value="1" name="connect"></form>';
        }

        return array(
            'html' => $html,
            'buttons' => $buttons,
            'redirect' => false,
            'has_errors' => $has_errors,
        );
    }

    /**
     * Point assets to this plugin instead of Forminator's addons dir.
     *
     * @return string
     */
    public function assets_path(): string
    {
        return yeepdf_forminator_integration_url() . 'assets/';
    }

    /**
     * @return string
     */
    public function get_icon()
    {
        return $this->assets_path() . 'icon.svg';
    }

    /**
     * @return string
     */
    public function get_icon_x2()
    {
        return $this->get_icon();
    }

    /**
     * @return string
     */
    public function get_image()
    {
        return $this->get_icon();
    }

    /**
     * @return string
     */
    public function get_image_x2()
    {
        return $this->get_icon();
    }

    /**
     * @return string
     */
    public function get_banner()
    {
        return $this->get_icon();
    }

    /**
     * @return string
     */
    public function get_banner_x2()
    {
        return $this->get_icon();
    }
}
