import 'dart:async';
import 'package:flutter/material.dart';
import '../core/localization/app_localizations.dart';
import '../core/theme/app_colors.dart';
import '../services/opening_hours_service.dart';
class OpeningStatusBadge extends StatefulWidget {
const OpeningStatusBadge({super.key, required this.openingHours});
final String? openingHours;
@override
State<OpeningStatusBadge> createState() => _OpeningStatusBadgeState();
}
class _OpeningStatusBadgeState extends State<OpeningStatusBadge> {
Timer? _timer;
@override
void initState() {
super.initState();
_timer = Timer.periodic(const Duration(minutes: 1), (_) {
if (mounted) setState(() {});
});
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
final info = OpeningHoursService.parse(widget.openingHours);
if (!info.canShow || info.state == OpeningState.unknown) {
return const SizedBox.shrink();
}
final l10n = AppLocalizations.of(context);
final style = switch (info.state) {
OpeningState.open || OpeningState.alwaysAccessible => const _BadgeStyle(
background: Color(0xFFE6F4EA),
foreground: Color(0xFF176B35),
),
OpeningState.closed => const _BadgeStyle(
background: Color(0xFFFDE8E7),
foreground: Color(0xFFB3261E),
),
OpeningState.closingSoon => const _BadgeStyle(
background: Color(0xFFFFEAC2),
foreground: Color(0xFF8A5300),
),
OpeningState.openingSoon => const _BadgeStyle(
background: Color(0xFFEDE7F6),
foreground: Color(0xFF5E35B1),
),
OpeningState.unknown => const _BadgeStyle(
background: AppColors.line,
foreground: AppColors.mutedInk,
),
};
final label = switch (info.state) {
OpeningState.open => l10n.t('openingStatusOpen'),
OpeningState.closed => l10n.t('openingStatusClosed'),
OpeningState.closingSoon => l10n.t('openingStatusClosingSoon'),
OpeningState.openingSoon => l10n.t('openingStatusOpeningSoon'),
OpeningState.alwaysAccessible => l10n.t('openingStatusAlwaysAccessible'),
OpeningState.unknown => '',
};
return Align(
alignment: Alignment.centerLeft,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: style.background,
borderRadius: BorderRadius.circular(999),
),
child: Text(
label,
style: TextStyle(
color: style.foreground,
fontWeight: FontWeight.w900,
fontSize: 12,
),
),
),
);
}
}
class OpeningHoursSection extends StatefulWidget {
const OpeningHoursSection({super.key, required this.openingHours});
final String? openingHours;
@override
State<OpeningHoursSection> createState() => _OpeningHoursSectionState();
}
class _OpeningHoursSectionState extends State<OpeningHoursSection> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
final info = OpeningHoursService.parse(widget.openingHours);
if (!info.canShow) return const SizedBox.shrink();
final l10n = AppLocalizations.of(context);
final now = DateTime.now().toUtc();
final berlinWeekday = _berlinWeekday(now);
final tomorrow =
berlinWeekday == DateTime.sunday ? DateTime.monday : berlinWeekday + 1;
if (!info.hasStructuredSchedule && !info.alwaysAccessible) {
return _OpeningCard(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_Header(title: l10n.t('openingHours')),
const SizedBox(height: 10),
Text(info.originalText, style: const TextStyle(height: 1.45)),
],
),
);
}
return _OpeningCard(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_Header(title: l10n.t('openingHours')),
const SizedBox(height: 12),
if (info.alwaysAccessible)
_HoursRow(
label: l10n.t('openingToday'),
value: l10n.t('openingStatusAlwaysAccessible'),
emphasized: true,
)
else ...[
_HoursRow(
label: l10n.t('openingToday'),
value: _dayValue(context, info, berlinWeekday),
emphasized: true,
),
const SizedBox(height: 7),
_HoursRow(
label: l10n.t('openingTomorrow'),
value: _dayValue(context, info, tomorrow),
),
],
if (info.hasStructuredSchedule) ...[
const SizedBox(height: 8),
TextButton.icon(
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: const Size(0, 36),
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
onPressed: () => setState(() => _expanded = !_expanded),
icon: Icon(
_expanded
? Icons.expand_less_rounded
: Icons.expand_more_rounded,
size: 20,
),
label: Text(
l10n.t(_expanded ? 'openingShowLess' : 'openingShowAll'),
style: const TextStyle(fontWeight: FontWeight.w800),
),
),
],
if (_expanded) ...[
const SizedBox(height: 4),
const Divider(height: 18),
for (var day = DateTime.monday; day <= DateTime.sunday; day++) ...[
_HoursRow(
label: l10n.t('weekday$day'),
value: _dayValue(context, info, day),
),
if (day != DateTime.sunday) const SizedBox(height: 6),
],
],
if (info.notes.isNotEmpty) ...[
const SizedBox(height: 12),
Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: AppColors.softPink,
borderRadius: BorderRadius.circular(12),
),
child: Text(
info.notes.join(' · '),
style: const TextStyle(
color: AppColors.mutedInk,
height: 1.35,
fontSize: 12,
),
),
),
],
],
),
);
}
String _dayValue(
BuildContext context,
OpeningHoursInfo info,
int weekday,
) {
final l10n = AppLocalizations.of(context);
if (info.alwaysAccessible) return l10n.t('openingStatusAlwaysAccessible');
if (!info.knownDays.contains(weekday)) return l10n.t('openingNoInfo');
final intervals = info.intervalsForWeekday(weekday);
if (intervals.isEmpty) return l10n.t('openingStatusClosed');
return intervals.map((e) => e.label).join(', ');
}
int _berlinWeekday(DateTime utc) {
final year = utc.year;
int lastSunday(int month) {
final last = DateTime.utc(year, month + 1, 0);
return last.day - (last.weekday % 7);
}
final start = DateTime.utc(year, 3, lastSunday(3), 1);
final end = DateTime.utc(year, 10, lastSunday(10), 1);
final summer = !utc.isBefore(start) && utc.isBefore(end);
return utc.add(Duration(hours: summer ? 2 : 1)).weekday;
}
}
class _OpeningCard extends StatelessWidget {
const _OpeningCard({required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(18),
border: Border.all(color: AppColors.line),
),
child: child,
);
}
}
class _Header extends StatelessWidget {
const _Header({required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Row(
children: [
const Icon(Icons.schedule_outlined, color: AppColors.heartRed),
const SizedBox(width: 10),
Text(
title,
style: const TextStyle(fontWeight: FontWeight.w900),
),
],
);
}
}
class _HoursRow extends StatelessWidget {
const _HoursRow({
required this.label,
required this.value,
this.emphasized = false,
});
final String label;
final String value;
final bool emphasized;
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 88,
child: Text(
label,
style: TextStyle(
color: AppColors.mutedInk,
fontWeight: emphasized ? FontWeight.w800 : FontWeight.w600,
fontSize: 13,
),
),
),
const SizedBox(width: 8),
Expanded(
child: Text(
value,
textAlign: TextAlign.right,
style: TextStyle(
fontWeight: emphasized ? FontWeight.w900 : FontWeight.w700,
),
),
),
],
);
}
}
class _BadgeStyle {
const _BadgeStyle({required this.background, required this.foreground});
final Color background;
final Color foreground;
}

