<?php

namespace PWH_DCFH\App\Frontend\DiviContactForm\Features;

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

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('PWH_DCFH_Field_Layout')) {
    class PWH_DCFH_Field_Layout
    {
        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;
        }

        /**
         * Get Field Type
         * @return mixed
         */
        private function get_field_type()
        {
            return $this->props['field_type'];
        }

        /**
         * Get Field Required Status
         * @return mixed|string
         */
        private function get_required_mark_status()
        {
            return isset($this->props['required_mark']) ? $this->props['required_mark'] : 'on';
        }

        /**
         * Get Field Asterisk Status
         * @return mixed|string
         */
        private function get_required_asterisk_status()
        {
            return isset($this->props['use_required_asterisk']) ? $this->props['use_required_asterisk'] : 'on';
        }

        /**
         * Get Field Label Status
         * @return mixed|string
         */
        private function get_field_label_status()
        {
            return isset($this->props['use_field_label']) ? $this->props['use_field_label'] : 'off';
        }

        /**
         * Get Field Placeholder Status
         * @return mixed|string
         */
        private function get_field_placeholder_status()
        {
            return isset($this->props['use_field_placeholder']) ? $this->props['use_field_placeholder'] : 'off';
        }

        /**
         * Get Field Placeholder
         * @return mixed|string
         */
        private function get_field_placeholder()
        {
            return isset($this->props['field_placeholder']) ? $this->props['field_placeholder'] : '';
        }

        /**
         * Get Field Desc
         * @return mixed|string
         */
        private function get_field_description()
        {
            return isset($this->props['field_description']) ? $this->props['field_description'] : '';
        }

        /**
         * Get Field Desc Location
         * @return mixed|string
         */
        private function get_field_description_location()
        {
            return isset($this->props['field_description_location']) ? $this->props['field_description_location'] : 'below';
        }

        /**
         * Is Allowed To Add Icon
         * @return bool
         */
        private function is_allow_to_add_icon()
        {
            if (
                (isset($this->props['use_datetime']) && 'on' == $this->props['use_datetime'])
                && (isset($this->props['datetime_inline'])) && 'on' == $this->props['datetime_inline']) {
                return false;
            }

            return true;
        }

        /**
         * Add Hidden Input Label
         * @throws DOMException
         * @return false|string
         */
        public function add_conditional_hidden_input_label()
        {
            if (isset($this->props['conditional_logic']) && 'on' === $this->props['conditional_logic'] && isset($this->props['field_title']) && !empty($this->props['field_title'])) {
                $dom = PWH_DCFH_DOM::dom($this->output);
                $qualified_name = 'input';
                $type = $this->props['field_type'];
                if ('text' === $type) {
                    $qualified_name = 'textarea';
                } elseif ('select' === $type) {
                    $qualified_name = 'select';
                }
                $input = $dom->getElementsByTagName($qualified_name);
                $p_tag = $dom->getElementsByTagName('p');
                if ((isset($input->length) && 0 !== $input->length) && (isset($p_tag->length) && 0 !== $p_tag->length)) {

                    $input_item = $input->item(0);
                    $p_item = $p_tag->item(0);
                    $input_name = $input_item->getAttribute('name');
                    $hidden_input = $dom->createElement('input');
                    $hidden_input->setAttribute('type', 'hidden');
                    $hidden_input->setAttribute('readonly', 'readonly');
                    $hidden_input->setAttribute('name', "{$input_name}_field_label");
                    $hidden_input->setAttribute('value', "{$this->props['field_title']}|{$this->props['field_type']}");
                    $p_item->appendChild($hidden_input);
                    $this->output = $dom->saveHTML();

                }
            }

            return $this->output;
        }

        /**
         * Add Desc and Placeholder
         * @throws DOMException
         * @return false|string
         */
        public function field_desc_placeholder()
        {
            $field_type = $this->get_field_type();
            $use_field_label = $this->get_field_label_status();
            $required_mark = $this->get_required_mark_status();
            $use_required_asterisk = $this->get_required_asterisk_status();
            $field_description = $this->get_field_description();
            $field_description_location = $this->get_field_description_location();
            $hide_field_placeholder = $this->get_field_placeholder_status();
            $field_placeholder = $this->get_field_placeholder();
            $field_order_class = ET_Builder_Element::get_module_order_class($this->render_slug);
            $field_index = pwh_dcfh_helpers()::get_field_index($field_order_class);
            $asterisk = ('on' === $required_mark && 'on' === $use_required_asterisk && 'off' === $use_field_label) ? ' *' : '';

            $dom = PWH_DCFH_DOM::dom($this->output);
            $p_tag = $dom->getElementsByTagName('p');
            if ((isset($p_tag->length) && 0 !== $p_tag->length)) {
                $p_item = $p_tag->item(0);
                $span_element = null;
                $attr_multi_view_key = 'data-et-multi-view';
                // Create Description Span
                if (!empty($field_description)) {
                    $span_element = $dom->createElement('span');
                    $span_element->setAttribute('id', "et_pb_contact_field_desc_$field_index");
                    $span_element->setAttribute('class', 'et_pb_contact_field_desc');
                    $span_element->appendChild($dom->createCDATASection(wp_kses($field_description, self::get_allowed_html_tags())));
                }
                // Field Placeholder and Description
                if (in_array($field_type, ['input', 'email', 'text', 'select'])) {
                    if ('text' === $field_type) {
                        $qualified_name = 'textarea';
                    } elseif ('select' === $field_type) {
                        $qualified_name = 'select';
                    } else {
                        $qualified_name = 'input';
                    }
                    $input_tag = $dom->getElementsByTagName($qualified_name);
                    $input_item = $input_tag->item(0);
                    if ('on' === $required_mark) {
                        $input_item->setAttribute('aria-required', 'true');
                    }
                    if (in_array($field_type, ['input', 'email', 'text'])) {
                        if ($input_item->hasAttribute($attr_multi_view_key) && !empty($field_placeholder)) {
                            $multi_view = $input_item->getAttribute($attr_multi_view_key);
                            $multi_view = json_decode($multi_view, true);
                            if (!empty($multi_view) && isset($multi_view['schema']['attrs'])) {
                                foreach ($multi_view['schema']['attrs'] as $k => $v) {
                                    if (isset($multi_view['schema']['attrs'][$k]['placeholder'])) {
                                        $multi_view['schema']['attrs'][$k]['placeholder'] = $field_placeholder;
                                    }
                                }
                                $input_item->setAttribute($attr_multi_view_key, wp_json_encode($multi_view));
                            }
                        }
                        $field_placeholder = empty($field_placeholder) ? $input_item->getAttribute('placeholder') : $field_placeholder;
                        $input_item->setAttribute('placeholder', $field_placeholder.$asterisk);
                    }
                    if (!empty($span_element)) {
                        'below' === $field_description_location ? $p_item->appendChild($span_element) : $p_item->insertBefore($span_element, $input_item);
                    }
                    if ('select' === $field_type) {
                        $selected_index = null;
                        $select_options = $this->props['select_options'];
                        if (!empty($select_options)) {
                            $select_options = json_decode(str_replace(['&#91;', '&#93;'], ['[', ']'], $select_options));
                            if (!empty($select_options)) {
                                $selected_index = array_search(1, array_column($select_options, 'checked'));
                                if (!empty($selected_index)) {
                                    $selected_index = 'on' === $hide_field_placeholder ? $selected_index : ($selected_index + 1);
                                }
                            }
                        }
                        $options = $dom->getElementsByTagName('option');
                        for ($i = 0; $i < $options->length; $i++) {
                            $option_item = $options->item($i);
                            // Replace Custom Placeholder
                            if ('off' === $hide_field_placeholder & 0 === $i) {
                                if ($option_item->hasAttribute($attr_multi_view_key) && !empty($field_placeholder)) {
                                    $multi_view = $option_item->getAttribute($attr_multi_view_key);
                                    $multi_view = json_decode($multi_view, true);
                                    if (!empty($multi_view) && isset($multi_view['schema']['content'])) {
                                        foreach ($multi_view['schema']['content'] as $k => $v) {
                                            if (isset($multi_view['schema']['content'][$k])) {
                                                $multi_view['schema']['content'][$k] = $field_placeholder;
                                            }
                                        }
                                        $option_item->setAttribute($attr_multi_view_key, wp_json_encode($multi_view));
                                    }
                                }
                                $option_item->nodeValue = empty($field_placeholder) ? $option_item->nodeValue.$asterisk : $field_placeholder.$asterisk;
                            }
                            // Remove Placeholder
                            if ('on' === $hide_field_placeholder && 0 === $i) {
                                $option_item->parentNode->removeChild($option_item);
                            }
                            // Set Selected Option
                            if ($selected_index === $i) {
                                $option_item->setAttribute('selected', 'selected');
                            }
                        }
                    }
                }
                // Checkbox and Radio Field Description
                if (in_array($field_type, ['checkbox', 'radio'])) {
                    if (!empty($span_element)) {
                        $dom_xpath = new DOMXPath($dom);
                        $options_wrapper = $dom_xpath->query('//span[contains(@class,"et_pb_contact_field_options_wrapper")]');
                        $options_list = $dom_xpath->query('//span[contains(@class,"et_pb_contact_field_options_list")]');
                        'below' === $field_description_location ? $p_item->appendChild($span_element) : $options_wrapper->item(0)->insertBefore($span_element, $options_list->item(0));
                    }
                }
                $this->output = $dom->saveHTML();
            }

            return $this->output;
        }

        /**
         *  Add Field Icon
         * @throws DOMException
         * @return false|string
         */
        public function field_icon()
        {
            // IF Datepicker Inline Return
            if (false === $this->is_allow_to_add_icon()) {
                return $this->output;
            }
            $field_type = $this->get_field_type();
            $field_icon = isset($this->props['field_icon']) ? $this->props['field_icon'] : '';
            $field_icon_color = !empty($this->props['field_icon_color']) ? $this->props['field_icon_color'] : '#999';
            if (!empty($field_icon)) {
                $dom = PWH_DCFH_DOM::dom($this->output);
                // IF Input, Email & Text
                if (in_array($field_type, ['input', 'email', 'text'])) {
                    $qualified_name = in_array($field_type, ['input', 'email']) ? 'input' : 'textarea';
                    $p_tag = $dom->getElementsByTagName('p');
                    $input_tag = $dom->getElementsByTagName($qualified_name);
                    if (isset($p_tag->length) && 0 !== $p_tag->length && isset($input_tag->length) && 0 !== $input_tag->length) {
                        $input_item = $input_tag->item(0);
                        $p_item = $p_tag->item(0);
                        $field_icon_span = $dom->createElement('span');
                        $field_order_class = ET_Builder_Element::get_module_order_class($this->render_slug);
                        $field_index = pwh_dcfh_helpers()::get_field_index($field_order_class);
                        $field_icon_span->setAttribute('id', "et_pb_contact_field_icon_$field_index");
                        $field_icon_span->setAttribute('class', 'et_pb_contact_field_icon');
                        $field_icon_span->setAttribute('data-icon', html_entity_decode(esc_attr(et_pb_process_font_icon($field_icon))));
                        $field_icon_span->setAttribute('data-field-type', $field_type);
                        $p_item->insertBefore($field_icon_span, $input_item);
                        $this->output = $dom->saveHTML();
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => 'p.et_pb_contact_field%%order_class%% input,p.et_pb_contact_field%%order_class%% textarea',
                            'declaration' => 'text-indent:1.3rem;',
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => "p.et_pb_contact_field%%order_class%% .et_pb_contact_field_icon:before",
                            'declaration' => sprintf('color:%s;', $field_icon_color),
                        ]);
                        if ((function_exists('et_pb_get_icon_font_family') && function_exists('et_pb_get_icon_font_weight'))) {
                            ET_Builder_Element::set_style($this->render_slug, [
                                'selector' => "p.et_pb_contact_field%%order_class%% .et_pb_contact_field_icon:before",
                                'declaration' => sprintf("font-family:%s;font-weight:%s;", et_pb_get_icon_font_family($field_icon), et_pb_get_icon_font_weight($field_icon)),
                            ]);
                        } else {
                            ET_Builder_Element::set_style($this->render_slug, [
                                'selector' => "p.et_pb_contact_field%%order_class%% .et_pb_contact_field_icon:before",
                                'declaration' => "font-family:ETModules;",
                            ]);
                        }
                    }
                }
                // IF Checkbox, Radio & Select
                if (in_array($field_type, ['checkbox', 'radio', 'select'])) {
                    $dom_xpath = new DOMXPath($dom);
                    $css_selector = in_array($field_type, ['radio', 'checkbox']) ? 'et_pb_contact_field_options_wrapper' : 'et_pb_contact_form_label';
                    $field_options_wrapper = $dom_xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), '$css_selector')]");
                    if ((isset($field_options_wrapper->length) && 0 !== $field_options_wrapper->length)) {
                        $field_options_wrapper[0]->setAttribute('data-icon', html_entity_decode(esc_attr(et_pb_process_font_icon($field_icon))));
                        $this->output = $dom->saveHTML();
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => "p.et_pb_contact_field%%order_class%% .et_pb_contact_form_label",
                            'declaration' => 'padding-left:18px;',
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => "p.et_pb_contact_field%%order_class%% .et_pb_contact_field_options_title",
                            'declaration' => 'padding-left:24px;',
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => "p.et_pb_contact_field%%order_class%% .$css_selector:before",
                            'declaration' => sprintf('content: attr(data-icon);color:%s;', $field_icon_color),
                        ]);
                        if ((function_exists('et_pb_get_icon_font_family') && function_exists('et_pb_get_icon_font_weight'))) {
                            ET_Builder_Element::set_style($this->render_slug, [
                                'selector' => "p.et_pb_contact_field%%order_class%% .$css_selector:before",
                                'declaration' => sprintf("font-family:%s;font-weight:%s;", et_pb_get_icon_font_family($field_icon), et_pb_get_icon_font_weight($field_icon)),
                            ]);
                        } else {
                            ET_Builder_Element::set_style($this->render_slug, [
                                'selector' => "p.et_pb_contact_field%%order_class%% .$css_selector:before",
                                'declaration' => "font-family:ETModules;",
                            ]);
                        }
                    }
                }
            }

            return $this->output;
        }

        /**
         * Show Hide Label and Placeholder
         * @return void
         */
        public function show_hide_field_label_placeholder()
        {
            $field_type = $this->get_field_type();
            $required_mark = $this->get_required_mark_status();
            $use_required_asterisk = $this->get_required_asterisk_status();
            $use_field_label = $this->get_field_label_status();
            $field_description = $this->get_field_description();
            $field_description_location = $this->get_field_description_location();
            // Show Label Text, Input, Email and Select
            if ('on' === $use_field_label) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_form_label',
                    'declaration' => 'display:flex;',
                ]);
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_form_label',
                    'declaration' => 'font-weight: 600;margin-bottom: 0.5em;',
                ]);
                $show_field_labels_left = isset($this->props['show_field_labels_left']) ? $this->props['show_field_labels_left'] : 'off';
                $show_field_labels_left_tablet = isset($this->props['show_field_labels_left_tablet']) ? $this->props['show_field_labels_left_tablet'] : 'off';
                $show_field_labels_left_phone = isset($this->props['show_field_labels_left_phone']) ? $this->props['show_field_labels_left_phone'] : 'off';
                $show_field_labels_left_responsive = et_pb_responsive_options()->is_responsive_enabled($this->props, 'show_field_labels_left');
                $d_label_width = et_pb_responsive_options()->get_property_values($this->props, 'field_label_width');
                $d_label_width_desktop = (isset($d_label_width['desktop']) && empty($d_label_width['desktop'])) ? '120px' : $d_label_width['desktop'];
                $d_label_width_tablet = (isset($d_label_width['tablet']) && empty($d_label_width['tablet'])) ? $d_label_width_desktop : $d_label_width['tablet'];
                $d_label_width_phone = (isset($d_label_width['phone']) && empty($d_label_width['phone'])) ? $d_label_width_tablet : $d_label_width['phone'];
                if ('on' === $show_field_labels_left) {
                    ET_Builder_Element::set_style($this->render_slug, [
                        'selector' => '%%order_class%%.et_pb_contact_field',
                        'declaration' => 'display: flex;flex-wrap: wrap;',
                        '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_field .et_pb_contact_form_label',
                        'declaration' => "flex: 0 0 $d_label_width_desktop;",
                        '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_field input[type="text"],%%order_class%%.et_pb_contact_field input[type="email"],%%order_class%%.et_pb_contact_field input[type="number"],%%order_class%%.et_pb_contact_field textarea,%%order_class%%.et_pb_contact_field select',
                        'declaration' => "width: calc(100% - $d_label_width_desktop) !important;",
                        '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_field .et_pb_contact_field_desc',
                        'declaration' => 'flex-basis: 100%;width: 100% !important;order: 3;',
                        'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                    ]);
                }
                if ('on' === $show_field_labels_left_tablet && $show_field_labels_left_responsive) {
                    ET_Builder_Element::set_style($this->render_slug, [
                        'selector' => '%%order_class%%.et_pb_contact_field',
                        'declaration' => 'display: flex;flex-wrap: wrap;',
                        'media_query' => ET_Builder_Element::get_media_query('768_980'),
                    ]);
                    ET_Builder_Element::set_style($this->render_slug, [
                        'selector' => '%%order_class%%.et_pb_contact_field .et_pb_contact_form_label',
                        'declaration' => "flex: 0 0 $d_label_width_tablet;",
                        'media_query' => ET_Builder_Element::get_media_query('768_980'),
                    ]);
                    ET_Builder_Element::set_style($this->render_slug, [
                        'selector' => '%%order_class%%.et_pb_contact_field input[type="text"],%%order_class%%.et_pb_contact_field input[type="email"],%%order_class%%.et_pb_contact_field input[type="number"],%%order_class%%.et_pb_contact_field textarea,%%order_class%%.et_pb_contact_field select',
                        'declaration' => "width: calc(100% - $d_label_width_tablet) !important;",
                        'media_query' => ET_Builder_Element::get_media_query('768_980'),
                    ]);
                    ET_Builder_Element::set_style($this->render_slug, [
                        'selector' => '%%order_class%%.et_pb_contact_field .et_pb_contact_field_desc',
                        'declaration' => 'flex-basis: 100%;width: 100% !important;order: 3;',
                        'media_query' => ET_Builder_Element::get_media_query('768_980'),
                    ]);
                }
                if ('on' === $show_field_labels_left_phone && $show_field_labels_left_responsive) {
                    ET_Builder_Element::set_style($this->render_slug, [
                        'selector' => '%%order_class%%.et_pb_contact_field',
                        'declaration' => 'display: flex;flex-wrap: wrap;',
                        '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_field .et_pb_contact_form_label',
                        'declaration' => "flex: 0 0 $d_label_width_phone;",
                        '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_field input[type="text"],%%order_class%%.et_pb_contact_field input[type="email"],%%order_class%%.et_pb_contact_field input[type="number"],%%order_class%%.et_pb_contact_field textarea,%%order_class%%.et_pb_contact_field select',
                        'declaration' => "width: calc(100% - $d_label_width_phone) !important;",
                        '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_field .et_pb_contact_field_desc',
                        'declaration' => 'flex-basis: 100%;width: 100% !important;order: 3;',
                        'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                    ]);

                }
            }
            if ('select' === $field_type) {
                $select_declaration = '';
                if ('off' === $use_field_label && !empty($field_description)) {
                    $select_declaration = 'below' === $field_description_location ? 'top:30% !important;' : 'top:65% !important;';
                }
                if ('on' === $use_field_label && !empty($field_description)) {
                    $select_declaration = 'below' === $field_description_location ? 'top:54% !important;' : 'top:75% !important;';
                }
                if ('on' === $use_field_label && empty($field_description)) {
                    $select_declaration = 'top:75% !important';
                }
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%%.et_pb_contact_field[data-type=select]:after',
                    'declaration' => $select_declaration,
                ]);
            }
            // Hide Label Checkbox and Radio
            $use_field_label_cr = isset($this->props['use_field_label_cr']) ? $this->props['use_field_label_cr'] : 'off';
            if ('off' === $use_field_label_cr) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_field_options_title',
                    'declaration' => 'display:none;',
                ]);
            }
            // Hide Placeholder
            $hide_field_placeholder = $this->get_field_placeholder_status();
            if ('on' === $hide_field_placeholder) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%%  ::-webkit-input-placeholder,%%order_class%%  ::-moz-placeholder,%%order_class%%  ::-ms-input-placeholder',
                    'declaration' => 'color:transparent !important;',
                ]);
            }
            // Show/Hide Asterisk
            if ('on' === $use_field_label && 'on' === $required_mark && 'on' === $use_required_asterisk) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_form_label:after',
                    'declaration' => 'content: "*";',
                ]);
            }
            if (in_array($field_type, ['checkbox', 'radio']) && 'on' === $required_mark && 'on' === $use_required_asterisk) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_field_options_title:after',
                    'declaration' => 'content: "*";',
                ]);
            }
        }

        /**
         * Render Label Design Settings
         * @return void
         */
        public function field_label_css()
        {
            $css_selector = '%%order_class%% .et_pb_contact_form_label,%%order_class%% .et_pb_contact_field_options_title';
            $desktop_font = isset($this->props['field_label_font']) ? $this->props['field_label_font'] : null;
            $tablet_font = isset($this->props['field_label_font_tablet']) ? $this->props['field_label_font_tablet'] : null;
            $phone_font = isset($this->props['field_label_font_phone']) ? $this->props['field_label_font_phone'] : null;
            // Text Font
            if (!empty($desktop_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => $css_selector,
                    '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,
                    '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,
                    'declaration' => sprintf('%s;', et_builder_set_element_font($phone_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
            $d_text_color = et_pb_responsive_options()->get_property_values($this->props, 'field_label_color');
            et_pb_responsive_options()->generate_responsive_css($d_text_color, $css_selector, 'color', $this->render_slug, '', 'color');
            $d_background = pwh_dcfh_module_helpers()::get_global_color(et_pb_responsive_options()->get_property_values($this->props, 'field_label_background'));
            et_pb_responsive_options()->generate_responsive_css($d_background, $css_selector, 'background', $this->render_slug, '', 'color');
            $d_text_size = et_pb_responsive_options()->get_property_values($this->props, 'field_label_size');
            et_pb_responsive_options()->generate_responsive_css($d_text_size, $css_selector, 'font-size', $this->render_slug);
            $d_text_letter = et_pb_responsive_options()->get_property_values($this->props, 'field_label_letter_space');
            et_pb_responsive_options()->generate_responsive_css($d_text_letter, $css_selector, 'letter-spacing', $this->render_slug);
            $d_text_line = et_pb_responsive_options()->get_property_values($this->props, 'field_label_line_height');
            et_pb_responsive_options()->generate_responsive_css($d_text_line, $css_selector, 'line-height', $this->render_slug);
            $d_text_alignment = et_pb_responsive_options()->get_property_values($this->props, 'field_label_alignment');
            if (!empty($d_text_alignment)) {
                $d_text_alignment = pwh_dcfh_module_helpers()::get_flex_alignment($d_text_alignment);
            }
            $show_field_labels_left = isset($this->props['show_field_labels_left']) ? $this->props['show_field_labels_left'] : 'off';
            if ('off' === $show_field_labels_left) {
                et_pb_responsive_options()->generate_responsive_css($d_text_alignment, $css_selector, 'justify-content', $this->render_slug, '', 'text_alignment');
            } else {
                et_pb_responsive_options()->generate_responsive_css($d_text_alignment, $css_selector, 'align-items', $this->render_slug, '', 'text_alignment');
            }
            $d_margin = et_pb_responsive_options()->get_property_values($this->props, 'field_label_margin');
            $d_margin = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_margin);
            et_pb_responsive_options()->generate_responsive_css($d_margin, $css_selector, 'margin', $this->render_slug, '', 'custom_padding');
            $d_padding = et_pb_responsive_options()->get_property_values($this->props, 'field_label_padding');
            $d_padding = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_padding);
            et_pb_responsive_options()->generate_responsive_css($d_padding, $css_selector, 'padding', $this->render_slug, '', 'custom_padding');
            $d_border = et_pb_responsive_options()->get_property_values($this->props, 'field_label_border');
            $d_border = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_border);
            et_pb_responsive_options()->generate_responsive_css($d_border, $css_selector, 'border-radius', $this->render_slug, '', 'border-radius');
            $d_border_color = et_pb_responsive_options()->get_property_values($this->props, 'field_label_border_color');
            et_pb_responsive_options()->generate_responsive_css($d_border_color, $css_selector, 'border-color', $this->render_slug, '', 'color');
            $d_border_width = et_pb_responsive_options()->get_property_values($this->props, 'field_label_border_width');
            et_pb_responsive_options()->generate_responsive_css($d_border_width, $css_selector, 'border-width', $this->render_slug);
            $d_border_style = et_pb_responsive_options()->get_property_values($this->props, 'field_label_border_style');
            et_pb_responsive_options()->generate_responsive_css($d_border_style, $css_selector, 'border-style', $this->render_slug, '', 'select');
        }

        /**
         * Render Global Label Design Settings
         * @return void
         */
        public function field_desc_css()
        {
            $css_selector = '%%order_class%% .et_pb_contact_field_desc';
            $desktop_font = isset($this->props['field_desc_font']) ? $this->props['field_desc_font'] : null;
            $tablet_font = isset($this->props['field_desc_font_tablet']) ? $this->props['field_desc_font_tablet'] : null;
            $phone_font = isset($this->props['field_desc_font_phone']) ? $this->props['field_desc_font_phone'] : null;
            if (!empty($desktop_font)) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => $css_selector,
                    '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,
                    '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,
                    'declaration' => sprintf('%s;', et_builder_set_element_font($phone_font)),
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
            $d_text_color = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_color');
            et_pb_responsive_options()->generate_responsive_css($d_text_color, $css_selector, 'color', $this->render_slug, '', 'color');
            $d_background = pwh_dcfh_module_helpers()::get_global_color(et_pb_responsive_options()->get_property_values($this->props, 'field_desc_background'));
            et_pb_responsive_options()->generate_responsive_css($d_background, $css_selector, 'background', $this->render_slug, '', 'color');
            $d_text_size = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_size');
            et_pb_responsive_options()->generate_responsive_css($d_text_size, $css_selector, 'font-size', $this->render_slug);
            $d_text_letter = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_letter_space');
            et_pb_responsive_options()->generate_responsive_css($d_text_letter, $css_selector, 'letter-spacing', $this->render_slug);
            $d_text_line = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_line_height');
            et_pb_responsive_options()->generate_responsive_css($d_text_line, $css_selector, 'line-height', $this->render_slug);
            $d_text_alignment = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_alignment');
            if (!empty($d_text_alignment)) {
                $d_text_alignment = pwh_dcfh_module_helpers()::get_flex_alignment($d_text_alignment);
            }
            et_pb_responsive_options()->generate_responsive_css($d_text_alignment, $css_selector, 'justify-content', $this->render_slug, '', 'text_alignment');
            $d_margin = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_margin');
            $d_margin = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_margin);
            et_pb_responsive_options()->generate_responsive_css($d_margin, $css_selector, 'margin', $this->render_slug, '', 'custom_padding');
            $d_padding = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_padding');
            $d_padding = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_padding);
            et_pb_responsive_options()->generate_responsive_css($d_padding, $css_selector, 'padding', $this->render_slug, '', 'custom_padding');
            $d_border = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_border');
            $d_border = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props($d_border);
            et_pb_responsive_options()->generate_responsive_css($d_border, $css_selector, 'border-radius', $this->render_slug, '', 'border-radius');
            $d_border_color = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_border_color');
            et_pb_responsive_options()->generate_responsive_css($d_border_color, $css_selector, 'border-color', $this->render_slug, '', 'color');
            $d_border_width = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_border_width');
            et_pb_responsive_options()->generate_responsive_css($d_border_width, $css_selector, 'border-width', $this->render_slug);
            $d_border_style = et_pb_responsive_options()->get_property_values($this->props, 'field_desc_border_style');
            et_pb_responsive_options()->generate_responsive_css($d_border_style, $css_selector, 'border-style', $this->render_slug, '', 'select');
        }

        /**
         * Render Input Focus
         * @return void
         */
        public function field_focus_css()
        {
            $css_selector = '.input:focus,.input[type="text"]:active + label i,.input[type="checkbox"]:active + label i,.input[type="radio"]:active + label i';
            $d_border = pwh_dcfh_module_helpers()::get_responsive_margin_padding_border_props(et_pb_responsive_options()->get_property_values($this->props, 'input_focus_border'));
            et_pb_responsive_options()->generate_responsive_css($d_border, $css_selector, 'border-radius', $this->render_slug, '', 'border-radius');
            $d_border_color = et_pb_responsive_options()->get_property_values($this->props, 'input_focus_border_color');
            et_pb_responsive_options()->generate_responsive_css($d_border_color, $css_selector, 'border-color', $this->render_slug, ' !important;', 'color');
            $d_border_width = et_pb_responsive_options()->get_property_values($this->props, 'input_focus_border_width');
            et_pb_responsive_options()->generate_responsive_css($d_border_width, $css_selector, 'border-width', $this->render_slug);
            $d_border_style = et_pb_responsive_options()->get_property_values($this->props, 'input_focus_border_style');
            et_pb_responsive_options()->generate_responsive_css($d_border_style, $css_selector, 'border-style', $this->render_slug, '', 'select');
        }

        /**
         * Render Textarea Min Height CSS
         * @return void
         */
        public function input_textarea_css()
        {
            $devices_options = et_pb_responsive_options()->get_property_values($this->props, 'textarea_min_height');
            if ('150px' === $devices_options['desktop']) {
                $devices_options['desktop'] = '';
            }
            if ('150px' === $devices_options['tablet']) {
                $devices_options['tablet'] = '';
            }
            if ('150px' === $devices_options['phone']) {
                $devices_options['phone'] = '';
            }
            if (!empty(array_filter($devices_options))) {
                et_pb_responsive_options()->generate_responsive_css($devices_options, '%%order_class%% textarea.et_pb_contact_message', 'min-height', $this->render_slug);
            }
        }

        /**
         * Render Checkboxes CSS
         * @return void
         */
        public function input_checkbox_css()
        {
            $checkbox_layout = isset($this->props['checkbox_layout']) ? $this->props['checkbox_layout'] : 'column';
            $devices_options = et_pb_responsive_options()->get_property_values($this->props, 'checkbox_columns');
            $devices_options['desktop'] = isset($devices_options['desktop']) ? $devices_options['desktop'] : '';
            $devices_options['tablet'] = (!empty($devices_options['tablet'])) ? $devices_options['tablet'] : $devices_options['desktop'];
            $devices_options['phone'] = (!empty($devices_options['phone'])) ? $devices_options['phone'] : $devices_options['tablet'];
            if ('column' === $checkbox_layout) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_field_options_list',
                    'declaration' => 'display: flex;flex-wrap: wrap;',
                ]);
                $devices_options = pwh_dcfh_module_helpers()::get_flex_column($devices_options);
                et_pb_responsive_options()->generate_responsive_css($devices_options, '%%order_class%% .et_pb_contact_field_checkbox', 'flex-basis', $this->render_slug, '', 'select');
            }
            if ('inline' === $checkbox_layout) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_field_options_list span',
                    'declaration' => 'display: inline-block;margin-right:30px;',
                ]);
            }
        }

        /**
         * Render Textarea Min Height CSS
         * @return void
         */
        public function input_radio_css()
        {
            $radio_layout = isset($this->props['radio_layout']) ? $this->props['radio_layout'] : 'column';
            $devices_options = et_pb_responsive_options()->get_property_values($this->props, 'radio_columns');
            $devices_options['desktop'] = isset($devices_options['desktop']) ? $devices_options['desktop'] : '';
            $devices_options['tablet'] = !empty($devices_options['tablet']) ? $devices_options['tablet'] : $devices_options['desktop'];
            $devices_options['phone'] = !empty($devices_options['phone']) ? $devices_options['phone'] : $devices_options['tablet'];
            if ('column' === $radio_layout) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_field_options_list',
                    'declaration' => 'display: flex;flex-wrap: wrap;',
                ]);
                $devices_options = pwh_dcfh_module_helpers()::get_flex_column($devices_options);
                et_pb_responsive_options()->generate_responsive_css($devices_options, '%%order_class%% .et_pb_contact_field_radio', 'flex-basis', $this->render_slug, '', 'select');
            }
            if ('inline' === $radio_layout) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_field_options_list span',
                    'declaration' => 'display:inline-block;margin-right:30px;',
                ]);
            }
        }

        /**
         * Get HTML Allowed Tags
         * @return array
         */
        private static function get_allowed_html_tags()
        {
            return [
                'a' => ['class' => true, 'id' => true, 'href' => true, 'rel' => true, 'title' => true, 'target' => true],
                'abbr' => ['title' => true],
                'br' => [],
                'b' => [],
                'cite' => [],
                'del' => [],
                'em' => [],
                'img' => ['alt' => true, 'class' => true, 'id' => true, 'height' => true, 'src' => true, 'width' => true, 'style' => true],
                'span' => ['class' => true, 'id' => true, 'title' => true, 'style' => true],
                'strike' => [],
                'strong' => [],
                'small' => [],
                'svg' => [
                    'class' => true,
                    'aria-hidden' => true,
                    'aria-labelledby' => true,
                    'role' => true,
                    'xmlns' => true,
                    'width' => true,
                    'height' => true,
                    'viewbox' => true,
                ],
                'g' => ['fill' => true],
                'title' => ['title' => true],
                'path' => [
                    'd' => true,
                    'fill' => true,
                ],
                'iframe' => [
                    'src' => true,
                    'height' => true,
                    'width' => true,
                    'frameborder' => true,
                    'allowfullscreen' => true
                ],
                'sub' => [],
                'sup' => [],
                'table' => [
                    'align' => true,
                    'bgcolor' => true,
                    'border' => true,
                    'cellpadding' => true,
                    'cellspacing' => true,
                    'rules' => true,
                    'summary' => true,
                    'width' => true,],
                'tbody' => [
                    'align' => true,
                    'char' => true,
                    'charoff' => true,
                    'valign' => true,
                ],
                'td' => [
                    'abbr' => true,
                    'align' => true,
                    'axis' => true,
                    'bgcolor' => true,
                    'char' => true,
                    'charoff' => true,
                    'colspan' => true,
                    'headers' => true,
                    'height' => true,
                    'nowrap' => true,
                    'rowspan' => true,
                    'scope' => true,
                    'valign' => true,
                    'width' => true,
                ],
                'tfoot' => [
                    'align' => true,
                    'char' => true,
                    'charoff' => true,
                    'valign' => true,
                ],
                'th' => [
                    'abbr' => true,
                    'align' => true,
                    'axis' => true,
                    'bgcolor' => true,
                    'char' => true,
                    'charoff' => true,
                    'colspan' => true,
                    'headers' => true,
                    'height' => true,
                    'nowrap' => true,
                    'rowspan' => true,
                    'scope' => true,
                    'valign' => true,
                    'width' => true,
                ],
                'thead' => [
                    'align' => true,
                    'char' => true,
                    'charoff' => true,
                    'valign' => true,
                ],
                'tr' => [
                    'align' => true,
                    'bgcolor' => true,
                    'char' => true,
                    'charoff' => true,
                    'valign' => true,
                ],
                'video' => [
                    'autoplay' => true,
                    'controls' => true,
                    'height' => true,
                    'loop' => true,
                    'muted' => true,
                    'playsinline' => true,
                    'poster' => true,
                    'preload' => true,
                    'src' => true,
                    'width' => true,
                ],
                'audio' => [
                    'autoplay' => true,
                    'controls' => true,
                    'loop' => true,
                    'muted' => true,
                    'preload' => true,
                    'src' => true,
                ],
            ];
        }

    }
}