<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils as UnlockafeUtils;
use Unlockafe_addons\Traits\Widget_helper;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;

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

/**
 * Party Propz Widget
 *
 * @since 1.0.0
 */
class Party_Propz extends Widget_Base
{
    use Widget_helper, UnlockafeUtils;

    /**
     * Retrieve the widget name.
     */
    public function get_name()
    {
        return 'unlockafe-party-propz';
    }

    /**
     * Retrieve the widget title.
     */
    public function get_title()
    {
        return __('Party Propz', 'unlock-addons-for-elementor');
    }

    /**
     * Retrieve the widget icon.
     */
    public function get_icon()
    {
        return 'eicon-star ulicon';
    }

    /**
     * Retrieve the list of categories.
     */
    public function get_categories()
    {
        return ['unlockafe-addons'];
    }

    /**
     * Retrieve the list of keywords.
     */
    public function get_keywords()
    {
        return ['party', 'props', 'decoration', 'animation', 'confetti', 'balloon', 'celebration', 'festive'];
    }

    /**
     * Retrieve the list of scripts.
     */
    public function get_script_depends()
    {
        return ['unlockafe-party-propz-scripts'];
    }

    /**
     * Retrieve the list of styles.
     */
    public function get_style_depends()
    {
        return ['unlockafe-party-propz-style', 'font-awesome'];
    }

    /**
     * Register widget controls.
     */
    protected function register_controls()
    {
        $this->register_content_controls();
        $this->register_style_controls();
    }

