<?php

namespace Unlockafe_Addons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Icons_Manager;
use Elementor\Repeater;

if (!defined('ABSPATH')) exit; // Exit if accessed directly

/**
 * Elementor Circle Menu Widget.
 *
 * @since 1.0.0
 */
class Circle_Menu extends Widget_Base
{

    public function get_name()
    {
        return 'unlockafe-circle-menu';
    }

    public function get_title()
    {
        return esc_html__('Circle Menu', 'unlock-addons-for-elementor');
    }

    public function get_icon()
    {
        return 'eicon-menu-bar ulicon';
    }

    public function get_categories()
    {
        return ['unlockafe-addons'];
    }

    public function get_keywords()
    {
        return ['unlock', 'circle', 'menu', 'icon', 'list', 'fab', 'floating'];
    }

    public function get_script_depends()
    {
        return ['unlockafe-circle-menu-script'];
    }

    public function get_style_depends()
    {
        return ['unlockafe-circle-menu-style'];
    }

    protected function register_controls()
    {
        //============================================
        // Content Tab
        //============================================

        $this->start_controls_section('layout_section', [
            'label' => esc_html__('Layout', 'unlock-addons-for-elementor'),
        ]);

        $this->add_control('layout_style', [
            'label'   => esc_html__('Style Preset', 'unlock-addons-for-elementor'),
            'type'    => Controls_Manager::SELECT,
            'default' => 'menu-rectangle', // Default changed to match first example
            'options' => [
                'menu-rectangle' => esc_html__('Rectangle', 'unlock-addons-for-elementor'),
                'menu-radius'    => esc_html__('Radius', 'unlock-addons-for-elementor'),
                'menu-circle'    => esc_html__('Circle', 'unlock-addons-for-elementor'),
            ],
        ]);

        $this->end_controls_section();

        $this->start_controls_section('fab_section', [
            'label' => esc_html__('Main Button', 'unlock-addons-for-elementor'),
            'tab'   => Controls_Manager::TAB_CONTENT,
        ]);

        $this->add_control('fab_menu_icon', [
            'label'   => esc_html__('Menu Icon', 'unlock-addons-for-elementor'),
            'type'    => Controls_Manager::ICONS,
            'default' => [
                'value'   => 'fas fa-bars',
                'library' => 'fa-solid',
            ],
        ]);

        $this->add_control('fab_close_icon', [
            'label'   => esc_html__('Close Icon', 'unlock-addons-for-elementor'),
            'type'    => Controls_Manager::ICONS,
            'default' => [
                'value'   => 'fas fa-times',
                'library' => 'fa-solid',
            ],
        ]);

        $this->end_controls_section();

        $this->start_controls_section('menu_items_section', [
            'label' => esc_html__('Menu Items', 'unlock-addons-for-elementor'),
            'tab'   => Controls_Manager::TAB_CONTENT,
        ]);

        $repeater = new Repeater();

        $repeater->add_control('item_icon', [
            'label'   => esc_html__('Icon', 'unlock-addons-for-elementor'),
            'type'    => Controls_Manager::ICONS,
            'default' => [
                'value'   => 'fas fa-home',
                'library' => 'fa-solid',
            ],
        ]);

        $repeater->add_control('item_link', [
            'label'         => esc_html__('Link', 'unlock-addons-for-elementor'),
            'type'          => Controls_Manager::URL,
            'placeholder'   => esc_html__('https://your-link.com', 'unlock-addons-for-elementor'),
            'show_external' => true,
            'default'       => [
                'url'         => '#',
                'is_external' => false,
                'nofollow'    => false,
            ],
        ]);

        $this->add_control('menu_items', [
            'label'       => esc_html__('Items', 'unlock-addons-for-elementor'),
            'type'        => Controls_Manager::REPEATER,
            'fields'      => $repeater->get_controls(),
            'default'     => [
                ['item_icon' => ['value' => 'fas fa-home', 'library' => 'fa-solid']],
                ['item_icon' => ['value' => 'fas fa-box', 'library' => 'fa-solid']],
                ['item_icon' => ['value' => 'fas fa-truck', 'library' => 'fa-solid']],
                ['item_icon' => ['value' => 'fas fa-user', 'library' => 'fa-solid']],
            ],
            'title_field' => '{{{ elementor.helpers.renderIcon( this, item_icon, {}, "i", "panel" ) || "" }}}',
        ]);

        $this->end_controls_section();

        //============================================
        // Style Tab
        //============================================

        $this->start_controls_section('main_button_style_section', [
            'label' => esc_html__('Main Button', 'unlock-addons-for-elementor'),
            'tab'   => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_responsive_control('main_button_size', [
            'label'      => esc_html__('Size (px)', 'unlock-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => ['px' => ['min' => 40, 'max' => 100, 'step' => 1]],
            'default'    => ['unit' => 'px', 'size' => 60],
            'selectors'  => [
                '{{WRAPPER}} .fab' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control('main_button_icon_size', [
            'label'      => esc_html__('Icon Size (px)', 'unlock-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => ['px' => ['min' => 10, 'max' => 50, 'step' => 1]],
            'default'    => ['unit' => 'px', 'size' => 24],
            'selectors'  => [
                '{{WRAPPER}} .fab i'   => 'font-size: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .fab svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->start_controls_tabs('main_button_style_tabs');

        // Normal Tab
        $this->start_controls_tab('main_button_normal_tab', ['label' => esc_html__('Normal', 'unlock-addons-for-elementor')]);
        $this->add_control('main_button_bg_color', ['label' => esc_html__('Background Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .fab' => 'background-color: {{VALUE}};']]);
        $this->add_control('main_button_icon_color', ['label' => esc_html__('Icon Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'default' => '#ffffff', 'selectors' => ['{{WRAPPER}} .fab i' => 'color: {{VALUE}};', '{{WRAPPER}} .fab svg' => 'fill: {{VALUE}};']]);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'main_button_shadow', 'selector' => '{{WRAPPER}} .fab']);
        $this->end_controls_tab();

        // Hover Tab
        $this->start_controls_tab('main_button_hover_tab', ['label' => esc_html__('Hover', 'unlock-addons-for-elementor')]);
        $this->add_control('main_button_hover_bg_color', ['label' => esc_html__('Background Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .fab:hover' => 'background-color: {{VALUE}};']]);
        $this->add_control('main_button_hover_icon_color', ['label' => esc_html__('Icon Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .fab:hover i' => 'color: {{VALUE}};', '{{WRAPPER}} .fab:hover svg' => 'fill: {{VALUE}};']]);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'main_button_hover_shadow', 'selector' => '{{WRAPPER}} .fab:hover']);
        $this->end_controls_tab();

        // Active Tab
        $this->start_controls_tab('main_button_active_tab', ['label' => esc_html__('Active', 'unlock-addons-for-elementor')]);
        $this->add_control('main_button_active_bg_color', ['label' => esc_html__('Background Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .floating-menu-container.active .fab' => 'background-color: {{VALUE}};']]);
        $this->add_control('main_button_active_icon_color', ['label' => esc_html__('Icon Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .floating-menu-container.active .fab i' => 'color: {{VALUE}};', '{{WRAPPER}} .floating-menu-container.active .fab svg' => 'fill: {{VALUE}};']]);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'main_button_active_shadow', 'selector' => '{{WRAPPER}} .floating-menu-container.active .fab']);
        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->end_controls_section();

        $this->start_controls_section('item_style_section', [
            'label' => esc_html__('Menu Items', 'unlock-addons-for-elementor'),
            'tab'   => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_responsive_control('item_size', [
            'label'      => esc_html__('Size (px)', 'unlock-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => ['px' => ['min' => 30, 'max' => 80, 'step' => 1]],
            'default'    => ['unit' => 'px', 'size' => 50],
            'selectors'  => [
                '{{WRAPPER}} .menu-items a' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control('item_icon_size', [
            'label'      => esc_html__('Icon Size (px)', 'unlock-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => ['px' => ['min' => 10, 'max' => 40, 'step' => 1]],
            'default'    => ['unit' => 'px', 'size' => 20],
            'selectors'  => [
                '{{WRAPPER}} .menu-items a i' => 'font-size: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .menu-items a svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};'
            ],
        ]);

        $this->start_controls_tabs('item_style_tabs');

        $this->start_controls_tab('item_normal_tab', ['label' => esc_html__('Normal', 'unlock-addons-for-elementor')]);
        $this->add_control('item_bg_color', ['label' => esc_html__('Background Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .menu-items a' => 'background-color: {{VALUE}};']]);
        $this->add_control('item_icon_color', ['label' => esc_html__('Icon Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'default' => '#ffffff', 'selectors' => ['{{WRAPPER}} .menu-items a i' => 'color: {{VALUE}};', '{{WRAPPER}} .menu-items a svg' => 'fill: {{VALUE}};']]);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'item_shadow', 'selector' => '{{WRAPPER}} .menu-items a']);
        $this->end_controls_tab();

        $this->start_controls_tab('item_hover_tab', ['label' => esc_html__('Hover', 'unlock-addons-for-elementor')]);
        $this->add_control('item_hover_bg_color', ['label' => esc_html__('Background Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .menu-items a:hover' => 'background-color: {{VALUE}};']]);
        $this->add_control('item_hover_icon_color', ['label' => esc_html__('Icon Color', 'unlock-addons-for-elementor'), 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .menu-items a:hover i' => 'color: {{VALUE}};', '{{WRAPPER}} .menu-items a:hover svg' => 'fill: {{VALUE}};']]);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'item_hover_shadow', 'selector' => '{{WRAPPER}} .menu-items a:hover']);
        $this->end_controls_tab();

        $this->end_controls_tabs();
        $this->end_controls_section();
    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $layout_style = $settings['layout_style'];

        $this->add_render_attribute('wrapper', 'class', 'unlockafe-circle-menu-wrapper');

        $this->add_render_attribute('container', 'class', [
            'floating-menu-container',
            esc_attr($layout_style)
        ]);

        $this->add_render_attribute('fab', 'class', 'fab');

        ?>
        <div <?php echo $this->get_render_attribute_string('wrapper'); ?>>
            <div <?php echo $this->get_render_attribute_string('container'); ?>>
                <button <?php echo $this->get_render_attribute_string('fab'); ?>>
                    <span class="icon-menu">
                        <?php Icons_Manager::render_icon($settings['fab_menu_icon'], ['aria-hidden' => 'true']); ?>
                    </span>
                    <span class="icon-close">
                        <?php Icons_Manager::render_icon($settings['fab_close_icon'], ['aria-hidden' => 'true']); ?>
                    </span>
                </button>

                <?php if ($settings['menu_items']) : ?>
                    <ul class="menu-items">
                        <?php
                        foreach ($settings['menu_items'] as $index => $item) {
                            $repeater_setting_key = $this->get_repeater_setting_key('item_link', 'menu_items', $index);
                            $this->add_link_attributes($repeater_setting_key, $item['item_link']);
                            $item_class = 'elementor-repeater-item-' . $item['_id'];
                            ?>
                            <li class="<?php echo esc_attr($item_class); ?>">
                                <a <?php echo $this->get_render_attribute_string($repeater_setting_key); ?>>
                                    <?php Icons_Manager::render_icon($item['item_icon'], ['aria-hidden' => 'true']); ?>
                                </a>
                            </li>
                        <?php } ?>
                    </ul>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
}