<?php

namespace PWH_DCFH\App\Frontend\DiviContactForm\Features;

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

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

        public $output;

        public $props = [];

        public $contact_form_order_class;

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

        /**
         * Render Ouput
         * @throws DOMException
         */
        public function output()
        {
            $success_button_text = $this->props['success_button_text'];
            $success_button_type = isset($this->props['success_button_type']) ? $this->props['success_button_type'] : 'text';
            $success_button_url = isset($this->props['success_button_url']) ? $this->props['success_button_url'] : null;
            $success_button_page = isset($this->props['success_button_page']) ? $this->props['success_button_page'] : null;
            $success_button_target = isset($this->props['success_button_target']) ? $this->props['success_button_target'] : '_self';
            $success_button_href = '';
            if ('url' === $success_button_type && !empty($success_button_url)) {
                $success_button_href = $success_button_url;
            } elseif ('page' === $success_button_type && !empty($success_button_page)) {
                $success_button_href = get_the_permalink($success_button_page);
            }
            $dom = PWH_DCFH_DOM::dom($this->output);
            $dom_xpath = new DOMXPath($dom);
            // $form = $dom_xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), '$this->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);
                // $form = $dom->getElementById($this->contact_form_order_class);
                $div_wrapper = $dom->createElement('div');
                $div_wrapper->setAttribute('class', 'et_pb_success_btn_container');
                $anchor_tag = $dom->createElement('a', $success_button_text);
                $anchor_tag->setAttribute('class', 'et_pb_success_button');
                if ('text' !== $success_button_type && '' !== $success_button_href) {
                    $anchor_tag->setAttribute('href', $success_button_href);
                    $anchor_tag->setAttribute('target', $success_button_target);
                }
                $div_wrapper->appendChild($anchor_tag);
                $form_item->appendChild($div_wrapper);
                $this->output = $dom->saveHTML();
            }

            return $this->output;
        }

        /**
         * Render Css
         * @return void
         */
        public function css()
        {
            $css_selector = '%%order_class%% .et_pb_success_button';
            $is_success_button_font_responsive = et_pb_responsive_options()->is_responsive_enabled($this->props, 'success_button_font');
            $desktop_font = isset($this->props['success_button_font']) ? $this->props['success_button_font'] : null;
            $tablet_font = isset($this->props['success_button_font_tablet']) ? $this->props['success_button_font_tablet'] : null;
            $phone_font = isset($this->props['success_button_font_phone']) ? $this->props['success_button_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 ($is_success_button_font_responsive && !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 ($is_success_button_font_responsive && !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_color = et_pb_responsive_options()->get_property_values($this->props, 'success_button_color');
            et_pb_responsive_options()->generate_responsive_css($d_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, 'success_button_background'));
            et_pb_responsive_options()->generate_responsive_css($d_background, $css_selector, 'background', $this->render_slug, '', 'color');
            $d_size = et_pb_responsive_options()->get_property_values($this->props, 'success_button_size');
            et_pb_responsive_options()->generate_responsive_css($d_size, $css_selector, 'font-size', $this->render_slug);
            $d_letter = et_pb_responsive_options()->get_property_values($this->props, 'success_button_letter_space');
            et_pb_responsive_options()->generate_responsive_css($d_letter, $css_selector, 'letter-spacing', $this->render_slug);
            $line_height_important = et_builder_has_limitation('force_use_global_important') ? ' !important;' : '';
            $d_line_height = et_pb_responsive_options()->get_property_values($this->props, 'success_button_line_height');
            et_pb_responsive_options()->generate_responsive_css($d_line_height, $css_selector, 'line-height', $this->render_slug, $line_height_important);
            $d_alignment = et_pb_responsive_options()->get_property_values($this->props, 'success_button_alignment');
            if (!empty($d_alignment)) {
                $d_alignment = pwh_dcfh_module_helpers()::get_flex_alignment($d_alignment);
            }
            et_pb_responsive_options()->generate_responsive_css($d_alignment, '%%order_class%% .et_pb_success_btn_container', 'justify-content', $this->render_slug, '', 'text_alignment');
            $d_margin = et_pb_responsive_options()->get_property_values($this->props, 'success_button_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, 'success_button_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, 'success_button_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, 'success_button_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, 'success_button_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, 'success_button_border_style');
            et_pb_responsive_options()->generate_responsive_css($d_border_style, $css_selector, 'border-style', $this->render_slug, '', 'select');
            $shadow_preset = isset($this->props['success_button_shadow']) ? $this->props['success_button_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_button_shadow_horizontal']) ? $this->props['success_button_shadow_horizontal'] : $shadow['horizontal'];
                $vertical = isset($this->props['success_button_shadow_vertical']) ? $this->props['success_button_shadow_vertical'] : $shadow['vertical'];
                $blur = isset($this->props['success_button_shadow_blur']) ? $this->props['success_button_shadow_blur'] : $shadow['blur'];
                $spread = isset($this->props['success_button_shadow_spread']) ? $this->props['success_button_shadow_spread'] : $shadow['spread'];
                $color = isset($this->props['success_button_shadow_color']) ? $this->props['success_button_shadow_color'] : 'rgba(0,0,0,0.3)';
                $position = isset($this->props['success_button_shadow_position']) ? $this->props['success_button_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;",
                ]);
            }
        }

    }
}