<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
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;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Working_Hours extends Sifency_Addons_Widget_Base {

	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency_working_hours',
			'title'          => 'Sifency Working Hours',
			'icon'           => 'eicon-clock',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'working hours', 'schedule', 'time', 'business', 'hours' ),
			'style_depends'  => array( 'sifency-working-hours-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {

		$this->start_controls_section(
			'section_layout_settings',
			array( 'label' => esc_html__( 'Layout Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_style',
			array(
				'label'   => esc_html__( 'Select Layout', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'layout-1',
				'options' => array(
					'layout-1' => esc_html__( 'Layout 1: Minimalist (Dots)', 'sifency-addons' ),
					'layout-2' => esc_html__( 'Layout 2: Modern Cards', 'sifency-addons' ),
					'layout-3' => esc_html__( 'Layout 3: Split Grid', 'sifency-addons' ),
					'layout-4' => esc_html__( 'Layout 4: Icon Focused', 'sifency-addons' ),
					'layout-5' => esc_html__( 'Layout 5: Table Style', 'sifency-addons' ),
				),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'header_content',
			array( 'label' => esc_html__( 'Header Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'show_header',
			array(
				'label'   => 'Show Header?',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'header_caption',
			array(
				'label'     => 'Caption',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Schedule',
				'condition' => array( 'show_header' => 'yes' ),
			)
		);
		$this->add_control(
			'header_title',
			array(
				'label'     => 'Title',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Working Hours',
				'condition' => array( 'show_header' => 'yes' ),
			)
		);
		$this->add_control(
			'header_subtitle',
			array(
				'label'     => 'Subtitle',
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => 'Our doors are open every day.',
				'condition' => array( 'show_header' => 'yes' ),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_footer_content',
			array( 'label' => esc_html__( 'Footer Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'show_footer',
			array(
				'label'   => esc_html__( 'Show Footer?', 'sifency-addons' ),
				'type'    => \Elementor\Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'footer_location_heading',
			array(
				'label'     => esc_html__( 'Location Details', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => array( 'show_footer' => 'yes' ),
			)
		);
		$this->add_control(
			'footer_location_label',
			array(
				'label'     => esc_html__( 'Location Label', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::TEXT,
				'default'   => 'Location:',
				'condition' => array( 'show_footer' => 'yes' ),
			)
		);
		$this->add_control(
			'footer_location',
			array(
				'label'       => esc_html__( 'Location Address', 'sifency-addons' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'placeholder' => '123 Street, NY',
				'condition'   => array( 'show_footer' => 'yes' ),
			)
		);
		$this->add_control(
			'footer_phone_heading',
			array(
				'label'     => esc_html__( 'Phone Details', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => array( 'show_footer' => 'yes' ),
			)
		);
		$this->add_control(
			'footer_phone_label',
			array(
				'label'     => esc_html__( 'Phone Label', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::TEXT,
				'default'   => 'Phone:',
				'condition' => array( 'show_footer' => 'yes' ),
			)
		);
		$this->add_control(
			'footer_phone',
			array(
				'label'       => esc_html__( 'Phone Number', 'sifency-addons' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'placeholder' => '+1 234 567',
				'condition'   => array( 'show_footer' => 'yes' ),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Hours List', 'sifency-addons' ) )
		);
		$this->add_control(
			'auto_highlight',
			array(
				'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',
			array(
				'label'       => esc_html__( 'Day / Title', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'Monday',
				'label_block' => true,
			)
		);

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

		$repeater->add_control(
			'is_closed',
			array(
				'label'   => esc_html__( 'Is Closed?', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$repeater->add_control(
			'closed_text',
			array(
				'label'     => esc_html__( 'Closed Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Closed',
				'condition' => array( 'is_closed' => 'yes' ),
			)
		);
		$repeater->add_control(
			'icon',
			array(
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);
		$repeater->add_control(
			'force_highlight',
			array(
				'label'       => esc_html__( 'Force Highlight', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'description' => 'Manually mark this item as active.',
			)
		);
		$this->add_control(
			'hours_list',
			array(
				'label'       => esc_html__( 'Days', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'day'  => 'Monday',
						'time' => '10 AM - 23 PM',
					),
					array(
						'day'  => 'Tuesday',
						'time' => '08 AM - 23 PM',
					),
					array(
						'day'       => 'Wednesday',
						'is_closed' => 'yes',
					),
					array(
						'day'  => 'Thursday',
						'time' => '08 AM - 23 PM',
					),
					array(
						'day'  => 'Friday',
						'time' => '08 AM - 23 PM',
					),
				),
				'title_field' => '{{{ day }}}',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_general',
			array(
				'label' => 'General',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'rows_gap',
			array(
				'label'     => 'Row Gap',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-wh-item' => 'margin-bottom: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'divider_heading',
			array(
				'label'     => 'Row Divider',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'show_row_divider',
			array(
				'label'   => 'Show Row Divider',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'row_divider_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'condition' => array( 'show_row_divider' => 'yes' ),
				'selectors' => array( '{{WRAPPER}} .sifency-wh-item' => 'border-bottom: 1px solid {{VALUE}}; padding-bottom: 10px;' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_style',
				'label'    => 'Day Item',
				'selector' => '.sifency-wh-item',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'closed_item_style',
				'label'    => 'Closed Day',
				'selector' => 'li.sifency-wh-item.is-closed',
				'exclude'  => array( 'tabs', 'transition', 'dimensions', 'padding', 'margin' ),
			)
		);

		$this->start_controls_section(
			'style_typography',
			array(
				'label' => 'Typography & Colors',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->start_controls_tabs( 'tabs_style' );

		$this->start_controls_tab( 'tab_normal', array( 'label' => 'Normal' ) );

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

		$this->add_control(
			'heading_time_normal',
			array(
				'label'     => 'Time',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'time_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-wh-time' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'time_typo',
				'selector' => '{{WRAPPER}} .sifency-wh-time',
			)
		);

		$this->add_control(
			'heading_line_normal',
			array(
				'label'     => 'Connector Line',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'line_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => 'transparent',
				'selectors' => array( '{{WRAPPER}} .sifency-wh-line' => 'border-bottom-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'line_style',
			array(
				'label'     => 'Style',
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'solid'  => 'Solid',
					'dashed' => 'Dashed',
					'dotted' => 'Dotted',
				),
				'default'   => 'solid',
				'selectors' => array( '{{WRAPPER}} .sifency-wh-line' => 'border-bottom-style: {{VALUE}};' ),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab( 'tab_closed', array( 'label' => 'Closed' ) );

		$this->add_control(
			'closed_note',
			array(
				'type' => Controls_Manager::RAW_HTML,
				'raw'  => 'Styles for items marked as "Closed".',
			)
		);

		$this->add_control(
			'closed_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{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',
			array(
				'label'     => 'Text Decoration',
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'none'         => 'None',
					'line-through' => 'Line Through',
				),
				'default'   => 'none',
				'selectors' => array( '{{WRAPPER}} .sifency-wh-item.is-closed .sifency-wh-day' => 'text-decoration: {{VALUE}};' ),
			)
		);

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

		$this->end_controls_tab();

		$this->start_controls_tab( 'tab_current', array( 'label' => 'Today' ) );

		$this->add_control(
			'current_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#000000',
				'selectors' => array( '{{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',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-wh-item.is-current' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'current_border',
				'selector' => '{{WRAPPER}} .sifency-wh-item.is-current',
			)
		);

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

		$this->end_controls_section();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'caption_style',
				'label'     => 'Caption',
				'selector'  => '.sifency-caption-style',
				'condition' => array(
					'show_header'     => 'yes',
					'header_caption!' => '',
				),
				'exclude'   => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style',
				'label'     => 'Heading',
				'selector'  => '.sifency-heading-style',
				'condition' => array(
					'show_header'   => 'yes',
					'header_title!' => '',
				),
				'exclude'   => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Sub Header',
				'selector'   => '.sifency-content-style',
				'condition'  => array(
					'show_header'      => 'yes',
					'header_subtitle!' => '',
				),
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
				'color_only' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'time_style',
				'label'    => 'Time',
				'selector' => '.sifency-number-style',
				'exclude'  => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'label_style',
				'label'      => 'label (Footer)',
				'selector'   => '.sifency-label-style',
				'condition'  => array( 'show_footer' => 'yes' ),
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
				'color_only' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'meta_style',
				'label'      => 'Phone & Location (Footer)',
				'selector'   => '.sifency-meta-style',
				'condition'  => array( 'show_footer' => 'yes' ),
				'exclude'    => array( 'shadow', 'stroke', 'transition', 'tabs', 'bg', 'radius', 'alignment' ),
				'color_only' => true,
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'   => 'button_style_',
				'label'    => 'Button',
				'selector' => '.sifency-hs-btn',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$current_day       = esc_html( date_i18n( 'l' ) );
		$wrapper_classes   = array(
			'sifency-wh-container',
			'sifency-wh-' . $settings['layout_style'],
		);
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			
			<?php if ( 'yes' === $settings['show_header'] ) : ?>
				<div class="sifency-wh-header">
					<?php
					if ( $settings['header_caption'] ) :
						?>
						<span class="sifency-wh-caption sifency-caption-style"><?php echo esc_html( $settings['header_caption'] ); ?></span><?php endif; ?>
					<?php
					if ( $settings['header_title'] ) :
						?>
						<h3 class="sifency-wh-title sifency-heading-style"><?php echo esc_html( $settings['header_title'] ); ?></h3><?php endif; ?>
					<?php
					if ( $settings['header_subtitle'] ) :
						?>
						<p class="sifency-wh-subtitle sifency-content-style"><?php echo esc_html( $settings['header_subtitle'] ); ?></p><?php endif; ?>
				</div>
			<?php endif; ?>

			<ul class="sifency-wh-list">
				<?php
				foreach ( $settings['hours_list'] as $item ) :
					$is_closed  = ( 'yes' === $item['is_closed'] );
					$is_current = ( stripos( $item['day'], $current_day ) !== false || 'yes' === $item['force_highlight'] );

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

						<?php if ( $settings['layout_style'] === 'layout-1' ) : ?>
							<div class="sifency-wh-line"></div>
						<?php endif; ?>

						<div class="sifency-wh-time-side">
							<span class="sifency-wh-time sifency-number-style"><?php echo $is_closed ? esc_html( $item['closed_text'] ) : esc_html( $item['time'] ); ?></span>
						</div>
					</li>
				<?php endforeach; ?>
			</ul>

			<?php if ( 'yes' === $settings['show_footer'] ) : ?>
				<div class="sifency-wh-footer">
					
					<?php if ( ! empty( $settings['footer_location'] ) ) : ?>
						<div class="sifency-wh-footer-item">
							<i class="eicon-location" aria-hidden="true"></i>
							<div class="sifency-wh-footer-content">
								<?php if ( ! empty( $settings['footer_location_label'] ) ) : ?>
									<span class="sifency-wh-footer-label sifency-label-style"><?php echo esc_html( $settings['footer_location_label'] ); ?></span>
								<?php endif; ?>
								<span class="sifency-wh-footer-value sifency-meta-style"><?php echo esc_html( $settings['footer_location'] ); ?></span>
							</div>
						</div>
					<?php endif; ?>

					<?php if ( ! empty( $settings['footer_phone'] ) ) : ?>
						<div class="sifency-wh-footer-item">
							<i class="eicon-phone" aria-hidden="true"></i>
							<div class="sifency-wh-footer-content">
								<?php if ( ! empty( $settings['footer_phone_label'] ) ) : ?>
									<span class="sifency-wh-footer-label  sifency-label-style"><?php echo esc_html( $settings['footer_phone_label'] ); ?></span>
								<?php endif; ?>
								<span class="sifency-wh-footer-value sifency-meta-style"><?php echo esc_html( $settings['footer_phone'] ); ?></span>
							</div>
						</div>
					<?php endif; ?>

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