<?php

namespace PWH_DCFH\App\Frontend\DiviContactForm\Features;

use PWH_DCFH\App\Controllers\PWH_DCFH_DOM;
use DOMXPath;
use ET_Builder_Element;
use DOMException;

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

        public $output;

        public $props = [];

        public $contact_form_order_class;

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

        /**
         * Render Output
         * @throws DOMException
         */
        public function output()
        {
            $dom = PWH_DCFH_DOM::dom($this->output);
            $dom_xpath = new DOMXPath($dom);
            // $form = $dom_xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), '$contact_form_order_class')]");
            $form = $dom_xpath->query('//div[contains(@class,"'.$this->contact_form_order_class.'")]');
            if (isset($form->length) && 0 !== $form->length) {
                $form_item = $form->item(0);
                // $et_pb_contact_message = $dom_xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), 'et-pb-contact-message')]");
                $et_pb_contact_message = $dom_xpath->query('//div[contains(@class,"et-pb-contact-message")]');
                if (isset($et_pb_contact_message->length) && 0 !== $et_pb_contact_message->length) {
                    $et_pb_contact_message_top = $et_pb_contact_message->item(0);
                    $et_pb_contact_message_top->setAttribute('class', 'et-pb-contact-message et_pb_contact_message_top');
                    $et_pb_contact_message_bottom = $dom->createElement('div');
                    $et_pb_contact_message_bottom->setAttribute('class', 'et-pb-contact-message et_pb_contact_message_bottom');
                    $form_item->appendChild($et_pb_contact_message_bottom);
                    $this->output = $dom->saveHTML();
                }
            }

            return $this->output;
        }

        /**
         * Render Css
         * @return void
         */
        public function css()
        {
            $css_selector = '%%order_class%% .et-pb-contact-message.et_pb_contact_error_meesage';
            /**
             * Location
             */
            $contact_message_location = $this->props['contact_message_location'] ?? 'top';
            $contact_message_location_tablet = $this->props['contact_message_location_tablet'] ?? $contact_message_location;
            $contact_message_location_phone = $this->props['contact_message_location_phone'] ?? $contact_message_location;
            $contact_message_responsive = et_pb_responsive_options()->is_responsive_enabled($this->props, 'contact_message_location');
            if ('bottom' === $contact_message_location) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "%%order_class%% .et_pb_contact_message_top",
                    'declaration' => "display:none",
                    'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                ]);
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "%%order_class%% .et_pb_contact_message_bottom",
                    'declaration' => "display:block;",
                    'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                ]);
            }
            if ($contact_message_responsive && 'bottom' === $contact_message_location_tablet) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "%%order_class%% .et_pb_contact_message_top",
                    'declaration' => "display:none",
                    'media_query' => ET_Builder_Element::get_media_query('768_980'),
                ]);
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "%%order_class%% .et_pb_contact_message_bottom",
                    'declaration' => "display:block",
                    'media_query' => ET_Builder_Element::get_media_query('768_980'),
                ]);
            }
            if ($contact_message_responsive && 'bottom' === $contact_message_location_phone) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "%%order_class%% .et_pb_contact_message_top",
                    'declaration' => "display:none",
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "%%order_class%% .et_pb_contact_message_bottom",
                    'declaration' => "display:block",
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
            /**
             * Container
             */
            $d_wrapper_background = pwh_dcfh_module_helpers()::get_global_color(et_pb_responsive_options()->get_property_values($this->props, 'errormessage_wrapper_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, 'errormessage_wrapper_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, 'errormessage_wrapper_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, 'errormessage_wrapper_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, 'errormessage_wrapper_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, 'errormessage_wrapper_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, 'errormessage_wrapper_border_style');
            et_pb_responsive_options()->generate_responsive_css($d_wrapper_border_style, $css_selector, 'border-style', $this->render_slug, '', 'select');
            $shadow_preset = $this->props['errormessage_wrapper_shadow'] ?? null;
            if (!empty($shadow_preset) && 'none' !== $shadow_preset) {
                $shadow = pwh_dcfh_module_helpers()::get_shadow_presets_values($shadow_preset);
                $horizontal = $this->props['errormessage_wrapper_shadow_horizontal'] ?? $shadow['horizontal'];
                $vertical = $this->props['errormessage_wrapper_shadow_vertical'] ?? $shadow['vertical'];
                $blur = $this->props['errormessage_wrapper_shadow_blur'] ?? $shadow['blur'];
                $spread = $this->props['errormessage_wrapper_shadow_spread'] ?? $shadow['spread'];
                $color = $this->props['errormessage_wrapper_shadow_color'] ?? 'rgba(0,0,0,0.3)';
                $position = $this->props['errormessage_wrapper_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;",
                ]);
            }
            /**
             * Fill Font
             */
            $is_font_responsive = et_pb_responsive_options()->is_responsive_enabled($this->props, 'errormessage_fill_font');
            $desktop_fill_font = $this->props['errormessage_fill_font'] ?? null;
            $tablet_fill_font = $this->props['errormessage_fill_font_tablet'] ?? null;
            $phone_fill_font = $this->props['errormessage_fill_font_phone'] ?? null;
            if (!empty($desktop_fill_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector p",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($desktop_fill_font)),
                ]);
            }
            if ($is_font_responsive && !empty($tablet_fill_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector p",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($tablet_fill_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                ]);
            }
            if ($is_font_responsive && !empty($phone_fill_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector p",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($phone_fill_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
            $d_fill_fontcolor = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_fill_fontcolor');
            et_pb_responsive_options()->generate_responsive_css($d_fill_fontcolor, "$css_selector p", 'color', $this->render_slug, '', 'color');
            $d_fill_fontsize = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_fill_fontsize');
            et_pb_responsive_options()->generate_responsive_css($d_fill_fontsize, "$css_selector p", 'font-size', $this->render_slug);
            $d_fill_letterspace = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_fill_letterspace');
            et_pb_responsive_options()->generate_responsive_css($d_fill_letterspace, "$css_selector p", 'letter-spacing', $this->render_slug);
            $line_height_important = et_builder_has_limitation('force_use_global_important') ? ' !important;' : '';
            $d_fill_lineheight = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_fill_lineheight');
            et_pb_responsive_options()->generate_responsive_css($d_fill_lineheight, "$css_selector p", 'line-height', $this->render_slug, $line_height_important);
            $d_fill_alignment = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_fill_alignment');
            et_pb_responsive_options()->generate_responsive_css($d_fill_alignment, "$css_selector p", 'text-align', $this->render_slug, '', 'text_alignment');
            /**
             * Error Font
             */
            $is_font_responsive = et_pb_responsive_options()->is_responsive_enabled($this->props, 'errormessage_errors_font');
            $desktop_errors_font = $this->props['errormessage_errors_font'] ?? null;
            $tablet_errors_font = $this->props['errormessage_errors_font_tablet'] ?? null;
            $phone_errors_font = $this->props['errormessage_errors_font_phone'] ?? null;
            if (!empty($desktop_errors_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector ul",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($desktop_errors_font)),
                ]);
            }
            if ($is_font_responsive && !empty($tablet_errors_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector ul",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($tablet_errors_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                ]);
            }
            if ($is_font_responsive && !empty($phone_errors_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => "$css_selector ul",
                    'declaration' => sprintf('%s;', et_builder_set_element_font($phone_errors_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
            $d_errors_fontcolor = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_errors_fontcolor');
            et_pb_responsive_options()->generate_responsive_css($d_errors_fontcolor, "$css_selector ul", 'color', $this->render_slug, '', 'color');
            $d_errors_fontsize = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_errors_fontsize');
            et_pb_responsive_options()->generate_responsive_css($d_errors_fontsize, "$css_selector ul", 'font-size', $this->render_slug);
            $d_errors_letterspace = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_errors_letterspace');
            et_pb_responsive_options()->generate_responsive_css($d_errors_letterspace, "$css_selector ul", 'letter-spacing', $this->render_slug);
            $line_height_important = et_builder_has_limitation('force_use_global_important') ? ' !important;' : '';
            $d_errors_lineheight = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_errors_lineheight');
            et_pb_responsive_options()->generate_responsive_css($d_errors_lineheight, "$css_selector ul", 'line-height', $this->render_slug, $line_height_important);
            $d_errors_alignment = et_pb_responsive_options()->get_property_values($this->props, 'errormessage_errors_alignment');
            et_pb_responsive_options()->generate_responsive_css($d_errors_alignment, "$css_selector ul", 'text-align', $this->render_slug, '', 'text_alignment');
        }

    }
}