import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:url_launcher/url_launcher.dart'; import '../../customAppBar/toastHelper.dart'; import '../postEnrollment/service/api_service.dart'; import 'package:nhance_app_pwa/logger.dart'; class PopupHelper { /// 🔹 Launch URL safely static Future launchURL(String url, BuildContext context) async { final uri = Uri.tryParse(url); if (uri != null && await canLaunchUrl(uri)) { await launchUrl(uri, mode: LaunchMode.externalApplication); } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Could not launch URL')), ); } } /// 🔹 Show redirect popup static Future showRedirectPopup({ required BuildContext context, required ApiService apiService, required String? empPrimaryId, }) async { String? wellnessURL; String wellnessMessage = ''; // 🔹 Fetch link using API final response = await apiService.getWellnessLink(empPrimaryId,''); logDebug('Wellness response : $response'); if (response['status'] == 'success') { wellnessURL = response['data']; } else if (response['status'] == 'failed') { wellnessMessage = response['message']; } else { wellnessMessage = '⚠️ Unknown response format'; } final bool isMobile = MediaQuery.of(context).size.width < 600; // 🔹 If failed → show message dialog instead of redirect popup if (response['status'] == 'failed') { _showMessageDialog(context, wellnessMessage, isMobile); return; } // 🔹 Otherwise show redirect popup showDialog( context: context, barrierDismissible: false, builder: (BuildContext dialogContext) { final double dialogWidth = isMobile ? MediaQuery.of(context).size.width * 0.85 : 400; final double dialogHeight = isMobile ? MediaQuery.of(context).size.height * 0.17 : 150; // Auto close after 3 seconds and redirect Future.delayed(const Duration(seconds: 2), () { if (Navigator.of(dialogContext).canPop()) { Navigator.of(dialogContext).pop(); } if (wellnessURL != null) { launchURL(wellnessURL, context); } else { ToastHelper.showInfoToast(context, 'Invalid link or unavailable'); } }); // Auto close after 3 seconds and redirect Future.delayed(const Duration(seconds: 2), () { if (Navigator.of(dialogContext).canPop()) { Navigator.of(dialogContext).pop(); } if (wellnessURL != null) { launchURL(wellnessURL, context); } else { ToastHelper.showInfoToast(context, 'Invalid link or unavailable'); } }); return Dialog( insetPadding: EdgeInsets.symmetric(horizontal: isMobile ? 40 : 0), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), backgroundColor: Colors.white, child: Container( width: dialogWidth, height: dialogHeight, padding: const EdgeInsets.all(20), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( 'You are being redirected out of this app.', // 'You are being redirected out of this app. Click Proceed to Continue', textAlign: TextAlign.center, style: GoogleFonts.poppins( fontSize: isMobile ? 14 : 16, fontWeight: FontWeight.w400, color: const Color(0xFF777777), ), ), // const SizedBox(height: 20), // Row( // mainAxisAlignment: MainAxisAlignment.spaceEvenly, // children: [ // Expanded( // child: OutlinedButton( // onPressed: () => Navigator.pop(dialogContext), // style: OutlinedButton.styleFrom( // side: const BorderSide(color: Color(0xFFE26728)), // shape: RoundedRectangleBorder( // borderRadius: BorderRadius.circular(10), // ), // padding: const EdgeInsets.symmetric(vertical: 12), // ), // child: FittedBox( // fit: BoxFit.scaleDown, // child: Text( // 'Cancel', // style: GoogleFonts.poppins( // color: const Color(0xFFE26728), // fontSize: isMobile ? 14 : 16, // ), // ), // ), // ), // ), // const SizedBox(width: 15), // Expanded( // child: ElevatedButton( // onPressed: () { // Navigator.of(dialogContext).pop(); // if (wellnessURL != null) { // launchURL(wellnessURL, context); // } else { // ToastHelper.showInfoToast( // context, 'Invalid link or unavailable'); // } // }, // style: ElevatedButton.styleFrom( // backgroundColor: const Color(0xFFE26728), // shape: RoundedRectangleBorder( // borderRadius: BorderRadius.circular(10), // ), // padding: const EdgeInsets.symmetric(vertical: 12), // ), // child: FittedBox( // fit: BoxFit.scaleDown, // child: Text( // 'Proceed', // style: GoogleFonts.poppins( // color: Colors.white, // fontSize: isMobile ? 14 : 16, // ), // ), // ), // ), // ), // ], // ), ], ), ), ); }, ); } /// 🔹 Message dialog (for "failed" API response) static void _showMessageDialog( BuildContext context, String message, bool isMobile) { showDialog( context: context, barrierDismissible: false, builder: (BuildContext dialogContext) { final double dialogWidth = isMobile ? MediaQuery.of(context).size.width * 0.8 : 400; final double dialogHeight = isMobile ? MediaQuery.of(context).size.height * 0.15 : 130; return Dialog( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), backgroundColor: Colors.white, child: Container( width: dialogWidth, height: dialogHeight, padding: const EdgeInsets.all(20), child: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( message, textAlign: TextAlign.center, style: GoogleFonts.poppins( fontSize: isMobile ? 14 : 16, fontWeight: FontWeight.w400, color: const Color(0xFF444444), ), ), Align( alignment: Alignment.bottomCenter, child: ElevatedButton( onPressed: () => Navigator.of(dialogContext).pop(), style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFFE26728), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), padding: const EdgeInsets.symmetric( vertical: 10, horizontal: 30), ), child: Text( 'OK', style: GoogleFonts.poppins( color: Colors.white, fontSize: isMobile ? 14 : 16, ), ), ), ), ], ), ), ); }, ); } }