<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
use Elementor\Repeater;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Process extends Widget_Base {
    use Sifency_Core_Text_Style;
	public function get_name() {
		return 'sifency_process';
	}

	public function get_title() {
		return esc_html__( 'Sifency Process', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-flow';
	}

	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}

	public function get_style_depends() {
		return [ 'sifency-process-style' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			[ 'label' => esc_html__( 'Process Steps', 'sifency-addons' ) ]
		);

		$this->add_control(
			'skin',
			[
				'label'   => esc_html__( 'Layout Skin', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'bubble',
				'options' => [
					'bubble'     => 'Bubble Flow (Screenshot)',
					'horizontal' => 'Horizontal Standard',
					'vertical'   => 'Vertical Timeline',
					'cards'      => 'Modern Cards',
				],
				'prefix_class' => 'sifency-process-skin-',
			]
		);

		$this->add_responsive_control(
			'columns',
			[
				'label' => 'Columns (Horizontal/Cards)',
				'type' => Controls_Manager::SELECT,
				'default' => '4',
				'options' => [ '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4', '5'=>'5' ],
				'condition' => [ 'skin' => ['horizontal', 'cards', 'bubble'] ],
				'selectors' => [ '{{WRAPPER}} .sifency-process-wrapper' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ],
			]
		);

		$repeater = new Repeater();

		$repeater->add_control( 'title', [
			'label' => esc_html__( 'Title', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'Step Title',
			'label_block' => true,
		]);

		$repeater->add_control( 'description', [
			'label' => esc_html__( 'Description', 'sifency-addons' ),
			'type' => Controls_Manager::TEXTAREA,
			'default' => 'Short description of this process step.',
		]);

		$repeater->add_control( 'icon_type', [
			'label' => 'Graphic Type',
			'type' => Controls_Manager::CHOOSE,
			'options' => [
				'icon' => [ 'icon' => 'eicon-star' ],
				'text' => [ 'icon' => 'eicon-font' ],
				'none' => [ 'icon' => 'eicon-ban' ],
			],
			'default' => 'icon',
		]);

		$repeater->add_control( 'step_icon', [
			'label' => 'Icon',
			'type' => Controls_Manager::ICONS,
			'default' => [ 'value' => 'fas fa-rocket', 'library' => 'fa-solid' ],
			'condition' => [ 'icon_type' => 'icon' ],
		]);

		$repeater->add_control( 'step_number', [
			'label' => 'Custom Number/Text',
			'type' => Controls_Manager::TEXT,
			'placeholder' => '1',
			'condition' => [ 'icon_type' => 'text' ],
		]);

		// Individual Override Colors
		$repeater->add_control( 'item_color', [
			'label' => 'Accent Color',
			'type' => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} {{CURRENT_ITEM}} .sifency-process-icon-box' => 'background-color: {{VALUE}};' ],
		]);

		$this->add_control(
			'steps',
			[
				'label' => esc_html__( 'Steps', 'sifency-addons' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[ 'title' => 'Heelo Sun', 'item_color' => '#fddad3ff' ],
					[ 'title' => 'Hello Moon', 'item_color' => '#6d8fffff' ],
					[ 'title' => 'Hello Naptune', 'item_color' => '#f4ff77' ],
					[ 'title' => 'Hello Jupiter', 'item_color' => '#8b9dffff' ],
				],
				'title_field' => '{{{ title }}}',
			]
		);

		$this->end_controls_section();

		// ==========================
		// STYLE: GENERAL
		// ==========================
		$this->start_controls_section( 'style_general', [ 'label' => 'General', 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_responsive_control( 'gap', [
			'label' => 'Gap',
			'type' => Controls_Manager::SLIDER,
			'selectors' => [ '{{WRAPPER}} .sifency-process-wrapper' => 'gap: {{SIZE}}px;' ],
		]);
		
		// Connector Line
		$this->add_control( 'connector_heading', [ 'label' => 'Connector Line', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$this->add_control( 'show_connector', [ 'label' => 'Show Line', 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] );
		$this->add_control( 'connector_color', [
			'label' => 'Line Color',
			'type' => Controls_Manager::COLOR,
			'default' => '#ccc',
			'condition' => [ 'show_connector' => 'yes' ],
			'selectors' => [ '{{WRAPPER}} .sifency-process-step:not(:last-child):after' => 'border-color: {{VALUE}};' ],
		]);

		$this->end_controls_section();

		// ==========================
		// STYLE: ICON / BUBBLE
		// ==========================
		$this->start_controls_section( 'style_icon', [ 'label' => 'Icon / Bubble', 'tab' => Controls_Manager::TAB_STYLE ] );
		
		$this->add_responsive_control( 'icon_size', [
			'label' => 'Box Size',
			'type' => Controls_Manager::SLIDER,
			'range' => [ 'px' => [ 'min' => 50, 'max' => 200 ] ],
			'default' => [ 'size' => 120 ],
			'selectors' => [ '{{WRAPPER}} .sifency-process-icon-box' => 'width: {{SIZE}}px; height: {{SIZE}}px; flex: 0 0 {{SIZE}}px;' ],
		]);

		$this->add_responsive_control( 'icon_font_size', [
			'label' => 'Icon Size',
			'type' => Controls_Manager::SLIDER,
			'selectors' => [ '{{WRAPPER}} .sifency-process-icon' => 'font-size: {{SIZE}}px;' ],
		]);

		$this->add_control( 'icon_color', [ 'label' => 'Icon Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-process-icon' => 'color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'icon_bg', 'selector' => '{{WRAPPER}} .sifency-process-icon-box' ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'selector' => '{{WRAPPER}} .sifency-process-icon-box' ] );
		$this->add_responsive_control( 'icon_radius', [ 'label' => 'Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-process-icon-box' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_shadow', 'selector' => '{{WRAPPER}} .sifency-process-icon-box' ] );
        $this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Process Item Title',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Process Item Subtitle',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'number_style',
            'label'    => 'Count',
            'selector' => '.sifency-number-style',
            'target_children' => false,
        ]);
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$wrapper_classes = 'sifency-process-wrapper';
		if ( 'yes' === $settings['show_connector'] ) $wrapper_classes .= ' has-connector';
		?>
		<div class="<?php echo esc_attr( $wrapper_classes ); ?>">
			<?php foreach ( $settings['steps'] as $index => $step ) : 
				$count = $index + 1;
				$step_classes = 'sifency-process-step elementor-repeater-item-' . $step['_id'];
				
				// Staggered Animation Delay
				$delay = $index * 0.2; 
				$style_attr = "animation-delay: {$delay}s;";
			?>
				<div class="<?php echo esc_attr( $step_classes ); ?>" style="<?php echo esc_attr( $style_attr ); ?>">
					
					<div class="sifency-process-icon-wrap">
						<div class="sifency-process-icon-box">
							<?php if ( 'icon' === $step['icon_type'] ) : ?>
								<div class="sifency-process-icon">
									<?php \Elementor\Icons_Manager::render_icon( $step['step_icon'], [ 'aria-hidden' => 'true' ] ); ?>
								</div>
							<?php elseif ( 'text' === $step['icon_type'] ) : ?>
								<span class="sifency-process-icon-text"><?php echo esc_html( $step['step_number'] ); ?></span>
							<?php endif; ?>
						</div>

						<div class="sifency-process-count sifency-number-style"><?php echo esc_html( $count ); ?>.</div>
					</div>

					<div class="sifency-process-content">
						<h3 class="sifency-process-title sifency-heading-style"><?php echo esc_html( $step['title'] ); ?></h3>
						<div class="sifency-process-desc sifency-content-style"><?php echo esc_html( $step['description'] ); ?></div>
					</div>

				</div>
			<?php endforeach; ?>
		</div>
		<?php
	}
}