import 'package:flutter/material.dart';
import '../../core/localization/app_localizations.dart';
import '../../core/theme/app_colors.dart';
import '../../services/settings_service.dart';
import '../../services/offline_cache_service.dart';
import '../../data/location_scope.dart';
import '../../data/location_store.dart';
import '../../widgets/analytics_view.dart';
import '../../widgets/app_version_label.dart';
import '../../services/analytics_service.dart';
import 'legal_document_page.dart';
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key, required this.settings});
final SettingsService settings;
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
return AnalyticsView(
event: 'settings_view',
child: Scaffold(
appBar: AppBar(
title: Text(l10n.t('settings'),
style: const TextStyle(fontWeight: FontWeight.w800))),
body: ListView(
padding: const EdgeInsets.all(20),
children: [
Text(l10n.t('language'),
style: Theme.of(context)
.textTheme
.titleMedium
?.copyWith(fontWeight: FontWeight.w800)),
const SizedBox(height: 10),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
border: Border.all(color: AppColors.line)),
child: Column(
children: AppLocalizations.supportedLocales.map((locale) {
final code = locale.languageCode;
final selected = code == settings.locale.languageCode;
return ListTile(
title: Text(l10n.languageName(code)),
leading: Icon(
selected
? Icons.radio_button_checked
: Icons.radio_button_off,
),
selected: selected,
onTap: () {
AnalyticsService.instance.track('language_change',
properties: {'language': code});
settings.setLanguage(code);
},
);
}).toList(),
),
),
const SizedBox(height: 22),
ListTile(
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.favorite_outline),
title: Text(l10n.t('about')),
subtitle: const AppVersionLabel()),
const Divider(height: 32),
Text(l10n.t('legalSectionTitle'),
style: Theme.of(context)
.textTheme
.titleMedium
?.copyWith(fontWeight: FontWeight.w800)),
const SizedBox(height: 6),
ListTile(
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.shield_outlined),
title: Text(l10n.t('legalPrivacyTitle')),
subtitle: Text(l10n.t('legalPrivacySubtitle')),
trailing: const Icon(Icons.chevron_right_rounded),
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (_) => const LegalDocumentPage(
type: LegalDocumentType.privacy))),
),
ListTile(
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.gavel_outlined),
title: Text(l10n.t('legalImprintTitle')),
subtitle: Text(l10n.t('legalImprintSubtitle')),
trailing: const Icon(Icons.chevron_right_rounded),
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (_) => const LegalDocumentPage(
type: LegalDocumentType.imprint))),
),
const Divider(height: 32),
Builder(
builder: (context) {
final location = LocationScope.of(context);
final label = switch (location.status) {
AppLocationStatus.ready => l10n.t('locationAvailable'),
AppLocationStatus.loading => l10n.t('locationLoading'),
AppLocationStatus.unavailable =>
l10n.t('locationNotAvailable'),
AppLocationStatus.idle => l10n.t('locationNotChecked'),
};
return ListTile(
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.my_location_outlined),
title: Text(l10n.t('locationTitle')),
subtitle: Text(
l10n.tf('locationUseNote', {'status': label}),
),
trailing: IconButton(
tooltip: l10n.t('refreshLocation'),
onPressed: location.isLoading ? null : location.refresh,
icon: const Icon(Icons.refresh_rounded),
),
);
},
),
const Divider(height: 32),
ListTile(
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.offline_pin_outlined),
title: Text(l10n.t('offlineData')),
subtitle: Text(l10n.t('offlineDataNote')),
trailing: TextButton(
onPressed: () async {
final messenger = ScaffoldMessenger.of(context);
await OfflineCacheService.clear();
if (!context.mounted) return;
messenger.showSnackBar(
SnackBar(content: Text(l10n.t('offlineCacheCleared'))),
);
},
child: Text(l10n.t('clearCache')),
),
),
],
),
));
}
}

