<?php
namespace VIA_Woodmart_Customize\Modules\Elementor_Custom_Animations;

use VIA_Woodmart_Customize\Abstracts\Base_Module;

if (!defined('ABSPATH')) {
	exit;
}

class Module extends Base_Module
{
	public $category = 'elementor';
	public $name = 'Elementor Custom Animations';
	public $description = 'Thêm hiệu ứng trượt và mờ ảo tùy chỉnh vào phần nâng cao của các tiện ích Elementor.';

	public function init()
	{
		// Cấu hình script và styles
		add_action('wp_enqueue_scripts', [$this, 'enqueue_assets']);

		// Hook vào Elementor Advanced tabs của mọi phần tử (Widget, Section, Column, Container)
		$actions = [
			'elementor/element/common/_section_style/after_section_end', // Widget APIũ
			'elementor/element/section/section_advanced/after_section_end', // Section
			'elementor/element/column/section_advanced/after_section_end', // Column
			'elementor/element/container/section_layout/after_section_end' // Container (layout tab)
		];

		foreach ($actions as $action) {
			add_action($action, [$this, 'register_controls'], 10, 2);
		}
	}

	public function enqueue_assets()
	{
		wp_enqueue_style('via-elementor-animations', VIA_WOODMART_CUSTOMIZE_URL . 'includes/modules/elementor-custom-animations/assets/css/style.css', [], VIA_WOODMART_CUSTOMIZE_VERSION);
		wp_enqueue_script('via-elementor-animations', VIA_WOODMART_CUSTOMIZE_URL . 'includes/modules/elementor-custom-animations/assets/js/script.js', [], VIA_WOODMART_CUSTOMIZE_VERSION, true);
	}

	public function register_controls($element, $args)
	{
		$element->start_controls_section(
			'via_custom_animations_section',
			[
				'label' => 'VIA Animations (Mượt)',
				'tab'   => \Elementor\Controls_Manager::TAB_ADVANCED,
			]
		);

		$element->add_control(
			'via_entrance_animation',
			[
				'label' => 'Hiệu ứng xuất hiện',
				'type'  => \Elementor\Controls_Manager::SELECT,
				'default' => '',
				'options' => [
					'' => 'Mặc định (Không)',
					'slide-up' => 'Slide Up (Trượt lên)',
					'slide-down' => 'Slide Down (Trượt xuống)',
					'fade-in' => 'Fade In (Mờ ảo)',
					'zoom-in' => 'Zoom In (Phóng to)',
				],
				// Elementor sẽ xuất class "via-anim-ready via-anim-slide-up"
				'prefix_class' => 'via-anim-ready via-anim-',
			]
		);

		$element->add_control(
			'via_anim_duration',
			[
				'label' => 'Thời lượng hiệu ứng (ms)',
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 100,
						'max' => 5000,
						'step' => 50,
					],
				],
				'default' => [
					'size' => 600,
				],
				// Inject biến CSS tuỳ chỉnh vào thẳng wrapper
				'selectors' => [
					'{{WRAPPER}}' => '--via-anim-duration: {{SIZE}}ms;',
				],
				'condition' => [
					'via_entrance_animation!' => '',
				],
			]
		);

		$element->add_control(
			'via_anim_delay',
			[
				'label' => 'Độ trễ (ms)',
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 5000,
						'step' => 50,
					],
				],
				'default' => [
					'size' => 0,
				],
				// Inject biến CSS tuỳ chỉnh vào thẳng wrapper
				'selectors' => [
					'{{WRAPPER}}' => '--via-anim-delay: {{SIZE}}ms;',
				],
				'condition' => [
					'via_entrance_animation!' => '',
				],
			]
		);

		$element->end_controls_section();
	}
}