<?php

namespace PWH_DCFH\App\Frontend\DiviContactForm\Features;

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

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

        /**
         * Render Output
         * @throws DOMException
         */
        public function output()
        {
            $dom = PWH_DCFH_DOM::dom($this->output);
            $input = $dom->getElementsByTagName('input');
            $p_tag = $dom->getElementsByTagName('p');
            if (isset($input->length, $p_tag->length) && 0 !== $input->length && 0 !== $p_tag->length) {
                $field_id = isset($this->props['field_id']) ? sanitize_key($this->props['field_id']) : 0;
                $p_item = $p_tag->item(0);
                if ($p_item !== null) {
                    $p_item = $p_tag->item(0);
                    $p_item->setAttribute('class', $p_item->getAttribute('class').' et_pb_rating_container');
                    $ratings = [
                        ['value' => '5.0', 'title' => __('5 Stars', 'pwh-dcfh')],
                        ['value' => '4.5', 'title' => __('4.5 Stars', 'pwh-dcfh'), 'class' => 'half'],
                        ['value' => '4.0', 'title' => __('4 Stars', 'pwh-dcfh')],
                        ['value' => '3.5', 'title' => __('3.5 Stars', 'pwh-dcfh'), 'class' => 'half'],
                        ['value' => '3.0', 'title' => __('3 Stars', 'pwh-dcfh')],
                        ['value' => '2.5', 'title' => __('2.5 Stars', 'pwh-dcfh'), 'class' => 'half'],
                        ['value' => '2.0', 'title' => __('2 Stars', 'pwh-dcfh')],
                        ['value' => '1.5', 'title' => __('1.5 Stars', 'pwh-dcfh'), 'class' => 'half'],
                        ['value' => '1.0', 'title' => __('1 Star', 'pwh-dcfh')],
                        ['value' => '0.5', 'title' => __('0.5 Stars', 'pwh-dcfh'), 'class' => 'half']
                    ];
                    foreach ($ratings as $index => $rating) {
                        $id = "{$field_id}_".(10 - $index);
                        // Create input element
                        $rating_input = $dom->createElement('input');
                        $rating_input->setAttribute('type', 'radio');
                        $rating_input->setAttribute('id', $id);
                        $rating_input->setAttribute('name', "rating_$field_id");
                        $rating_input->setAttribute('value', $rating['value']);
                        $p_item->appendChild($rating_input);
                        // Create label element
                        $rating_label = $dom->createElement('label');
                        $rating_label->setAttribute('for', $id);
                        $rating_label->setAttribute('title', $rating['title']);
                        if (isset($rating['class'])) {
                            $rating_label->setAttribute('class', $rating['class']);
                        }
                        // Create and append SVG elements
                        $svg_node1 = $dom->createElement('svg');
                        $svg_node1->setAttribute('viewBox', '0 0 24 24');
                        $path_node1 = $dom->createElement('path');
                        $path_node1->setAttribute('d', 'M12 .587l3.668 7.431L24 9.748l-6 5.84 1.421 8.287L12 18.902l-7.421 3.973L6 15.588.004 9.748 8.332 8.02z');
                        $svg_node1->appendChild($path_node1);
                        $rating_label->appendChild($svg_node1);
                        if (isset($rating['class'])) {
                            // If it's a half star, create and append another SVG
                            $svg_node2 = $dom->createElement('svg');
                            $svg_node2->setAttribute('viewBox', '0 0 24 24');
                            $path_node2 = $dom->createElement('path');
                            $path_node2->setAttribute('d', 'M12 .587l3.668 7.431L24 9.748l-6 5.84 1.421 8.287L12 18.902l-7.421 3.973L6 15.588.004 9.748 8.332 8.02z');
                            $svg_node2->appendChild($path_node2);
                            $rating_label->appendChild($svg_node2);
                        }
                        $p_item->appendChild($rating_label);
                    }
                    $this->output = $dom->saveHTML();
                }

            }

            return $this->output;

        }

        /**
         * Render Css
         * @return void
         */
        public function css()
        {
            $selector_full = '%%order_class%%.et_pb_rating_container > label svg';
            $selector_half = '%%order_class%%.et_pb_rating_container .half svg';
            $d_color = et_pb_responsive_options()->get_property_values($this->props, 'star_color');
            et_pb_responsive_options()->generate_responsive_css($d_color, "$selector_full,$selector_half", 'fill', $this->render_slug, '', 'color');
            $d_checked_color = et_pb_responsive_options()->get_property_values($this->props, 'star_checked_color');
            et_pb_responsive_options()->generate_responsive_css($d_checked_color, "
            %%order_class%%.et_pb_rating_container input:checked ~ label svg,
            %%order_class%%.et_pb_rating_container label:hover svg,
            %%order_class%%.et_pb_rating_container label:hover ~ label svg,
            %%order_class%%.et_pb_rating_container input:checked ~ .half svg,
            %%order_class%%.et_pb_rating_container label:hover ~ .half svg,
            %%order_class%%.et_pb_rating_container input:checked ~ label:hover ~ .half svg", 'fill', $this->render_slug, '', 'color');
            et_pb_responsive_options()->generate_responsive_css($d_checked_color, "
            %%order_class%%.et_pb_rating_container input:checked + label:hover svg,
            %%order_class%%.et_pb_rating_container input:checked ~ label:hover svg,
            %%order_class%%.et_pb_rating_container input:checked ~ label:hover ~ label svg,
            %%order_class%%.et_pb_rating_container label:hover ~ input:checked ~ label svg,.et_pb_rating_container input:checked + label:hover ~ .half svg,
            %%order_class%%.et_pb_rating_container input:checked ~ label:hover ~ .half svg,
            %%order_class%%.et_pb_rating_container input:checked ~ label:hover ~ label ~ .half svg,
            %%order_class%%.et_pb_rating_container label:hover ~ input:checked ~ .half svg", 'fill', $this->render_slug, '', 'color');
            $d_width = et_pb_responsive_options()->get_property_values($this->props, 'star_svg_width');
            et_pb_responsive_options()->generate_responsive_css($d_width, "$selector_full,$selector_half", 'width', $this->render_slug);
            $d_height = et_pb_responsive_options()->get_property_values($this->props, 'star_svg_height');
            et_pb_responsive_options()->generate_responsive_css($d_height, "$selector_full,$selector_half", 'height', $this->render_slug);
        }
    }
}