<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils;
use Unlockafe_addons\Traits\Widget_helper;
use Elementor\Controls_Manager;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;

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

/**
 * Header Logo Widget
 *
 * @since 1.0.0
 */
class Header_Logo extends Widget_Base
{
	use Widget_helper;
	use Utils;

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

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

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

	/**
	 * Retrieve the list of categories the widget belongs to.
	 *
	 * Used to determine where to display the widget in the editor.
	 *
	 * Note that currently Elementor supports only one category.
	 * When multiple categories passed, Elementor uses the first one.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories()
	{
		return ['unlockafe-addons'];
	}

	public function get_style_depends()
	{
		return ['unlockafe-global', 'unlockafe-header-logo-style'];
	}

	public function get_script_depends()
	{
		return [];
	}

	/**
	 * Register the widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 *
	 * @access protected
	 */
	protected function register_controls()
	{
		$this->register_controls_section();
		$this->style_tab_content();
	}

	protected function register_controls_section()
	{
		// layout Panel
		$this->start_controls_section(
			'wpr_layout',
			[
				'label' => esc_html__('Design Layout', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'wpr_design_style',
			[
				'label' => esc_html__('Select Layout', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'layout-1' => esc_html__('Layout 1', 'unlock-addons-for-elementor'),
				],
				'default' => 'layout-1',
			]
		);

		$this->add_control(
			'wpr_logo_link',
			[
				'label' => esc_html__('Logo Link', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::URL,
				'placeholder' => esc_html__('https://example.com', 'unlock-addons-for-elementor'),
				'default' => [
					'url' => home_url('/'),
					'is_external' => false,
					'nofollow' => false,
				],
			]
		);

		$this->add_control(
			'wpr_logo_tagline_switch',
			[
				'label' => esc_html__('Show Tagline', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Show', 'unlock-addons-for-elementor'),
				'label_off' => esc_html__('Hide', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default' => '',
			]
		);

		$this->add_control(
			'wpr_logo_tagline_text',
			[
				'label' => esc_html__('Tagline Text', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__('Creative Agency', 'unlock-addons-for-elementor'),
				'label_block' => true,
				'condition' => [
					'wpr_logo_tagline_switch' => 'yes',
				],
			]
		);

		$this->end_controls_section();

		// Elementor Controls - Site Logo Section
		$this->start_controls_section(
			'_wpr_image',
			[
				'label' => esc_html__('Site Logo', 'unlock-addons-for-elementor'),
			]
		);

		// Start Tabs
		$this->start_controls_tabs('_wpr_logo_tabs');

		// Black Logo Tab
		$this->start_controls_tab(
			'_wpr_logo_black_tab',
			[
				'label' => esc_html__('Black Logo', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'wpr_logo_black',
			[
				'label' => esc_html__('Choose Black Logo', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::MEDIA,
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
			]
		);

		$this->end_controls_tab(); // End Black Logo Tab

		// White Logo Tab
		$this->start_controls_tab(
			'_wpr_logo_white_tab',
			[
				'label' => esc_html__('White Logo', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'wpr_logo_white',
			[
				'label' => esc_html__('Choose White Logo', 'unlock-addons-for-elementor'),
				'type' => \Elementor\Controls_Manager::MEDIA,
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
			]
		);

		$this->end_controls_tab(); // End White Logo Tab

		$this->end_controls_tabs(); // End Tabs

		$this->end_controls_section();
	}

	// Style Tab Content
	protected function style_tab_content()
	{
		// Container Section
		$this->start_controls_section(
			'unlockafe_logo_container_style',
			[
				'label' => esc_html__('Container', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'unlockafe_logo_alignment',
			[
				'label' => esc_html__('Alignment', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::CHOOSE,
				'options' => [
					'flex-start' => [
						'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',
					],
					'flex-end' => [
						'title' => esc_html__('Right', 'unlock-addons-for-elementor'),
						'icon' => 'eicon-text-align-right',
					],
				],
				'default' => 'flex-start',
				'selectors' => [
					'{{WRAPPER}} .unlock_addon_header_logo' => 'justify-content: {{VALUE}};',
				],
			]
		);

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

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

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

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name' => 'unlockafe_logo_container_bg',
				'selector' => '{{WRAPPER}} .unlock_addon_logo_box',
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'unlockafe_logo_container_border',
				'selector' => '{{WRAPPER}} .unlock_addon_logo_box',
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'unlockafe_logo_container_shadow',
				'selector' => '{{WRAPPER}} .unlock_addon_logo_box',
			]
		);

		$this->end_controls_section();

		// Logo Style Section
		$this->start_controls_section(
			'wpr_logo_style_section',
			[
				'label' => esc_html__('Logo Style', 'unlock-addons-for-elementor'),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		// Logo Width
		$this->add_control(
			'wpr_logo_width',
			[
				'type'        => \Elementor\Controls_Manager::SLIDER,
				'label'       => esc_html__('Logo Width', 'unlock-addons-for-elementor'),
				'description' => esc_html__('Set logo width in pixels (max: 1000px).', 'unlock-addons-for-elementor'),
				'size_units'  => ['px'],
				'range'       => [
					'px' => [
						'min'  => 10,
						'max'  => 1000,
						'step' => 1,
					],
				],
				'default'     => [
					'unit' => 'px',
					'size' => 150,
				],
				'selectors'   => [
					'{{WRAPPER}} .wpr-el-logo img' => 'width: {{SIZE}}{{UNIT}};',
				],
			]
		);

		// Logo Opacity
		$this->add_control(
			'wpr_logo_opacity',
			[
				'label'      => esc_html__('Logo Opacity', 'unlock-addons-for-elementor'),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => ['%'],
				'range'      => [
					'%' => [
						'min'  => 10,
						'max'  => 100,
						'step' => 1,
					],
				],
				'default'    => [
					'unit' => '%',
					'size' => 100,
				],
				'selectors'  => [
					'{{WRAPPER}} .wpr-el-logo img' => 'opacity: {{SIZE}}%;',
				],
			]
		);

		// Logo Margin
		$this->add_control(
			'wpr_logo_margin',
			[
				'label'      => esc_html__('Logo Margin', 'unlock-addons-for-elementor'),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => ['px', '%', 'em'],
				'selectors'  => [
					'{{WRAPPER}} .wpr-el-logo img' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		// Logo Padding
		$this->add_control(
			'wpr_logo_padding',
			[
				'label'      => esc_html__('Logo Padding', 'unlock-addons-for-elementor'),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => ['px', '%', 'em'],
				'selectors'  => [
					'{{WRAPPER}} .wpr-el-logo img' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		// Logo Border
		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name'      => 'wpr_logo_border',
				'label'     => esc_html__('Border', 'unlock-addons-for-elementor'),
				'selector'  => '{{WRAPPER}} .wpr-el-logo img',
			]
		);

		// Logo Border Radius
		$this->add_control(
			'wpr_logo_border_radius',
			[
				'label'      => esc_html__('Border Radius', 'unlock-addons-for-elementor'),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => ['px', '%', 'em'],
				'selectors'  => [
					'{{WRAPPER}} .wpr-el-logo img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		// Logo Box Shadow
		$this->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'wpr_logo_box_shadow',
				'label'    => esc_html__('Box Shadow', 'unlock-addons-for-elementor'),
				'selector' => '{{WRAPPER}} .wpr-el-logo img',
			]
		);

		// Hover Effects - Section Start
		$this->start_controls_tabs('wpr_logo_hover_tabs');

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

		$this->add_control(
			'wpr_logo_normal_opacity',
			[
				'label'      => esc_html__('Opacity', 'unlock-addons-for-elementor'),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => ['%'],
				'range'      => [
					'%' => [
						'min'  => 10,
						'max'  => 100,
						'step' => 1,
					],
				],
				'default'    => [
					'unit' => '%',
					'size' => 100,
				],
				'selectors'  => [
					'{{WRAPPER}} .wpr-el-logo img' => 'opacity: {{SIZE}}%;',
				],
			]
		);

		$this->end_controls_tab(); // End Normal Tab

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

		$this->add_control(
			'wpr_logo_hover_opacity',
			[
				'label'      => esc_html__('Hover Opacity', 'unlock-addons-for-elementor'),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => ['%'],
				'range'      => [
					'%' => [
						'min'  => 10,
						'max'  => 100,
						'step' => 1,
					],
				],
				'default'    => [
					'unit' => '%',
					'size' => 80,
				],
				'selectors'  => [
					'{{WRAPPER}} .wpr-el-logo img:hover' => 'opacity: {{SIZE}}%;',
				],
			]
		);

		$this->add_control(
			'wpr_logo_hover_border_color',
			[
				'label'     => esc_html__('Hover Border Color', 'unlock-addons-for-elementor'),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .wpr-el-logo img:hover' => 'border-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_tab(); // End Hover Tab
		$this->end_controls_tabs(); // End Hover Effects

		$this->end_controls_section(); // End Logo Style Section

		// Tagline style
		$this->start_controls_section(
			'unlockafe_logo_tagline_style',
			[
				'label' => esc_html__('Tagline', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
				'condition' => [
					'wpr_logo_tagline_switch' => 'yes',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'unlockafe_logo_tagline_typography',
				'selector' => '{{WRAPPER}} .unlock_addon_logo_tagline',
			]
		);

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

		$this->add_responsive_control(
			'unlockafe_logo_tagline_spacing',
			[
				'label' => esc_html__('Top Gap', 'unlock-addons-for-elementor'),
				'type' => Controls_Manager::SLIDER,
				'size_units' => ['px', 'em'],
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 60,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .unlock_addon_logo_tagline' => 'margin-top: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 *
	 * @access protected
	 */
	protected function render()
	{
		$settings = $this->get_settings_for_display();

		$logo_black_url = !empty($settings['wpr_logo_black']['id'])
			? wp_get_attachment_image_url($settings['wpr_logo_black']['id'], 'full')
			: (!empty($settings['wpr_logo_black']['url']) ? $settings['wpr_logo_black']['url'] : '');

		$logo_black_alt = !empty($settings['wpr_logo_black']['id'])
			? get_post_meta($settings['wpr_logo_black']['id'], "_wp_attachment_image_alt", true)
			: '';

		$logo_white_url = !empty($settings['wpr_logo_white']['id'])
			? wp_get_attachment_image_url($settings['wpr_logo_white']['id'], 'full')
			: (!empty($settings['wpr_logo_white']['url']) ? $settings['wpr_logo_white']['url'] : '');

		$logo_white_alt = !empty($settings['wpr_logo_white']['id'])
			? get_post_meta($settings['wpr_logo_white']['id'], "_wp_attachment_image_alt", true)
			: '';

		$logo_to_display = !empty($logo_white_url) ? $logo_white_url : $logo_black_url;
		$logo_alt = !empty($logo_white_url) ? $logo_white_alt : $logo_black_alt;
		$logo_class = !empty($logo_white_url) ? 'wpr-el-logo-white' : 'wpr-el-logo-black';

		$link_settings = !empty($settings['wpr_logo_link']) ? $settings['wpr_logo_link'] : [];
		$link_url = !empty($link_settings['url']) ? $link_settings['url'] : home_url('/');
		$link_target = !empty($link_settings['is_external']) ? '_blank' : '_self';
		$link_rel_parts = [];
		if (!empty($link_settings['nofollow'])) {
			$link_rel_parts[] = 'nofollow';
		}
		if (!empty($link_settings['is_external'])) {
			$link_rel_parts[] = 'noopener';
		}
		$link_rel = !empty($link_rel_parts) ? implode(' ', $link_rel_parts) : '';

		$wrapper_classes = [
			'unlock_addon_header_logo',
			'unlock_addon_header_logo_' . $settings['wpr_design_style'],
		];

		$show_tagline = ($settings['wpr_logo_tagline_switch'] === 'yes') && !empty($settings['wpr_logo_tagline_text']);
	?>

		<div class="<?php echo esc_attr(implode(' ', array_map('sanitize_html_class', $wrapper_classes))); ?>">
			<div class="unlock_addon_logo_box">
				<?php if (!empty($logo_to_display)) : ?>
					<a class="wpr-el-logo header__logo orvio-site-logo unlock_addon_logo_link <?php echo esc_attr($logo_class); ?>" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>" <?php echo !empty($link_rel) ? 'rel="' . esc_attr($link_rel) . '"' : ''; ?>>
						<span class="unlock_addon_logo_image">
							<img src="<?php echo esc_url($logo_to_display); ?>" alt="<?php echo esc_attr($logo_alt); ?>" />
						</span>
						<?php if ($show_tagline) : ?>
							<span class="unlock_addon_logo_tagline">
								<?php echo esc_html($settings['wpr_logo_tagline_text']); ?>
							</span>
						<?php endif; ?>
					</a>
				<?php else : ?>
					<?php
					$site_title = get_bloginfo('name');
					if (!empty($site_title)) :
					?>
						<a class="unlock_addon_logo_text_link" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>" <?php echo !empty($link_rel) ? 'rel="' . esc_attr($link_rel) . '"' : ''; ?>>
							<span class="unlock_addon_logo_text"><?php echo esc_html($site_title); ?></span>
						</a>
					<?php endif; ?>
				<?php endif; ?>
			</div>
		</div>
<?php

	}
}

