<?php

/**
 * Business Hour Widget
 *
 * Recreated to replace accidentally pasted marketing text that was being output
 * at the top of the admin/editor screens. This widget lets you display opening
 * hours for all 7 days, always showing every day (Mon–Sun) and marking missing
 * / closed days clearly as “Closed”.
 *
 * @package Unlock_Addons_For_Elementor
 */

namespace Unlockafe_addons\Widgets;

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Widget_Base;
use Unlockafe_addons\Traits\Utils as UnlockafeUtils;
use Unlockafe_addons\Traits\Widget_helper;

class Business_Hour extends Widget_Base {
	use Widget_helper, UnlockafeUtils;

	public function get_name() {
		return 'unlockafe-business-hour';
	}

	public function get_title() {
		return esc_html__('Business Hour', 'unlock-addons-for-elementor');
	}

	public function get_icon() {
		return 'eicon-time-line ulicon';
	}

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

	public function get_keywords() {
		return ['business', 'opening', 'hours', 'time', 'schedule'];
	}

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

	protected function register_controls() {
		$this->register_content_controls();
		$this->register_style_controls();
	}

	protected function register_content_controls() {
		$this->start_controls_section(
			'section_general',
			[
				'label' => esc_html__('General Settings', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'layout',
			[
				'label'   => esc_html__('Layout', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'layout-1' => esc_html__('Layout 1 - Simple List', 'unlock-addons-for-elementor'),
					'layout-2' => esc_html__('Layout 2 - Card Style', 'unlock-addons-for-elementor'),
					'layout-3' => esc_html__('Layout 3 - Modern Box', 'unlock-addons-for-elementor'),
					'layout-4' => esc_html__('Layout 4 - Minimal', 'unlock-addons-for-elementor'),
				],
				'default' => 'layout-3',
			]
		);

		$this->add_control(
			'title',
			[
				'label'       => esc_html__('Title', 'unlock-addons-for-elementor'),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__('Business Hours', 'unlock-addons-for-elementor'),
				'placeholder' => esc_html__('Enter title', 'unlock-addons-for-elementor'),
			]
		);

		$this->add_control(
			'highlight_today',
			[
				'label'        => esc_html__('Highlight Today', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__('Yes', 'unlock-addons-for-elementor'),
				'label_off'    => esc_html__('No', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'show_status',
			[
				'label'        => esc_html__('Show Open/Closed Status', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__('Yes', 'unlock-addons-for-elementor'),
				'label_off'    => esc_html__('No', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'time_format',
			[
				'label'   => esc_html__('Time Format', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'12' => esc_html__('12 Hour (AM/PM)', 'unlock-addons-for-elementor'),
					'24' => esc_html__('24 Hour', 'unlock-addons-for-elementor'),
				],
				'default' => '12',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_content',
			[
				'label' => esc_html__('Business Hours', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'day',
			[
				'label'   => esc_html__('Day', 'unlock-addons-for-elementor'),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'monday'    => esc_html__('Monday', 'unlock-addons-for-elementor'),
					'tuesday'   => esc_html__('Tuesday', 'unlock-addons-for-elementor'),
					'wednesday' => esc_html__('Wednesday', 'unlock-addons-for-elementor'),
					'thursday'  => esc_html__('Thursday', 'unlock-addons-for-elementor'),
					'friday'    => esc_html__('Friday', 'unlock-addons-for-elementor'),
					'saturday'  => esc_html__('Saturday', 'unlock-addons-for-elementor'),
					'sunday'    => esc_html__('Sunday', 'unlock-addons-for-elementor'),
				],
				'default' => 'monday',
			]
		);

		$repeater->add_control(
			'open_time',
			[
				'label'       => esc_html__('Open Time', 'unlock-addons-for-elementor'),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__('9:00 AM', 'unlock-addons-for-elementor'),
			]
		);

		$repeater->add_control(
			'close_time',
			[
				'label'       => esc_html__('Close Time', 'unlock-addons-for-elementor'),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__('6:00 PM', 'unlock-addons-for-elementor'),
			]
		);

		$repeater->add_control(
			'is_closed',
			[
				'label'        => esc_html__('Closed?', 'unlock-addons-for-elementor'),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__('Yes', 'unlock-addons-for-elementor'),
				'label_off'    => esc_html__('No', 'unlock-addons-for-elementor'),
				'return_value' => 'yes',
				'default'      => '',
			]
		);

		$this->add_control(
			'business_hours',
			[
				'label'       => esc_html__('Business Hours', 'unlock-addons-for-elementor'),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ day }}} - {{{ is_closed === "yes" ? "Closed" : open_time + " - " + close_time }}}',
				'default'     => [
					[
						'day'        => 'monday',
						'open_time'  => '9:00 AM',
						'close_time' => '6:00 PM',
					],
					[
						'day'        => 'tuesday',
						'open_time'  => '9:00 AM',
						'close_time' => '6:00 PM',
					],
					[
						'day'        => 'wednesday',
						'open_time'  => '9:00 AM',
						'close_time' => '6:00 PM',
					],
					[
						'day'        => 'thursday',
						'open_time'  => '9:00 AM',
						'close_time' => '6:00 PM',
					],
					[
						'day'        => 'friday',
						'open_time'  => '9:00 AM',
						'close_time' => '6:00 PM',
					],
					[
						'day'        => 'saturday',
						'open_time'  => '10:00 AM',
						'close_time' => '4:00 PM',
					],
					[
						'day'       => 'sunday',
						'is_closed' => 'yes',
					],
				],
			]
		);

		$this->end_controls_section();
	}

	protected function register_style_controls() {
		// Title
		$this->start_controls_section(
			'section_style_title',
			[
				'label' => esc_html__('Title', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'title_color',
			[
				'label'     => esc_html__('Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-business-hour-title' => 'color: {{VALUE}};',
				],
				'default'   => '#1f2937',
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .unlockafe-business-hour-title',
			]
		);

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

		$this->add_responsive_control(
			'title_align',
			[
				'label'     => esc_html__('Alignment', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'   => [
						'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',
					],
					'right'  => [
						'title' => esc_html__('Right', 'unlock-addons-for-elementor'),
						'icon'  => 'eicon-text-align-right',
					],
				],
				'default'   => 'left',
				'selectors' => [
					'{{WRAPPER}} .unlockafe-business-hour-title' => 'text-align: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		// Row/container.
		$this->start_controls_section(
			'section_style_row',
			[
				'label' => esc_html__('Row', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'row_border',
				'selector' => '{{WRAPPER}} .unlockafe-business-hour-row',
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'row_shadow',
				'selector' => '{{WRAPPER}} .unlockafe-business-hour-row',
			]
		);

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

		$this->add_responsive_control(
			'row_gap',
			[
				'label'      => esc_html__('Row Gap', 'unlock-addons-for-elementor'),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => ['px'],
				'range'      => [
					'px' => [
						'min' => 0,
						'max' => 50,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 8,
				],
				'selectors'  => [
					'{{WRAPPER}} .unlockafe-business-hour-list' => 'row-gap: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		// Day label.
		$this->start_controls_section(
			'section_style_day',
			[
				'label' => esc_html__('Day Label', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'day_color',
			[
				'label'     => esc_html__('Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-business-hour-day' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'day_typography',
				'selector' => '{{WRAPPER}} .unlockafe-business-hour-day',
			]
		);

		$this->end_controls_section();

		// Time label.
		$this->start_controls_section(
			'section_style_time',
			[
				'label' => esc_html__('Time / Status', 'unlock-addons-for-elementor'),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'time_color',
			[
				'label'     => esc_html__('Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .unlockafe-business-hour-time' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'closed_color',
			[
				'label'     => esc_html__('Closed Color', 'unlock-addons-for-elementor'),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#e53935',
				'selectors' => [
					'{{WRAPPER}} .unlockafe-business-hour-time.is-closed' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'time_typography',
				'selector' => '{{WRAPPER}} .unlockafe-business-hour-time',
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings       = $this->get_settings_for_display();
		$repeater_items = !empty($settings['business_hours']) ? $settings['business_hours'] : [];
		$layout         = !empty($settings['layout']) ? $settings['layout'] : 'layout-3';
		$title          = !empty($settings['title']) ? $settings['title'] : '';
		$highlight_today = !empty($settings['highlight_today']) && $settings['highlight_today'] === 'yes';
		$show_status    = !empty($settings['show_status']) && $settings['show_status'] === 'yes';

		// Get current day
		$current_day = strtolower(date('l')); // Monday, Tuesday, etc.
		$current_day_key = strtolower($current_day);

		// Index repeater by day for quick lookup.
		$by_day = [];
		foreach ($repeater_items as $item) {
			$day_key = !empty($item['day']) ? $item['day'] : '';
			if ($day_key) {
				$by_day[$day_key] = $item;
			}
		}

		$days = [
			'monday'    => esc_html__('Monday', 'unlock-addons-for-elementor'),
			'tuesday'   => esc_html__('Tuesday', 'unlock-addons-for-elementor'),
			'wednesday' => esc_html__('Wednesday', 'unlock-addons-for-elementor'),
			'thursday'  => esc_html__('Thursday', 'unlock-addons-for-elementor'),
			'friday'    => esc_html__('Friday', 'unlock-addons-for-elementor'),
			'saturday'  => esc_html__('Saturday', 'unlock-addons-for-elementor'),
			'sunday'    => esc_html__('Sunday', 'unlock-addons-for-elementor'),
		];

		$wrapper_class = 'unlockafe-business-hour-wrapper unlockafe-business-hour-' . esc_attr($layout);

		echo '<div class="' . $wrapper_class . '">';
		
		if (!empty($title)) {
			echo '<h3 class="unlockafe-business-hour-title">' . esc_html($title) . '</h3>';
		}

		echo '<div class="unlockafe-business-hour-list">';

		foreach ($days as $key => $label) {
			$item      = isset($by_day[$key]) ? $by_day[$key] : [];
			$is_closed = isset($item['is_closed']) && $item['is_closed'] === 'yes';

			$open_raw  = isset($item['open_time']) ? trim($item['open_time']) : '';
			$close_raw = isset($item['close_time']) ? trim($item['close_time']) : '';

			if (!$is_closed && ($open_raw === '' || $close_raw === '')) {
				$is_closed = true;
			}

			// Check if today
			$is_today = $highlight_today && ($key === $current_day_key);
			$item_class = 'unlockafe-business-hour-item';
			if ($is_today) {
				$item_class .= ' today';
			}
			if ($is_closed) {
				$item_class .= ' is-closed';
			}

			echo '<div class="' . esc_attr($item_class) . '">';
			echo '<span class="unlockafe-business-hour-day">' . esc_html($label) . '</span>';

			if ($is_closed) {
				echo '<span class="unlockafe-business-hour-time is-closed">' . esc_html__('Closed', 'unlock-addons-for-elementor') . '</span>';
			} else {
				$time_display = $open_raw . ' - ' . $close_raw;
				echo '<span class="unlockafe-business-hour-time">' . esc_html($time_display) . '</span>';
			}

			if ($show_status && !$is_closed) {
				// Check if currently open (simple check - can be enhanced)
				$status_class = 'status-open';
				$status_text = esc_html__('Open', 'unlock-addons-for-elementor');
				echo '<span class="unlockafe-business-hour-status ' . esc_attr($status_class) . '">' . $status_text . '</span>';
			} elseif ($show_status && $is_closed) {
				echo '<span class="unlockafe-business-hour-status status-closed">' . esc_html__('Closed', 'unlock-addons-for-elementor') . '</span>';
			}

			echo '</div>';
		}

		echo '</div>';
		echo '</div>';
	}
}