    /**
     * Register content controls.
     */
    protected function register_content_controls()
    {
        // General Settings
        $this->start_controls_section(
            'general_section',
            [
                'label' => __('General Settings', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'animation_type',
            [
                'label' => __('Animation Type', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'float' => __('Float', 'unlock-addons-for-elementor'),
                    'rotate' => __('Rotate', 'unlock-addons-for-elementor'),
                    'bounce' => __('Bounce', 'unlock-addons-for-elementor'),
                    'drift' => __('Drift', 'unlock-addons-for-elementor'),
                    'pulse' => __('Pulse', 'unlock-addons-for-elementor'),
                    'swing' => __('Swing', 'unlock-addons-for-elementor'),
                ],
                'default' => 'float',
            ]
        );

        $this->add_control(
            'animation_speed',
            [
                'label' => __('Animation Speed', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 10,
                        'step' => 0.5,
                    ],
                ],
                'default' => [
                    'size' => 3,
                ],
            ]
        );

        $this->add_control(
            'random_delay',
            [
                'label' => __('Random Animation Delay', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'unlock-addons-for-elementor'),
                'label_off' => __('No', 'unlock-addons-for-elementor'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'interactive',
            [
                'label' => __('Interactive (Follow Mouse)', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'unlock-addons-for-elementor'),
                'label_off' => __('No', 'unlock-addons-for-elementor'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'enable_snow',
            [
                'label' => __('Enable Falling Snow', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => __('Yes', 'unlock-addons-for-elementor'),
                'label_off' => __('No', 'unlock-addons-for-elementor'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'snow_count',
            [
                'label' => __('Snow Count', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 10,
                        'max' => 200,
                    ],
                ],
                'default' => [
                    'size' => 50,
                ],
                'condition' => [
                    'enable_snow' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'snow_speed',
            [
                'label' => __('Snow Speed', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 10,
                        'step' => 0.5,
                    ],
                ],
                'default' => [
                    'size' => 3,
                ],
                'condition' => [
                    'enable_snow' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'snow_size',
            [
                'label' => __('Snow Size', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 2,
                        'max' => 20,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 5,
                ],
                'condition' => [
                    'enable_snow' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'snow_color',
            [
                'label' => __('Snow Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#d3d3d3',
                'condition' => [
                    'enable_snow' => 'yes',
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-snowflake' => 'color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->add_control(
            'snow_design',
            [
                'label' => __('Snow Design', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'default' => __('Default Snowflake ❄', 'unlock-addons-for-elementor'),
                    'dot' => __('Dot •', 'unlock-addons-for-elementor'),
                    'circle' => __('Circle ●', 'unlock-addons-for-elementor'),
                    'star' => __('Star ★', 'unlock-addons-for-elementor'),
                    'cross' => __('Cross ✕', 'unlock-addons-for-elementor'),
                    'diamond' => __('Diamond ◆', 'unlock-addons-for-elementor'),
                ],
                'default' => 'default',
                'condition' => [
                    'enable_snow' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'snow_opacity',
            [
                'label' => __('Snow Opacity', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 1,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'size' => 0.7,
                ],
                'condition' => [
                    'enable_snow' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'snow_blur',
            [
                'label' => __('Snow Blur Effect', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 5,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 0.5,
                ],
                'condition' => [
                    'enable_snow' => 'yes',
                ],
            ]
        );

        $this->end_controls_section();

        // Props Section
        $this->start_controls_section(
            'props_section',
            [
                'label' => __('Party Props', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'prop_type',
            [
                'label' => __('Prop Type', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'balloon' => __('Balloon', 'unlock-addons-for-elementor'),
                    'confetti' => __('Confetti', 'unlock-addons-for-elementor'),
                    'star' => __('Star', 'unlock-addons-for-elementor'),
                    'heart' => __('Heart', 'unlock-addons-for-elementor'),
                    'gift' => __('Gift', 'unlock-addons-for-elementor'),
                    'cake' => __('Cake', 'unlock-addons-for-elementor'),
                    'santa-cap' => __('Santa Cap', 'unlock-addons-for-elementor'),
                    'christmas-tree' => __('Christmas Tree', 'unlock-addons-for-elementor'),
                    'snowflake' => __('Snowflake', 'unlock-addons-for-elementor'),
                    'reindeer' => __('Reindeer', 'unlock-addons-for-elementor'),
                    'snow' => __('Snow', 'unlock-addons-for-elementor'),
                    'hanukkah-candle' => __('Hanukkah Candle', 'unlock-addons-for-elementor'),
                    'xmas-border' => __('Xmas Border', 'unlock-addons-for-elementor'),
                    'icon' => __('Custom Icon', 'unlock-addons-for-elementor'),
                    'image' => __('Custom Image', 'unlock-addons-for-elementor'),
                ],
                'default' => 'balloon',
            ]
        );

        $repeater->add_control(
            'prop_icon',
            [
                'label' => __('Icon', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-birthday-cake',
                    'library' => 'fa-solid',
                ],
                'condition' => [
                    'prop_type' => 'icon',
                ],
            ]
        );

        $repeater->add_control(
            'prop_image',
            [
                'label' => __('Image', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => \Elementor\Utils::get_placeholder_image_src(),
                ],
                'condition' => [
                    'prop_type' => 'image',
                ],
            ]
        );

        $repeater->add_control(
            'prop_color',
            [
                'label' => __('Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ff6b6b',
                'condition' => [
                    'prop_type!' => ['icon', 'image'],
                ],
            ]
        );

        $repeater->add_control(
            'prop_size',
            [
                'label' => __('Size', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => [
                    'px' => [
                        'min' => 20,
                        'max' => 200,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 60,
                ],
            ]
        );

        $repeater->add_control(
            'prop_position_x',
            [
                'label' => __('Position X (%)', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 50,
                ],
            ]
        );

        $repeater->add_control(
            'prop_position_y',
            [
                'label' => __('Position Y (%)', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 50,
                ],
            ]
        );

        $repeater->add_control(
            'prop_z_index',
            [
                'label' => __('Z-Index', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::NUMBER,
                'default' => 1,
                'min' => 1,
                'max' => 100,
            ]
        );

        $repeater->add_control(
            'prop_rotation',
            [
                'label' => __('Rotation (degrees)', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'deg' => [
                        'min' => -180,
                        'max' => 180,
                    ],
                ],
                'default' => [
                    'unit' => 'deg',
                    'size' => 0,
                ],
            ]
        );

        $repeater->add_control(
            'prop_opacity',
            [
                'label' => __('Opacity', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 1,
                        'step' => 0.1,
                    ],
                ],
                'default' => [
                    'size' => 1,
                ],
            ]
        );

        $this->add_control(
            'party_props',
            [
                'label' => __('Party Props', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'default' => [
                    [
                        'prop_type' => 'balloon',
                        'prop_color' => '#ff6b6b',
                        'prop_size' => ['size' => 60],
                        'prop_position_x' => ['size' => 20],
                        'prop_position_y' => ['size' => 30],
                    ],
                    [
                        'prop_type' => 'star',
                        'prop_color' => '#ffd93d',
                        'prop_size' => ['size' => 50],
                        'prop_position_x' => ['size' => 80],
                        'prop_position_y' => ['size' => 20],
                    ],
                    [
                        'prop_type' => 'confetti',
                        'prop_color' => '#6bcf7f',
                        'prop_size' => ['size' => 40],
                        'prop_position_x' => ['size' => 50],
                        'prop_position_y' => ['size' => 70],
                    ],
                    [
                        'prop_type' => 'heart',
                        'prop_color' => '#ff6b9d',
                        'prop_size' => ['size' => 45],
                        'prop_position_x' => ['size' => 70],
                        'prop_position_y' => ['size' => 60],
                    ],
                ],
                'title_field' => '{{{ prop_type }}}',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register style controls.
     */
    protected function register_style_controls()
    {
        // Container Style
        $this->start_controls_section(
            'container_style_section',
            [
                'label' => __('Container', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'container_height',
            [
                'label' => __('Container Height', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'vh'],
                'range' => [
                    'px' => [
                        'min' => 200,
                        'max' => 2000,
                    ],
                    'vh' => [
                        'min' => 20,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 500,
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-party-propz-wrapper' => 'height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'container_background',
                'label' => __('Background', 'unlock-addons-for-elementor'),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .unlockafe-party-propz-wrapper',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Render widget output on the frontend.
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $animation_type = $settings['animation_type'] ?? 'float';
        $animation_speed = !empty($settings['animation_speed']['size']) ? $settings['animation_speed']['size'] : 3;
        $random_delay = $settings['random_delay'] === 'yes';
        $interactive = $settings['interactive'] === 'yes';
        $enable_snow = $settings['enable_snow'] === 'yes';
        $snow_count = !empty($settings['snow_count']['size']) ? intval($settings['snow_count']['size']) : 50;
        $snow_speed = !empty($settings['snow_speed']['size']) ? floatval($settings['snow_speed']['size']) : 3;
        $snow_size = !empty($settings['snow_size']['size']) ? intval($settings['snow_size']['size']) : 5;
        $snow_color = !empty($settings['snow_color']) ? esc_attr($settings['snow_color']) : '#d3d3d3';
        $snow_design = !empty($settings['snow_design']) ? esc_attr($settings['snow_design']) : 'default';
        $snow_opacity = !empty($settings['snow_opacity']['size']) ? floatval($settings['snow_opacity']['size']) : 0.7;
        $snow_blur = !empty($settings['snow_blur']['size']) ? floatval($settings['snow_blur']['size']) : 0.5;
        $party_props = !empty($settings['party_props']) ? $settings['party_props'] : [];

        $wrapper_class = 'unlockafe-party-propz-wrapper';
        $wrapper_class .= ' unlockafe-animation-' . esc_attr($animation_type);
        if ($interactive) {
            $wrapper_class .= ' unlockafe-interactive';
        }

        ?>
        <div class="<?php echo esc_attr($wrapper_class); ?>"
             data-animation-type="<?php echo esc_attr($animation_type); ?>"
             data-animation-speed="<?php echo esc_attr($animation_speed); ?>"
             data-random-delay="<?php echo $random_delay ? 'yes' : 'no'; ?>"
             data-interactive="<?php echo $interactive ? 'yes' : 'no'; ?>"
             data-enable-snow="<?php echo $enable_snow ? 'yes' : 'no'; ?>"
             data-snow-count="<?php echo esc_attr($snow_count); ?>"
             data-snow-speed="<?php echo esc_attr($snow_speed); ?>"
             data-snow-size="<?php echo esc_attr($snow_size); ?>"
             data-snow-color="<?php echo esc_attr($snow_color); ?>"
             data-snow-design="<?php echo esc_attr($snow_design); ?>"
             data-snow-opacity="<?php echo esc_attr($snow_opacity); ?>"
             data-snow-blur="<?php echo esc_attr($snow_blur); ?>">
            <?php if (!empty($party_props)): ?>
                <?php foreach ($party_props as $index => $prop): 
                    $prop_type = !empty($prop['prop_type']) ? esc_attr($prop['prop_type']) : 'balloon';
                    $prop_color = !empty($prop['prop_color']) ? esc_attr($prop['prop_color']) : '#ff6b6b';
                    $prop_size = !empty($prop['prop_size']['size']) ? intval($prop['prop_size']['size']) : 60;
                    $prop_size_unit = !empty($prop['prop_size']['unit']) ? esc_attr($prop['prop_size']['unit']) : 'px';
                    $position_x = !empty($prop['prop_position_x']['size']) ? floatval($prop['prop_position_x']['size']) : 50;
                    $position_y = !empty($prop['prop_position_y']['size']) ? floatval($prop['prop_position_y']['size']) : 50;
                    $z_index = !empty($prop['prop_z_index']) ? intval($prop['prop_z_index']) : 1;
                    $prop_icon = !empty($prop['prop_icon']) ? $prop['prop_icon'] : '';
                    $prop_image = !empty($prop['prop_image']['url']) ? esc_url($prop['prop_image']['url']) : '';
                    $prop_rotation = !empty($prop['prop_rotation']['size']) ? floatval($prop['prop_rotation']['size']) : 0;
                    $prop_opacity = !empty($prop['prop_opacity']['size']) ? floatval($prop['prop_opacity']['size']) : 1;
                    
                    $prop_class = 'unlockafe-party-prop';
                    $prop_class .= ' unlockafe-prop-' . $prop_type;
                    $prop_class .= ' unlockafe-prop-' . ($index + 1);
                    
                    $style = 'left: ' . $position_x . '%; top: ' . $position_y . '%; z-index: ' . $z_index . ';';
                    $style .= ' width: ' . $prop_size . $prop_size_unit . '; height: ' . $prop_size . $prop_size_unit . ';';
                    $style .= ' opacity: ' . $prop_opacity . ';';
                    if ($prop_rotation != 0) {
                        $style .= ' transform: rotate(' . $prop_rotation . 'deg);';
                    }
                ?>
                    <div class="<?php echo esc_attr($prop_class); ?>" style="<?php echo esc_attr($style); ?>" data-prop-type="<?php echo esc_attr($prop_type); ?>" data-prop-color="<?php echo esc_attr($prop_color); ?>">
                        <?php if ($prop_type === 'icon' && !empty($prop_icon)): ?>
                            <div class="unlockafe-prop-icon">
                                <?php \Elementor\Icons_Manager::render_icon($prop_icon, ['aria-hidden' => 'true']); ?>
                            </div>
                        <?php elseif ($prop_type === 'image' && !empty($prop_image)): ?>
                            <div class="unlockafe-prop-image">
                                <img src="<?php echo $prop_image; ?>" alt="Party Prop" loading="lazy">
                            </div>
                        <?php else: ?>
                            <div class="unlockafe-prop-shape" style="background-color: <?php echo esc_attr($prop_color); ?>;"></div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
        <?php
    }
}


