import 'package:flutter/material.dart';
import '../../core/localization/app_localizations.dart';
import '../../core/theme/app_colors.dart';
import '../../core/taxonomy/app_taxonomy.dart';
import '../../data/app_data_scope.dart';
import '../../models/ad_campaign.dart';
import '../../models/tour.dart';
import '../../services/analytics_service.dart';
import '../../widgets/sponsored_banner.dart';
import '../../widgets/tour_card.dart';
import '../planner/route_planner_page.dart';
import 'tour_detail_page.dart';
class ToursPage extends StatefulWidget {
const ToursPage({
super.key,
this.initialCategory,
this.initialTopicSlug,
this.initialFamilyOnly = false,
this.initialMaxMinutes,
this.initialHighlightsOnly = false,
});
final String? initialCategory;
final String? initialTopicSlug;
final bool initialFamilyOnly;
final int? initialMaxMinutes;
final bool initialHighlightsOnly;
@override
State<ToursPage> createState() => _ToursPageState();
}
class _ToursPageState extends State<ToursPage> {
final TextEditingController _searchController = TextEditingController();
late String _category;
late bool _familyOnly;
late bool _highlightsOnly;
int? _maxMinutes;
String _query = '';
String? _topicSlug;
String? _curatedTopicId;
@override
void initState() {
super.initState();
_category = widget.initialCategory ?? '';
_topicSlug = widget.initialTopicSlug;
_familyOnly = widget.initialFamilyOnly;
_highlightsOnly = false;
_curatedTopicId = widget.initialHighlightsOnly ? 'highlights' : null;
_maxMinutes = widget.initialMaxMinutes;
AnalyticsService.instance.screenView('tours_catalog');
}
@override
void dispose() {
_searchController.dispose();
super.dispose();
}
List<Tour> _filtered(String lang) {
final q = _query.trim().toLowerCase();
final store = AppDataScope.of(context);
return store.tours.where((tour) {
final categoryOk = (_category.isEmpty || tour.category == _category) &&
(_topicSlug == null || tour.topicSlugs.contains(_topicSlug));
final curatedTopic =
_curatedTopicId == null ? null : AppTaxonomy.byId(_curatedTopicId!);
final curatedTopicOk =
curatedTopic == null || AppTaxonomy.matchesTour(curatedTopic, tour);
final familyOk = !_familyOnly || tour.familyFriendly;
final highlightOk = !_highlightsOnly || tour.highlight;
final timeOk =
_maxMinutes == null || tour.durationMinutes <= _maxMinutes!;
final queryOk = q.isEmpty ||
tour.title.get(lang).toLowerCase().contains(q) ||
tour.subtitle.get(lang).toLowerCase().contains(q) ||
tour.category.toLowerCase().contains(q) ||
tour.topicNames.any((name) => name.toLowerCase().contains(q)) ||
tour.topicSlugs.any((slug) => slug.toLowerCase().contains(q));
return categoryOk &&
curatedTopicOk &&
familyOk &&
highlightOk &&
timeOk &&
queryOk;
}).toList();
}
void _reset() => setState(() {
_category = '';
_familyOnly = false;
_highlightsOnly = false;
_maxMinutes = null;
_query = '';
_topicSlug = null;
_curatedTopicId = null;
_searchController.clear();
});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final lang = Localizations.localeOf(context).languageCode;
final tours = _filtered(lang);
final ad = AppDataScope.of(context).campaignFor(AdPlacement.toursBanner,
category: _category.isEmpty ? null : _category);
return Scaffold(
appBar: AppBar(
title: Text(l10n.t('allTours'),
style: const TextStyle(fontWeight: FontWeight.w800))),
body: ListView(
padding: const EdgeInsets.fromLTRB(20, 8, 20, 28),
children: [
Material(
color: AppColors.ink,
borderRadius: BorderRadius.circular(22),
child: InkWell(
borderRadius: BorderRadius.circular(22),
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const RoutePlannerPage(),
),
),
child: Padding(
padding: const EdgeInsets.all(18),
child: Row(
children: [
CircleAvatar(
radius: 25,
backgroundColor: AppColors.heartRed,
child: Icon(Icons.auto_awesome, color: Colors.white),
),
SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
l10n.t('individualTour'),
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w900,
fontSize: 17,
),
),
SizedBox(height: 4),
Text(
l10n.t('automaticPlannerTeaser'),
style: TextStyle(color: Colors.white70),
),
],
),
),
Icon(Icons.arrow_forward_rounded, color: Colors.white),
],
),
),
),
),
const SizedBox(height: 16),
TextField(
controller: _searchController,
onChanged: (value) => setState(() => _query = value),
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
hintText: l10n.t('tourSearch'),
border: const OutlineInputBorder()),
),
const SizedBox(height: 12),
SizedBox(
height: 40,
child: ListView(
scrollDirection: Axis.horizontal,
children: <({String id, String label})>[
(id: 'all', label: l10n.t('all')),
for (final topic in AppTaxonomy.topics.take(9))
(id: topic.id, label: topic.label(l10n)),
(id: 'family_only', label: l10n.t('family')),
(id: 'short_only', label: l10n.t('max90')),
].map((filter) {
final selected = switch (filter.id) {
'all' => _category.isEmpty &&
_curatedTopicId == null &&
!_familyOnly &&
!_highlightsOnly &&
_maxMinutes == null,
'family_only' => _familyOnly,
'short_only' => _maxMinutes == 90,
_ => _curatedTopicId == filter.id,
};
return Padding(
padding: const EdgeInsets.only(right: 7),
child: ChoiceChip(
label: Text(filter.label),
selected: selected,
onSelected: (_) => setState(() {
switch (filter.id) {
case 'all':
_category = '';
_curatedTopicId = null;
_familyOnly = false;
_highlightsOnly = false;
_maxMinutes = null;
_topicSlug = null;
break;
case 'family_only':
_familyOnly = !_familyOnly;
break;
case 'short_only':
_maxMinutes = _maxMinutes == 90 ? null : 90;
break;
default:
_curatedTopicId =
_curatedTopicId == filter.id ? null : filter.id;
_category = '';
_topicSlug = null;
}
}),
),
);
}).toList(),
),
),
const SizedBox(height: 12),
Row(children: [
Text(
'${tours.length} ${l10n.t(tours.length == 1 ? 'tourSingular' : 'tourPlural')}',
style: const TextStyle(fontWeight: FontWeight.w800)),
const Spacer(),
TextButton.icon(
onPressed: _reset,
icon: const Icon(Icons.refresh, size: 18),
label: Text(l10n.t('reset'))),
]),
if (ad != null) ...[
SponsoredBanner(
campaign: ad, compact: true, placement: 'tour_banner'),
const SizedBox(height: 14)
],
if (tours.isEmpty)
Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: AppColors.surface,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: AppColors.line)),
child: Column(children: [
const Icon(Icons.search_off_rounded, size: 42),
const SizedBox(height: 10),
Text(l10n.t('noTourResults'))
]),
)
else
for (var i = 0; i < tours.length; i++) ...[
TourCard(
tour: tours[i],
languageCode: lang,
onTap: () {
AnalyticsService.instance.track('tour_card_click',
properties: {
'tour_id': tours[i].id,
'source': 'tours_catalog'
});
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => TourDetailPage(tour: tours[i])));
},
),
if (i < tours.length - 1) const SizedBox(height: 14),
],
],
),
);
}
}

