import 'package:flutter/material.dart';
import '../../core/localization/app_localizations.dart';
import '../../data/app_data_scope.dart';
import '../../models/poi.dart';
import '../../models/tour.dart';
import '../../services/favorites_service.dart';
import '../../services/analytics_service.dart';
import '../poi/poi_detail_page.dart';
import '../tours/tour_detail_page.dart';
import '../planner/route_planner_page.dart';
class FavoritesPage extends StatefulWidget {
const FavoritesPage({super.key});
@override
State<FavoritesPage> createState() => _FavoritesPageState();
}
class _FavoritesPageState extends State<FavoritesPage> {
Set<String> _poiIds = const {};
Set<String> _tourIds = const {};
bool _loading = true;
@override
void initState() {
super.initState();
AnalyticsService.instance.screenView('favorites');
_load();
}
Future<void> _load() async {
final results = await Future.wait(
[FavoritesService.poiIds(), FavoritesService.tourIds()]);
if (!mounted) return;
setState(() {
_poiIds = results[0];
_tourIds = results[1];
_loading = false;
});
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final lang = Localizations.localeOf(context).languageCode;
final store = AppDataScope.of(context);
final pois = store.pois.where((item) => _poiIds.contains(item.id)).toList();
final tours =
store.tours.where((item) => _tourIds.contains(item.id)).toList();
return Scaffold(
appBar: AppBar(title: Text(l10n.t('favoritesTitle'))),
body: _loading
? const Center(child: CircularProgressIndicator())
: RefreshIndicator(
onRefresh: _load,
child: ListView(
padding: const EdgeInsets.fromLTRB(20, 8, 20, 32),
children: [
if (pois.isEmpty && tours.isEmpty)
Padding(
padding: const EdgeInsets.only(top: 80),
child: Column(
children: [
const Icon(Icons.favorite_border_rounded, size: 54),
const SizedBox(height: 14),
Text(l10n.t('favoritesEmptyTitle'),
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(fontWeight: FontWeight.w800)),
const SizedBox(height: 8),
Text(l10n.t('favoritesEmptySubtitle'),
textAlign: TextAlign.center),
],
),
),
if (pois.isNotEmpty) ...[
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFFFFEFF4),
borderRadius: BorderRadius.circular(20),
),
child: Row(
children: [
const Icon(Icons.route_rounded),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(l10n.t('favoriteRouteTitle'),
style: const TextStyle(
fontWeight: FontWeight.w900)),
const SizedBox(height: 3),
Text(l10n.t('favoriteRouteBody'),
style: const TextStyle(fontSize: 12)),
],
),
),
IconButton(
tooltip: l10n.t('createRoute'),
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => RoutePlannerPage(
initialRequiredPoiIds:
pois.map((p) => p.id).toSet()),
),
),
icon: const Icon(Icons.arrow_forward_rounded),
),
],
),
),
const SizedBox(height: 22),
Text(l10n.t('favoritePlaces'),
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(fontWeight: FontWeight.w900)),
const SizedBox(height: 8),
for (final poi in pois)
_PoiFavoriteTile(poi: poi, lang: lang, onReturn: _load),
const SizedBox(height: 18),
],
if (tours.isNotEmpty) ...[
Text(l10n.t('favoriteTours'),
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(fontWeight: FontWeight.w900)),
const SizedBox(height: 8),
for (final tour in tours)
_TourFavoriteTile(
tour: tour, lang: lang, onReturn: _load),
],
],
),
),
);
}
}
class _PoiFavoriteTile extends StatelessWidget {
const _PoiFavoriteTile(
{required this.poi, required this.lang, required this.onReturn});
final Poi poi;
final String lang;
final VoidCallback onReturn;
@override
Widget build(BuildContext context) => ListTile(
contentPadding: EdgeInsets.zero,
leading: const CircleAvatar(child: Icon(Icons.place_outlined)),
title: Text(poi.title.get(lang)),
subtitle: Text(poi.shortDescription.get(lang),
maxLines: 2, overflow: TextOverflow.ellipsis),
trailing: const Icon(Icons.chevron_right_rounded),
onTap: () async {
await Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => PoiDetailPage(poi: poi)));
onReturn();
},
);
}
class _TourFavoriteTile extends StatelessWidget {
const _TourFavoriteTile(
{required this.tour, required this.lang, required this.onReturn});
final Tour tour;
final String lang;
final VoidCallback onReturn;
@override
Widget build(BuildContext context) => ListTile(
contentPadding: EdgeInsets.zero,
leading: const CircleAvatar(child: Icon(Icons.route_outlined)),
title: Text(tour.title.get(lang)),
subtitle: Text(tour.subtitle.get(lang),
maxLines: 2, overflow: TextOverflow.ellipsis),
trailing: const Icon(Icons.chevron_right_rounded),
onTap: () async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (_) => TourDetailPage(tour: tour)));
onReturn();
},
);
}

