<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
use Elementor\Repeater;

class Sifency_Working_Hours extends Widget_Base {

	public function get_name() {
		return 'sifency_working_hours';
	}

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

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

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

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

	protected function register_controls() {

		// ==========================================
		// CONTENT: LIST
		// ==========================================
		$this->start_controls_section(
			'section_content',
			[ 'label' => esc_html__( 'Hours List', 'sifency-addons' ) ]
		);

		$this->add_control(
			'auto_highlight',
			[
				'label' => esc_html__( 'Auto-Highlight "Today"', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'yes',
				'description' => 'Automatically applies the "Current Day" style based on the visitor\'s local time or server time.',
			]
		);

		$repeater = new Repeater();

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

		$repeater->add_control( 'time', [
			'label' => esc_html__( 'Hours', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => '09:00 AM - 05:00 PM',
		]);

		$repeater->add_control( 'is_closed', [
			'label' => esc_html__( 'Is Closed?', 'sifency-addons' ),
			'type' => Controls_Manager::SWITCHER,
			'default' => 'no',
		]);

		$repeater->add_control( 'closed_text', [
			'label' => esc_html__( 'Closed Text', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'Closed',
			'condition' => [ 'is_closed' => 'yes' ],
		]);

		$repeater->add_control( 'icon', [
			'label' => esc_html__( 'Icon', 'sifency-addons' ),
			'type' => Controls_Manager::ICONS,
		]);

		// Manual Highlight Override
		$repeater->add_control( 'force_highlight', [
			'label' => esc_html__( 'Force Highlight', 'sifency-addons' ),
			'type' => Controls_Manager::SWITCHER,
			'description' => 'Manually mark this item as active.',
		]);

		$this->add_control(
			'hours_list',
			[
				'label' => esc_html__( 'Days', 'sifency-addons' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[ 'day' => 'Monday', 'time' => '10 AM - 23 PM' ],
					[ 'day' => 'Tuesday', 'time' => '08 AM - 23 PM' ],
					[ 'day' => 'Wednesday', 'is_closed' => 'yes' ],
					[ 'day' => 'Thursday', 'time' => '08 AM - 23 PM' ],
					[ 'day' => 'Friday', 'time' => '08 AM - 23 PM' ],
				],
				'title_field' => '{{{ day }}}',
			]
		);

		$this->end_controls_section();

		// ==========================================
		// STYLE: GENERAL
		// ==========================================
		$this->start_controls_section( 'style_general', [ 'label' => 'General', 'tab' => Controls_Manager::TAB_STYLE ] );
		
		$this->add_responsive_control( 'rows_gap', [
			'label' => 'Row Gap',
			'type' => Controls_Manager::SLIDER,
			'selectors' => [ '{{WRAPPER}} .sifency-wh-item' => 'margin-bottom: {{SIZE}}px;' ],
		]);

		$this->add_control( 'divider_heading', [ 'label' => 'Row Divider', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$this->add_control( 'show_row_divider', [
			'label' => 'Show Row Divider',
			'type' => Controls_Manager::SWITCHER,
			'default' => 'yes',
		]);
		$this->add_control( 'row_divider_color', [
			'label' => 'Color',
			'type' => Controls_Manager::COLOR,
			'default' => '#eee',
			'condition' => [ 'show_row_divider' => 'yes' ],
			'selectors' => [ '{{WRAPPER}} .sifency-wh-item' => 'border-bottom: 1px solid {{VALUE}}; padding-bottom: 10px;' ],
		]);

		$this->end_controls_section();

		// ==========================================
		// STYLE: TYPOGRAPHY (TABS)
		// ==========================================
		$this->start_controls_section( 'style_typography', [ 'label' => 'Typography & Colors', 'tab' => Controls_Manager::TAB_STYLE ] );

		$this->start_controls_tabs( 'tabs_style' );

		// --- NORMAL STATE ---
		$this->start_controls_tab( 'tab_normal', [ 'label' => 'Normal' ] );
		
		$this->add_control( 'heading_day_normal', [ 'label' => 'Day', 'type' => Controls_Manager::HEADING ] );
		$this->add_control( 'day_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-wh-day' => 'color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'day_typo', 'selector' => '{{WRAPPER}} .sifency-wh-day' ] );

		$this->add_control( 'heading_time_normal', [ 'label' => 'Time', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$this->add_control( 'time_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-wh-time' => 'color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'time_typo', 'selector' => '{{WRAPPER}} .sifency-wh-time' ] );
		
		$this->add_control( 'heading_line_normal', [ 'label' => 'Connector Line', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$this->add_control( 'line_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'default' => 'transparent', 'selectors' => [ '{{WRAPPER}} .sifency-wh-line' => 'border-bottom-color: {{VALUE}};' ] ] );
		$this->add_control( 'line_style', [
			'label' => 'Style', 'type' => Controls_Manager::SELECT, 
			'options' => [ 'solid'=>'Solid', 'dashed'=>'Dashed', 'dotted'=>'Dotted' ],
			'default' => 'solid',
			'selectors' => [ '{{WRAPPER}} .sifency-wh-line' => 'border-bottom-style: {{VALUE}};' ]
		]);

		$this->end_controls_tab();

		// --- CLOSED STATE ---
		$this->start_controls_tab( 'tab_closed', [ 'label' => 'Closed' ] );
		
		$this->add_control( 'closed_note', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => 'Styles for items marked as "Closed".' ] );
		
		$this->add_control( 'closed_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'default' => '#cccccc', 'selectors' => [ '{{WRAPPER}} .sifency-wh-item.is-closed .sifency-wh-day, {{WRAPPER}} .sifency-wh-item.is-closed .sifency-wh-time' => 'color: {{VALUE}};' ] ] );
		
		$this->add_control( 'closed_decoration', [
			'label' => 'Text Decoration',
			'type' => Controls_Manager::SELECT,
			'options' => [ 'none' => 'None', 'line-through' => 'Line Through' ],
			'default' => 'none',
			'selectors' => [ '{{WRAPPER}} .sifency-wh-item.is-closed .sifency-wh-day' => 'text-decoration: {{VALUE}};' ]
		]);

		$this->add_control( 'closed_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-wh-item.is-closed' => 'background-color: {{VALUE}};' ] ] );

		$this->end_controls_tab();

		// --- CURRENT DAY STATE ---
		$this->start_controls_tab( 'tab_current', [ 'label' => 'Today' ] );
		
		$this->add_control( 'current_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'default' => '#000000', 'selectors' => [ '{{WRAPPER}} .sifency-wh-item.is-current .sifency-wh-day, {{WRAPPER}} .sifency-wh-item.is-current .sifency-wh-time' => 'color: {{VALUE}}; font-weight: bold;' ] ] );
		
		$this->add_control( 'current_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-wh-item.is-current' => 'background-color: {{VALUE}};' ] ] );
		
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'current_border', 'selector' => '{{WRAPPER}} .sifency-wh-item.is-current' ] );

		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		
		// Get current day name (e.g., "Monday") based on WP timezone
		$current_day = date_i18n('l'); 

		?>
		<div class="sifency-working-hours-wrapper">
			<ul class="sifency-wh-list">
				<?php foreach ( $settings['hours_list'] as $item ) : 
					$is_closed = ( 'yes' === $item['is_closed'] );
					$is_current = false;

					// Check Auto Highlight
					if ( 'yes' === $settings['auto_highlight'] ) {
						// Simple check: does the Repeater Day string contain today's name?
						if ( stripos( $item['day'], $current_day ) !== false ) {
							$is_current = true;
						}
					}
					// Check Manual Force Highlight
					if ( 'yes' === $item['force_highlight'] ) {
						$is_current = true;
					}

					$item_classes = 'sifency-wh-item';
					if ( $is_closed ) $item_classes .= ' is-closed';
					if ( $is_current ) $item_classes .= ' is-current';
				?>
					<li class="<?php echo esc_attr( $item_classes ); ?>">
						
						<!-- Day Side -->
						<div class="sifency-wh-day-wrap">
							<?php if ( ! empty( $item['icon']['value'] ) ) : ?>
								<span class="sifency-wh-icon">
									<?php \Elementor\Icons_Manager::render_icon( $item['icon'], [ 'aria-hidden' => 'true' ] ); ?>
								</span>
							<?php endif; ?>
							<span class="sifency-wh-day"><?php echo esc_html( $item['day'] ); ?></span>
						</div>

						<!-- Connector Line -->
						<div class="sifency-wh-line"></div>

						<!-- Time Side -->
						<div class="sifency-wh-time-wrap">
							<span class="sifency-wh-time">
								<?php 
								if ( $is_closed ) {
									echo esc_html( $item['closed_text'] );
								} else {
									echo esc_html( $item['time'] );
								}
								?>
							</span>
						</div>

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