<?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_Background;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Border;

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

/**
 * Footer Newsletter Widget
 *
 * @since 1.0.0
 */
class Footer_Newsletter extends Widget_Base
{

    use Widget_helper, UnlockafeUtils;

    /**
     * Retrieve the widget name.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name()
    {
        return 'unlockafe-footer-newsletter';
    }

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('Footer Newsletter', 'unlock-addons-for-elementor');
    }

    /**
     * Retrieve the widget icon.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon()
    {
        return 'eicon-mail ulicon';
    }

    /**
     * Retrieve the list of categories the widget belongs to.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget categories.
     */
    public function get_categories()
    {
        return ['unlockafe-addons'];
    }

    /**
     * Retrieve the list of scripts the widget depended on.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget scripts dependencies.
     */
    public function get_script_depends()
    {
        return ['unlockafe-footer-newsletter-script'];
    }

    /**
     * Retrieve the list of styles the widget depended on.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget styles dependencies.
     */
    public function get_style_depends()
    {
        return ['unlockafe-footer-newsletter-style'];
    }

    /**
     * Register the widget controls.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function register_controls()
    {
        $this->register_controls_section();
        $this->style_tab_content();
    }

    /**
     * Register content controls.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function register_controls_section()
    {
        $this->start_controls_section(
            'wpr_footer_newsletter_section',
            [
                'label' => esc_html__('Content', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'newsletter_title',
            [
                'label' => esc_html__('Title', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Subscribe to Newsletter', 'unlock-addons-for-elementor'),
                'placeholder' => esc_html__('Enter title', 'unlock-addons-for-elementor'),
                'label_block' => true,
            ]
        );

        $this->add_control(
            'newsletter_description',
            [
                'label' => esc_html__('Description', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXTAREA,
                'default' => esc_html__('Get the latest news and updates delivered to your inbox.', 'unlock-addons-for-elementor'),
                'placeholder' => esc_html__('Enter description', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'email_placeholder',
            [
                'label' => esc_html__('Email Placeholder', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Enter your email', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'button_text',
            [
                'label' => esc_html__('Button Text', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Subscribe', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'form_action',
            [
                'label' => esc_html__('Form Action URL', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::URL,
                'placeholder' => esc_html__('https://your-mailchimp-url.com', 'unlock-addons-for-elementor'),
                'description' => esc_html__('Enter your email service provider form action URL (e.g., Mailchimp, ConvertKit)', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'layout_style',
            [
                'label' => esc_html__('Layout Style', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'inline' => esc_html__('Inline', 'unlock-addons-for-elementor'),
                    'stacked' => esc_html__('Stacked', 'unlock-addons-for-elementor'),
                ],
                'default' => 'inline',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register style controls.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function style_tab_content()
    {
        // Container Style
        $this->start_controls_section(
            'unlockafe_footer_newsletter_container_style',
            [
                'label' => esc_html__('Container', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_newsletter_alignment',
            [
                'label' => esc_html__('Alignment', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => esc_html__('Left', 'unlock-addons-for-elementor'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__('Center', 'unlock-addons-for-elementor'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => esc_html__('Right', 'unlock-addons-for-elementor'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'left',
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter' => 'text-align: {{VALUE}};',
                ],
            ]
        );

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

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_background',
                'label' => esc_html__('Background', 'unlock-addons-for-elementor'),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter',
            ]
        );

        $this->end_controls_section();

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

        $this->add_control(
            'unlockafe_footer_newsletter_title_color',
            [
                'label' => esc_html__('Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_title' => 'color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_title_typography',
                'label' => esc_html__('Typography', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_title',
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_newsletter_title_spacing',
            [
                'label' => esc_html__('Spacing', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

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

        $this->add_control(
            'unlockafe_footer_newsletter_description_color',
            [
                'label' => esc_html__('Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_description' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_description_typography',
                'label' => esc_html__('Typography', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_description',
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_newsletter_description_spacing',
            [
                'label' => esc_html__('Spacing', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_description' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

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

        $this->add_responsive_control(
            'unlockafe_footer_newsletter_form_gap',
            [
                'label' => esc_html__('Gap', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 30,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 10,
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_form.unlockafe-newsletter-stacked .unlock_addon_footer_newsletter_input' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_form.unlockafe-newsletter-inline .unlock_addon_footer_newsletter_input' => 'margin-right: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

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

        $this->add_control(
            'unlockafe_footer_newsletter_input_color',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_input' => 'color: {{VALUE}};',
                ],
            ]
        );

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

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_input_typography',
                'label' => esc_html__('Typography', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_input',
            ]
        );

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

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_input_border',
                'label' => esc_html__('Border', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_input',
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_newsletter_input_border_radius',
            [
                'label' => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_input' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

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

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_button_typography',
                'label' => esc_html__('Typography', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_button',
            ]
        );

        $this->start_controls_tabs('unlockafe_footer_newsletter_button_tabs');

        $this->start_controls_tab(
            'unlockafe_footer_newsletter_button_normal',
            [
                'label' => esc_html__('Normal', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'unlockafe_footer_newsletter_button_color',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_button' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_button_background',
                'label' => esc_html__('Background', 'unlock-addons-for-elementor'),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_button',
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'unlockafe_footer_newsletter_button_hover',
            [
                'label' => esc_html__('Hover', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'unlockafe_footer_newsletter_button_color_hover',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_button:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_button_background_hover',
                'label' => esc_html__('Background', 'unlock-addons-for-elementor'),
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_button:hover',
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

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

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_button_border',
                'label' => esc_html__('Border', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_button',
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_newsletter_button_border_radius',
            [
                'label' => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_newsletter_button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'unlockafe_footer_newsletter_button_box_shadow',
                'label' => esc_html__('Box Shadow', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .unlock_addon_footer_newsletter_button',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $title = !empty($settings['newsletter_title']) ? trim($settings['newsletter_title']) : esc_html__('Subscribe to Newsletter', 'unlock-addons-for-elementor');
        $description = !empty($settings['newsletter_description']) ? $settings['newsletter_description'] : '';
        $email_placeholder = !empty($settings['email_placeholder']) ? $settings['email_placeholder'] : esc_html__('Enter your email', 'unlock-addons-for-elementor');
        $button_text = !empty($settings['button_text']) ? $settings['button_text'] : esc_html__('Subscribe', 'unlock-addons-for-elementor');
        $form_action = !empty($settings['form_action']['url']) ? esc_url($settings['form_action']['url']) : '#';
        $layout_style = !empty($settings['layout_style']) ? $settings['layout_style'] : 'inline';
        $widget_id = $this->get_id();

        ?>
        <div class="unlock_addon_footer_newsletter">
            <h3 class="unlock_addon_footer_newsletter_title" style="display: block !important; visibility: visible !important; opacity: 1 !important;"><?php echo esc_html($title); ?></h3>
            
            <?php if (!empty($description)) : ?>
                <p class="unlock_addon_footer_newsletter_description"><?php echo esc_html($description); ?></p>
            <?php endif; ?>
            
            <form class="unlock_addon_footer_newsletter_form unlockafe-newsletter-<?php echo esc_attr($layout_style); ?>" action="<?php echo esc_attr($form_action); ?>" method="post" data-widget-id="<?php echo esc_attr($widget_id); ?>">
                <input type="email" name="EMAIL" class="unlock_addon_footer_newsletter_input" placeholder="<?php echo esc_attr($email_placeholder); ?>" required>
                <button type="submit" class="unlock_addon_footer_newsletter_button"><?php echo esc_html($button_text); ?></button>
            </form>
        </div>
        <?php
    }
}

