<?php

namespace PWH_DCFH\App\Frontend\DiviContactForm\Features;

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

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

        /**
         * Add Wait Text While File Upload
         * @return false|string
         */
        public function add_wait_text_attr()
        {
            $dom = PWH_DCFH_DOM::dom($this->output);
            $dom_xpath = new DOMXPath($dom);
            $button = $dom_xpath->query('//button[contains(@class,"et_contact_bottom_container")]');
            if ((isset($button->length) && 0 !== $button->length)) {
                $button_wait_text = $this->props['submit_button_wait_text'];
                $button_item = $button->item(0);
                $button_item->setAttribute('data-wait-text', $button_wait_text);
                $this->output = $dom->saveHTML();
            }

            return $this->output;
        }

        /**
         * Render Css
         * @return void
         */
        public function css()
        {
            // Alignment
            $button_fullwidth = isset($this->props['submit_button_full_width']) ? $this->props['submit_button_full_width'] : 'off';
            if ('off' === $button_fullwidth) {
                $is_used_captcha = isset($this->props['captcha']) ? $this->props['captcha'] : 'off';
                $submit_button_alignment = isset($this->props['submit_button_alignment']) ? $this->props['submit_button_alignment'] : 'right';
                $submit_button_alignment_tablet = isset($this->props['submit_button_alignment_tablet']) ? $this->props['submit_button_alignment_tablet'] : $submit_button_alignment;
                $submit_button_alignment_phone = isset($this->props['submit_button_alignment_phone']) ? $this->props['submit_button_alignment_phone'] : $submit_button_alignment_tablet;
                if ('on' === $is_used_captcha) {
                    if ('left' === $submit_button_alignment) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'position: absolute;left: 0 !important;margin: 0 !important;',
                            'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                        ]);
                    }
                    if ('center' === $submit_button_alignment) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'position: absolute;margin: 0 !important;left: 50%;transform: translate(-50%, 0);',
                            'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                        ]);
                    }
                    if ('left' === $submit_button_alignment_tablet) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'position: absolute;left: 0 !important;margin: 0 !important;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                        ]);
                    }
                    if ('center' === $submit_button_alignment_tablet) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'position: absolute;margin: 0 !important;left: 50%;transform: translate(-50%, 0);',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                        ]);
                    }
                    if ('left' === $submit_button_alignment_phone) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'position: absolute;left: 0 !important;margin: 0 !important;transform: translate(0,0);',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                        ]);
                    }
                    if ('center' === $submit_button_alignment_phone) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'position: absolute;margin: 0 !important;left: 50%;transform:translate(-50%, 0);',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                        ]);
                    }
                } else {

                    if ('left' === $submit_button_alignment) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'margin: 0 0 0 30px;',
                            'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_contact_bottom_container',
                            'declaration' => 'justify-content:flex-start;',
                            'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                        ]);
                    }
                    if ('center' === $submit_button_alignment) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'margin: 0;',
                            'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_contact_bottom_container',
                            'declaration' => 'justify-content:center;',
                            'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                        ]);
                    }
                    if ('left' === $submit_button_alignment_tablet) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'margin: 0 0 0 18px;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_contact_bottom_container',
                            'declaration' => 'justify-content:flex-start;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                        ]);
                    }
                    if ('center' === $submit_button_alignment_tablet) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'margin: 0;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_contact_bottom_container',
                            'declaration' => 'justify-content:center;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                        ]);
                    }
                    if ('left' === $submit_button_alignment_phone) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'margin: 0 0 0 12px;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_contact_bottom_container',
                            'declaration' => 'justify-content:flex-start;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                        ]);
                    }
                    if ('center' === $submit_button_alignment_phone) {
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_pb_contact_submit',
                            'declaration' => 'margin: 0;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                        ]);
                        ET_Builder_Element::set_style($this->render_slug, [
                            'selector' => '%%order_class%% .et_contact_bottom_container',
                            'declaration' => 'justify-content:center;',
                            'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                        ]);
                    }
                }
            }
            $is_fullwidth_responsive = et_pb_responsive_options()->is_responsive_enabled($this->props, 'submit_button_full_width');
            $button_fullwidth_tablet = isset($this->props['submit_button_full_width_tablet']) ? $this->props['submit_button_full_width_tablet'] : $button_fullwidth;
            $button_fullwidth_phone = isset($this->props['submit_button_full_width_phone']) ? $this->props['submit_button_full_width_phone'] : $button_fullwidth;
            if ('on' === $button_fullwidth) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_submit',
                    'declaration' => 'margin: 0 !important;',
                    'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                ]);
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_contact_bottom_container',
                    'declaration' => 'flex-direction: column; gap: 1em;padding-left: 3%;',
                    'media_query' => ET_Builder_Element::get_media_query('min_width_981'),
                ]);
            }
            if ($is_fullwidth_responsive && 'on' === $button_fullwidth_tablet) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_submit',
                    'declaration' => 'margin: 0 !important;',
                    'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                ]);
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_contact_bottom_container',
                    'declaration' => 'flex-direction: column; gap: 1em;padding-left: 3%;',
                    'media_query' => ET_Builder_Element::get_media_query('max_width_980'),
                ]);
            }
            if ($is_fullwidth_responsive && 'on' === $button_fullwidth_phone) {
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_pb_contact_submit',
                    'declaration' => 'margin: 0 !important;',
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
                ET_Builder_Element::set_style($this->render_slug, [
                    'selector' => '%%order_class%% .et_contact_bottom_container',
                    'declaration' => 'flex-direction: column; gap: 1em;padding-left: 3%;',
                    'media_query' => ET_Builder_Element::get_media_query('max_width_767'),
                ]);
            }
        }
    }
}