<?php

namespace PWH_DCFH\App\Frontend\DiviContactForm\Features;

use ET_Builder_Element;
use PWH_DCFH\App\Controllers\PWH_DCFH_DOM;
use DOMXPath;
use PWH_DCFH\App\Frontend\Request\PWH_DCFH_Post_Request;

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Success_Message')) {
    class PWH_DCFH_Success_Message
    {
        public $render_slug;

        public $output;

        public $props = [];

        /**
         * Class Constructor
         */
        public function __construct($render_slug, $output, $props)
        {
            $this->render_slug = $render_slug;
            $this->output = $output;
            $this->props = $props;
        }

        /**
         * Filter Message
         * @return string
         */
        public function output()
        {
            //$post_request = new PWH_DCFH_Post_Request();
            $this->output = preg_replace('/\bet-pb-contact-message et_pb_contact_message_top\b/', 'et-pb-contact-message et_pb_success_message', $this->output);
            $dom = PWH_DCFH_DOM::dom($this->output);
            $dom_xpath = new DOMXPath($dom);
            // $message = $dom_xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), 'et-pb-contact-message')]");
            $message = $dom_xpath->query('//div[contains(@class,"et-pb-contact-message")]');
            if (isset($message->length) && 0 !== $message->length) {
                $text_content = $message->item(0)->textContent;
                $text_content = pwh_dcfh_email_merge_tags_handler()::get_merge_tags_values($text_content);
                if (preg_match('/\bdcfh_all_fields\b/i', $text_content)) {
                    $text_content = str_replace("%%dcfh_all_fields%%", pwh_dcfh_email_merge_tags_handler()::get_all_fields_data(true), $text_content);
                }
                $message->item(0)->textContent = $text_content;
                $this->output = $dom->saveHTML();
                $this->output = html_entity_decode($this->output, ENT_QUOTES, 'utf-8');
            }

            return $this->output;
        }

        /**
         * Render Css
         * @return void
         */
        public function css()
        {
            $css_selector = '%%order_class%% .et-pb-contact-message.et_pb_success_message';
            $d_wrapper_background = pwh_dcfh_module_helpers()::get_global_color(et_pb_responsive_options()->get_property_values($this->props, 'success_message_background'));
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_background, $css_selector, 'background', $this->render_slug, '', 'color');
            $d_wrapper_margin = et_pb_responsive_options()->get_property_values($this->props, 'success_message_margin');
            $d_wrapper_margin = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_wrapper_margin);
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_margin, $css_selector, 'margin', $this->render_slug, '', 'custom_padding');
            $d_wrapper_padding = et_pb_responsive_options()->get_property_values($this->props, 'success_message_padding');
            $d_wrapper_padding = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_wrapper_padding);
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_padding, $css_selector, 'padding', $this->render_slug, '', 'custom_padding');
            $d_wrapper_border = et_pb_responsive_options()->get_property_values($this->props, 'success_message_border');
            $d_wrapper_border = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_wrapper_border);
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_border, $css_selector, 'border-radius', $this->render_slug, '', 'border-radius');
            $d_wrapper_border_color = et_pb_responsive_options()->get_property_values($this->props, 'success_message_border_color');
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_border_color, $css_selector, 'border-color', $this->render_slug, '', 'color');
            $d_wrapper_border_width = et_pb_responsive_options()->get_property_values($this->props, 'success_message_border_width');
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_border_width, $css_selector, 'border-width', $this->render_slug);
            $d_wrapper_border_style = et_pb_responsive_options()->get_property_values($this->props, 'success_message_border_style');
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_border_style, $css_selector, 'border-style', $this->render_slug, '', 'select');
            $shadow_preset = isset($this->props['success_message_shadow']) ? $this->props['success_message_shadow'] : null;
            if (!empty($shadow_preset) && 'none' !== $shadow_preset) {
                $shadow = pwh_dcfh_module_helpers()::get_shadow_presets_values($shadow_preset);
                $horizontal = isset($this->props['success_message_shadow_horizontal']) ? $this->props['success_message_shadow_horizontal'] : $shadow['horizontal'];
                $vertical = isset($this->props['success_message_shadow_vertical']) ? $this->props['success_message_shadow_vertical'] : $shadow['vertical'];
                $blur = isset($this->props['success_message_shadow_blur']) ? $this->props['success_message_shadow_blur'] : $shadow['blur'];
                $spread = isset($this->props['success_message_shadow_spread']) ? $this->props['success_message_shadow_spread'] : $shadow['spread'];
                $color = isset($this->props['success_message_shadow_color']) ? $this->props['success_message_shadow_color'] : 'rgba(0,0,0,0.3)';
                $position = isset($this->props['success_message_shadow_position']) ? $this->props['success_message_shadow_position'] : $shadow['position'];
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => $css_selector,
                    'declaration' => "-webkit-box-shadow: $position $horizontal $vertical $blur $spread $color;-moz-box-shadow: $position $horizontal $vertical $blur $spread $color; box-shadow: $position $horizontal $vertical $blur $spread $color;",
                ]);
            }
            /*
             * Heading
             */
            $desktop_font = isset($this->props['success_message_heading_font']) ? $this->props['success_message_heading_font'] : null;
            $tablet_font = isset($this->props['success_message_heading_font_tablet']) ? $this->props['success_message_heading_font_tablet'] : null;
            $phone_font = isset($this->props['success_message_heading_font_phone']) ? $this->props['success_message_heading_font_phone'] : null;
            if (!empty($desktop_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($desktop_font)),
                ]);
            }
            if (!empty($tablet_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($tablet_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                ]);
            }
            if (!empty($phone_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($phone_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
            $d_heading_color = et_pb_responsive_options()->get_property_values($this->props, 'success_message_heading_color');
            et_pb_responsive_options()->generate_responsive_css($d_heading_color, "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6", 'color', $this->render_slug, '', 'color');
            $d_heading_size = et_pb_responsive_options()->get_property_values($this->props, 'success_message_heading_size');
            et_pb_responsive_options()->generate_responsive_css($d_heading_size, "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6", 'font-size', $this->render_slug);
            $d_heading_letterspace = et_pb_responsive_options()->get_property_values($this->props, 'success_message_heading_letterspace');
            et_pb_responsive_options()->generate_responsive_css($d_heading_letterspace, "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6", 'letter-spacing', $this->render_slug);
            $d_heading_lineheight = et_pb_responsive_options()->get_property_values($this->props, 'success_message_heading_lineheight');
            et_pb_responsive_options()->generate_responsive_css($d_heading_lineheight, "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6", 'line-height', $this->render_slug);
            $d_heading_alignment = pwh_dcfh_module_helpers()::justified_to_justify(et_pb_responsive_options()->get_property_values($this->props, 'success_message_heading_alignment'));
            et_pb_responsive_options()->generate_responsive_css($d_heading_alignment, "$css_selector h1,$css_selector h2,$css_selector h3,$css_selector h4,$css_selector h5,$css_selector h6", 'text-align', $this->render_slug, '', 'text_alignment');
            /*
             *  Paragraph
             */
            $desktop_font = isset($this->props['success_message_font']) ? $this->props['success_message_font'] : null;
            $tablet_font = isset($this->props['success_message_font_tablet']) ? $this->props['success_message_font_tablet'] : null;
            $phone_font = isset($this->props['success_message_font_phone']) ? $this->props['success_message_font_phone'] : null;
            if (!empty($desktop_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector p",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($desktop_font)),
                ]);
            }
            if (!empty($tablet_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector p",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($tablet_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                ]);
            }
            if (!empty($phone_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector p",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($phone_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
            $d_paragraph_color = et_pb_responsive_options()->get_property_values($this->props, 'success_message_font_color');
            et_pb_responsive_options()->generate_responsive_css($d_paragraph_color, "$css_selector p", 'color', $this->render_slug, '', 'color');
            $d_paragraph_size = et_pb_responsive_options()->get_property_values($this->props, 'success_message_font_size');
            et_pb_responsive_options()->generate_responsive_css($d_paragraph_size, "$css_selector p", 'font-size', $this->render_slug);
            $d_paragraph_letter = et_pb_responsive_options()->get_property_values($this->props, 'success_message_letterspace');
            et_pb_responsive_options()->generate_responsive_css($d_paragraph_letter, "$css_selector p", 'letter-spacing', $this->render_slug);
            $d_paragraph_line = et_pb_responsive_options()->get_property_values($this->props, 'success_message_lineheight');
            et_pb_responsive_options()->generate_responsive_css($d_paragraph_line, "$css_selector p", 'line-height', $this->render_slug);
            $d_paragraph_alignment = et_pb_responsive_options()->get_property_values($this->props, 'success_message_alignment');
            et_pb_responsive_options()->generate_responsive_css($d_paragraph_alignment, "$css_selector p", 'text-align', $this->render_slug, '', 'text_alignment');
        }

    }
}