<?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\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Image_Size;

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

/**
 * Promo Box Widget
 *
 * @since 1.0.0
 */
class Promo_Box extends Widget_Base
{
    use Widget_helper, UnlockafeUtils;

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

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

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

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

    /**
     * Retrieve the list of keywords.
     */
    public function get_keywords()
    {
        return ['promo', 'box', 'banner', 'promotion', 'offer', 'discount', 'advertisement'];
    }

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

    /**
     * Retrieve the list of styles.
     */
    public function get_style_depends()
    {
        return ['unlockafe-promo-box-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(
            'promo_layout',
            [
                'label' => __('Layout', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'layout-1' => __('Layout 1 - Image Left', 'unlock-addons-for-elementor'),
                    'layout-2' => __('Layout 2 - Image Right', 'unlock-addons-for-elementor'),
                    'layout-3' => __('Layout 3 - Image Top', 'unlock-addons-for-elementor'),
                    'layout-4' => __('Layout 4 - Image Background', 'unlock-addons-for-elementor'),
                    'layout-5' => __('Layout 5 - Overlay Content', 'unlock-addons-for-elementor'),
                    'layout-6' => __('Layout 6 - Split Design', 'unlock-addons-for-elementor'),
                ],
                'default' => 'layout-1',
            ]
        );

        $this->add_control(
            'content_alignment',
            [
                'label' => __('Content Alignment', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'left' => __('Left', 'unlock-addons-for-elementor'),
                    'center' => __('Center', 'unlock-addons-for-elementor'),
                    'right' => __('Right', 'unlock-addons-for-elementor'),
                ],
                'default' => 'left',
            ]
        );

        $this->end_controls_section();

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

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

        $this->add_control(
            'promo_badge',
            [
                'label' => __('Badge Text', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => __('New', 'unlock-addons-for-elementor'),
                'placeholder' => __('e.g., Sale, New, Limited', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'promo_title',
            [
                'label' => __('Title', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => __('Special Promotion', 'unlock-addons-for-elementor'),
                'placeholder' => __('Enter title', 'unlock-addons-for-elementor'),
                'dynamic' => [
                    'active' => true,
                ],
            ]
        );

        $this->add_control(
            'promo_subtitle',
            [
                'label' => __('Subtitle', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => __('Limited Time Offer', 'unlock-addons-for-elementor'),
                'placeholder' => __('Enter subtitle', 'unlock-addons-for-elementor'),
                'dynamic' => [
                    'active' => true,
                ],
            ]
        );

        $this->add_control(
            'promo_description',
            [
                'label' => __('Description', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXTAREA,
                'default' => __('Get amazing deals and discounts on our premium products. Don\'t miss out!', 'unlock-addons-for-elementor'),
                'placeholder' => __('Enter description', 'unlock-addons-for-elementor'),
                'rows' => 4,
                'dynamic' => [
                    'active' => true,
                ],
            ]
        );

        $this->add_control(
            'promo_button_text',
            [
                'label' => __('Button Text', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => __('Shop Now', 'unlock-addons-for-elementor'),
                'placeholder' => __('Enter button text', 'unlock-addons-for-elementor'),
            ]
        );

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

        $this->add_control(
            'promo_discount',
            [
                'label' => __('Discount/Offer Text', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => __('50% OFF', 'unlock-addons-for-elementor'),
                'placeholder' => __('e.g., 50% OFF, Buy 1 Get 1', 'unlock-addons-for-elementor'),
            ]
        );

        $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_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'container_background',
                'label' => __('Background', 'unlock-addons-for-elementor'),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .unlockafe-promo-box-wrapper',
            ]
        );

        $this->add_responsive_control(
            'container_padding',
            [
                'label' => __('Padding', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-box-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'container_border_radius',
            [
                'label' => __('Border Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-box-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'container_box_shadow',
                'label' => __('Box Shadow', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlockafe-promo-box-wrapper',
            ]
        );

        $this->end_controls_section();

        // Badge Style
        $this->start_controls_section(
            'badge_style_section',
            [
                'label' => __('Badge', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'badge_background',
            [
                'label' => __('Background Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ef4444',
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-badge' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'badge_color',
            [
                'label' => __('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#ffffff',
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-badge' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'badge_typography',
                'selector' => '{{WRAPPER}} .unlockafe-promo-badge',
            ]
        );

        $this->end_controls_section();

        // Title Style
        $this->start_controls_section(
            'title_style_section',
            [
                'label' => __('Title', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => __('Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-title' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'selector' => '{{WRAPPER}} .unlockafe-promo-title',
            ]
        );

        $this->end_controls_section();

        // Subtitle Style
        $this->start_controls_section(
            'subtitle_style_section',
            [
                'label' => __('Subtitle', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'subtitle_color',
            [
                'label' => __('Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-subtitle' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'subtitle_typography',
                'selector' => '{{WRAPPER}} .unlockafe-promo-subtitle',
            ]
        );

        $this->end_controls_section();

        // Description Style
        $this->start_controls_section(
            'description_style_section',
            [
                'label' => __('Description', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'description_color',
            [
                'label' => __('Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-description' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'description_typography',
                'selector' => '{{WRAPPER}} .unlockafe-promo-description',
            ]
        );

        $this->end_controls_section();

        // Button Style
        $this->start_controls_section(
            'button_style_section',
            [
                'label' => __('Button', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'button_background',
            [
                'label' => __('Background Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-button' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'button_color',
            [
                'label' => __('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-button' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'button_typography',
                'selector' => '{{WRAPPER}} .unlockafe-promo-button',
            ]
        );

        $this->add_responsive_control(
            'button_padding',
            [
                'label' => __('Padding', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'button_border_radius',
            [
                'label' => __('Border Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Discount Style
        $this->start_controls_section(
            'discount_style_section',
            [
                'label' => __('Discount/Offer', 'unlock-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'discount_background',
            [
                'label' => __('Background Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#667eea',
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-discount' => 'background-color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->add_control(
            'discount_color',
            [
                'label' => __('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlockafe-promo-discount' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'discount_typography',
                'selector' => '{{WRAPPER}} .unlockafe-promo-discount',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Render widget output on the frontend.
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $layout = $settings['promo_layout'] ?? 'layout-1';
        $content_alignment = $settings['content_alignment'] ?? 'left';
        $promo_image = !empty($settings['promo_image']['url']) ? esc_url($settings['promo_image']['url']) : '';
        $promo_badge = !empty($settings['promo_badge']) ? esc_html($settings['promo_badge']) : '';
        $promo_title = !empty($settings['promo_title']) ? esc_html($settings['promo_title']) : '';
        $promo_subtitle = !empty($settings['promo_subtitle']) ? esc_html($settings['promo_subtitle']) : '';
        $promo_description = !empty($settings['promo_description']) ? esc_html($settings['promo_description']) : '';
        $promo_button_text = !empty($settings['promo_button_text']) ? esc_html($settings['promo_button_text']) : '';
        $promo_button_link = !empty($settings['promo_button_link']['url']) ? esc_url($settings['promo_button_link']['url']) : '';
        $button_target = !empty($settings['promo_button_link']['is_external']) ? '_blank' : '_self';
        $button_nofollow = !empty($settings['promo_button_link']['nofollow']) ? 'nofollow' : '';
        $promo_discount = !empty($settings['promo_discount']) ? esc_html($settings['promo_discount']) : '';

        $wrapper_class = 'unlockafe-promo-box-wrapper';
        $wrapper_class .= ' unlockafe-promo-' . esc_attr($layout);
        $wrapper_class .= ' unlockafe-promo-align-' . esc_attr($content_alignment);

        ?>
        <div class="<?php echo esc_attr($wrapper_class); ?>">
            <?php if ($promo_badge && ($layout === 'layout-4' || $layout === 'layout-5')): ?>
                <span class="unlockafe-promo-badge"><?php echo $promo_badge; ?></span>
            <?php endif; ?>

            <?php if ($promo_image && ($layout !== 'layout-4' && $layout !== 'layout-5')): ?>
                <div class="unlockafe-promo-image-wrapper">
                    <?php if ($promo_badge && ($layout === 'layout-1' || $layout === 'layout-2' || $layout === 'layout-3')): ?>
                        <span class="unlockafe-promo-badge"><?php echo $promo_badge; ?></span>
                    <?php endif; ?>
                    <img src="<?php echo $promo_image; ?>" alt="<?php echo esc_attr($promo_title); ?>" class="unlockafe-promo-image" loading="lazy">
                </div>
            <?php endif; ?>

            <?php if ($layout === 'layout-4' || $layout === 'layout-5'): ?>
                <div class="unlockafe-promo-background-image" style="background-image: url('<?php echo $promo_image; ?>');"></div>
            <?php endif; ?>

            <div class="unlockafe-promo-content">
                <?php if ($promo_subtitle): ?>
                    <span class="unlockafe-promo-subtitle"><?php echo $promo_subtitle; ?></span>
                <?php endif; ?>

                <?php if ($promo_title): ?>
                    <h2 class="unlockafe-promo-title"><?php echo $promo_title; ?></h2>
                <?php endif; ?>

                <?php if ($promo_description): ?>
                    <p class="unlockafe-promo-description"><?php echo $promo_description; ?></p>
                <?php endif; ?>

                <?php if ($promo_discount): ?>
                    <div class="unlockafe-promo-discount"><?php echo $promo_discount; ?></div>
                <?php endif; ?>

                <?php if ($promo_button_text && $promo_button_link): ?>
                    <a href="<?php echo $promo_button_link; ?>" target="<?php echo esc_attr($button_target); ?>" rel="<?php echo esc_attr($button_nofollow); ?>" class="unlockafe-promo-button">
                        <?php echo $promo_button_text; ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
}


