import 'dart:async';
import 'package:flutter/material.dart';
import '../../core/localization/app_localizations.dart';
import '../../core/navigation/deep_link.dart';
import '../../core/theme/app_colors.dart';
import '../../models/ad_campaign.dart';
import '../../services/analytics_service.dart';
import '../../services/campaign_action_service.dart';
import '../../services/settings_service.dart';
import '../../widgets/campaign_creative_view.dart';
import '../../widgets/remote_image.dart';
import '../campaigns/campaign_landing_page.dart';
import '../shell/main_shell.dart';
/// Optional paid sponsor placement shown only AFTER the MeinWeimar brand
/// splash. Premium Lottie/image creatives may use the full responsive viewport.
class SponsorInterstitialPage extends StatefulWidget {
const SponsorInterstitialPage({
super.key,
required this.settings,
required this.campaign,
required this.initialDeepLink,
});
final SettingsService settings;
final AdCampaign campaign;
final DeepLink initialDeepLink;
@override
State<SponsorInterstitialPage> createState() =>
_SponsorInterstitialPageState();
}
class _SponsorInterstitialPageState extends State<SponsorInterstitialPage> {
bool _leaving = false;
late final DateTime _shownAt = DateTime.now();
Timer? _viewTimer;
@override
void initState() {
super.initState();
AnalyticsService.instance.adImpression(
campaignId: widget.campaign.id,
placement: 'sponsor_interstitial',
);
_viewTimer = Timer(const Duration(seconds: 1), () {
AnalyticsService.instance.track('ad_view', properties: {
'campaign_id': widget.campaign.id,
'placement': 'sponsor_interstitial',
'duration_ms': 1000
});
});
}
@override
void dispose() {
_viewTimer?.cancel();
super.dispose();
}
Future<void> _enterApp({bool countAsClose = true}) async {
if (_leaving) return;
_leaving = true;
_viewTimer?.cancel();
await AnalyticsService.instance
.track(countAsClose ? 'ad_close' : 'ad_exit_after_click', properties: {
'campaign_id': widget.campaign.id,
'placement': 'sponsor_interstitial',
'duration_ms': DateTime.now().difference(_shownAt).inMilliseconds
});
await widget.settings.markSponsorInterstitialSeen(widget.campaign.id);
if (!mounted) return;
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (_) => MainShell(
settings: widget.settings,
initialDeepLink: widget.initialDeepLink,
),
),
);
}
Future<void> _openCampaign() async {
if (_leaving) return;
await AnalyticsService.instance.adClick(
campaignId: widget.campaign.id,
placement: 'sponsor_interstitial',
);
await widget.settings.markSponsorInterstitialSeen(widget.campaign.id);
if (!mounted) return;
final opened = await CampaignActionService.open(context, widget.campaign);
if (!mounted) return;
if (!opened) {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => CampaignLandingPage(campaign: widget.campaign),
),
);
}
if (mounted) await _enterApp(countAsClose: false);
}
@override
Widget build(BuildContext context) {
final campaign = widget.campaign;
if (campaign.creativeType != CampaignCreativeType.legacy) {
return _PremiumCreativeInterstitial(
campaign: campaign,
onClose: () => _enterApp(),
onOpen: _openCampaign,
);
}
return _LegacyInterstitial(
campaign: campaign,
onClose: () => _enterApp(),
onOpen: _openCampaign,
);
}
}
class _PremiumCreativeInterstitial extends StatelessWidget {
const _PremiumCreativeInterstitial({
required this.campaign,
required this.onClose,
required this.onOpen,
});
final AdCampaign campaign;
final VoidCallback onClose;
final VoidCallback onOpen;
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final lang = Localizations.localeOf(context).languageCode;
final headline = campaign.headline.get(lang).trim();
final hasAction = campaign.target.hasTarget ||
campaign.landingPageUrl?.trim().isNotEmpty == true;
return Scaffold(
backgroundColor: campaign.backgroundColor,
body: SafeArea(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: hasAction ? onOpen : null,
child: Stack(
fit: StackFit.expand,
children: [
Positioned.fill(
child: CampaignCreativeView(
campaign: campaign,
semanticLabel:
headline.isEmpty ? campaign.partnerName : headline,
),
),
Positioned(
left: 14,
top: 12,
child: IgnorePointer(
child: _OverlayLabel(text: l10n.t('sponsored')),
),
),
Positioned(
right: 12,
top: 8,
child: Material(
color: Colors.black.withValues(alpha: 0.62),
shape: const CircleBorder(),
child: IconButton(
tooltip: l10n.t('closeAd'),
onPressed: onClose,
icon: const Icon(Icons.close_rounded, color: Colors.white),
),
),
),
],
),
),
),
);
}
}
class _LegacyInterstitial extends StatelessWidget {
const _LegacyInterstitial({
required this.campaign,
required this.onClose,
required this.onOpen,
});
final AdCampaign campaign;
final VoidCallback onClose;
final VoidCallback onOpen;
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final lang = Localizations.localeOf(context).languageCode;
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 12, 20, 20),
child: Column(
children: [
Row(
children: [
_OverlayLabel(text: l10n.t('sponsored')),
const Spacer(),
IconButton(
tooltip: l10n.t('closeAd'),
onPressed: onClose,
icon: const Icon(Icons.close_rounded),
),
],
),
const Spacer(),
Container(
width: double.infinity,
constraints: const BoxConstraints(maxWidth: 620),
padding: const EdgeInsets.all(26),
decoration: BoxDecoration(
color: AppColors.surface,
borderRadius: BorderRadius.circular(30),
border: Border.all(color: AppColors.line),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
campaign.partnerName.toUpperCase(),
style: const TextStyle(
color: AppColors.heartRed,
fontWeight: FontWeight.w900,
letterSpacing: 1.4,
),
),
const SizedBox(height: 18),
RemoteImage(
url: campaign.imageUrl,
height: 180,
width: double.infinity,
borderRadius: BorderRadius.circular(22),
fallbackIcon: Icons.storefront_rounded,
fallbackIconSize: 58,
semanticLabel: campaign.partnerName,
),
const SizedBox(height: 22),
Text(
campaign.headline.get(lang),
style: Theme.of(context)
.textTheme
.headlineSmall
?.copyWith(fontWeight: FontWeight.w900),
),
const SizedBox(height: 10),
Text(
campaign.body.get(lang),
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: FilledButton(
onPressed: onOpen,
child: Text(campaign.buttonLabel.get(lang)),
),
),
const SizedBox(height: 6),
SizedBox(
width: double.infinity,
child: TextButton(
onPressed: onClose,
child: Text(l10n.t('continueToGuide')),
),
),
],
),
),
const Spacer(),
Padding(
padding: const EdgeInsets.only(bottom: 4),
child: Image.asset(
'assets/branding/meinweimar_logo_dark.webp',
width: 145,
fit: BoxFit.contain,
),
),
],
),
),
),
);
}
}
class _OverlayLabel extends StatelessWidget {
const _OverlayLabel({required this.text});
final String text;
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
color: Colors.black.withValues(alpha: 0.62),
borderRadius: BorderRadius.circular(999),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
child: Text(
text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 10,
fontWeight: FontWeight.w900,
letterSpacing: 1.15,
),
),
),
);
}
}

