<?php

namespace WPIS;

class FormEvaluationShortcode
{

    private static $shortcode = 'wpis-form-evaluation';

    static public function init()
    {

        $class = new self();

        add_shortcode(self::$shortcode, [$class, 'generateShortcode']);
    }

    public function generateShortcode($atts = false, $content = null)
    {

        $literals = [
            'form.gender.label'        => __('Gender', 'wpis'),
            'form.gender.value.male'   => __('Mister', 'wpis'),
            'form.gender.value.female' => __('Madam', 'wpis'),
            'form.gender.value.couple' => __('Mister and Madam', 'wpis'),
            'form.gender.value.other'  => __('Other', 'wpis'),
            'form.firstname.label'     => __('Firstname', 'wpis'),
            'form.lastname.label'      => __('Lastname', 'wpis'),
            'form.message.label'       => __('Message', 'wpis'),
            'form.phone.label'         => __('Phone', 'wpis'),
            'form.email.label'         => __('Email', 'wpis'),
            'form.addressStreet.label' => __('Street', 'wpis'),
            'form.addressNumber.label' => __('Number', 'wpis'),
            'form.addressBox.label'    => __('Box', 'wpis'),
            'form.addressZip.label'    => __('Zip', 'wpis'),
            'form.addressCity.label'   => __('City', 'wpis'),
            'form.policy.label'        => __('Privacy policy', 'wpis'),
            'form.policy.value'        => __('You affirm that you have read our confidentiality policy. You can unsubscribe at any time using the unsubscribe links.', 'wpis'),
            'form.submit.value'        => __('Send', 'wpis'),
            'message.success'          => __('Your message has been sent successfully !', 'wpis'),
            'message.processing'       => __('Sending ...', 'wpis'),
            'error.single'             => __('An error has occurred:', 'wpis'),
            'error.multiple'           => __('Several errors occurred:', 'wpis')
        ];

        ClientAssetsManager::enqueueFormsScript();

        $allowedStyleOption = ['on', 'off'];
        $allowedFormTypes = ['default'];
        $submitText = 'Envoyer';

        if (isset($atts['type']) && !in_array($atts['type'], $allowedFormTypes)) {
            return $this->generateError(__('Error, only the following form type are allowed: ', 'wpis') . implode(', ', $allowedFormTypes));
        };

        if (isset($atts['style']) && !in_array($atts['style'], $allowedStyleOption)) {
            return $this->generateError(__('Error, only the following style option are allowed: ', 'wpis') . implode(', ', $allowedStyleOption));
        };

        if (isset($atts['submit']) && $atts['submit'] !== '') {
            $literals['form.submit.value'] = $atts['submit'];
        };

        $formType = $atts['type'] ?? 'default';
        $pluginPath = __DIR__ . '/../../templates/form/evaluation.' . $formType . '.form.html';
        $form = file_get_contents(ThemeTemplateResolver::resolve('immosync/form/evaluation.html', $pluginPath, 'wpis_form_template_path'));

        if (isset($atts['style']) && $atts['style'] === 'off') {
            $form = $this->removeDefaultStyle($form);
        }

        wp_localize_script('wpis_forms', 'literals', $literals);

        foreach ($literals as $key => $value) {
            $form = preg_replace('/{{\s*' . preg_quote($key, '/') . '\s*}}/', $value, $form);
        }
        return $form;
    }

    public function generateError($message)
    {
        return sprintf('[ %s ]', self::$shortcode) . ' : ' .  $message;
    }

    private function removeDefaultStyle($html)
    {
        $tagName = 'style';
        $html = preg_replace('/<' . $tagName . '[^>]*>.*?<\/' . $tagName . '>/is', '', $html);
        return $html;
    }
}
