import 'package:animated_splash_screen/animated_splash_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:prosappco/src/authentication/authentication_repository.dart';
import 'package:prosappco/src/screens/calendar.dart';
import 'package:prosappco/src/screens/city.dart';
import 'package:prosappco/src/screens/login.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:prosappco/src/screens/login_email.dart';
import 'package:prosappco/src/screens/my_services.dart';
import 'package:prosappco/src/screens/my_services_pro.dart';
import 'package:prosappco/src/screens/new_number.dart';
import 'package:prosappco/src/screens/new_password.dart';
import 'package:prosappco/src/screens/profession.dart';
import 'package:prosappco/src/screens/professional_profile.dart';
import 'package:prosappco/src/screens/professional_revision.dart';
import 'package:prosappco/src/screens/profile.dart';
import 'package:prosappco/src/screens/profile_pro.dart';
import 'package:prosappco/src/screens/register.dart';
import 'package:prosappco/src/screens/request_sent.dart';
import 'package:prosappco/src/screens/service.dart';
import 'package:prosappco/src/screens/solicitudes.dart';
import 'package:prosappco/src/services/local_notifications.dart';
import 'firebase_options.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:flutter/foundation.dart' show kIsWeb; // Importa kIsWeb

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  print('Handling a backdround message: ${message.messageId}');
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  ).then((value) => Get.put(AuthenticationRepository()));

  FirebaseMessaging.onBackgroundMessage((_firebaseMessagingBackgroundHandler));

  FirebaseMessaging messaging = FirebaseMessaging.instance;

  NotificationSettings settings = await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true,
  );

  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    print('Got a message whilst in the foreground!');
    print('Message data: ${message.data}');

    if (message.notification != null) {
      print('Message also contained a notification: ${message.notification}');
    }
  });

  print('User granted permission: ${settings.authorizationStatus}');

  await initNotifications();

  await initializeDateFormatting('es_MX', null);

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  usePathUrlStrategy();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      theme: ThemeData(fontFamily: 'Poppins'),
      debugShowCheckedModeBanner: false,
      title: 'ProsApp',
      initialRoute: '/splash',
      routes: {
        '/splash': (context) => const SplashScreen(),
        '/': (context) => const LoginScreen(),
        '/login': (context) => const LoginEmailScreen(),
        '/profile': (context) => const ProfileScreen(),
        '/register': (context) => const RegisterScreen(),
        '/city': (context) => const CityScreen(),
        '/profession': (context) => const ProfessionScreen(),
        '/newNumber': (context) => const NewNumberScreen(),
        '/profesionalRevision': (context) => const ProfessionalRevisionScreen(),
        '/profesionalProfile': (context) => const ProfessionalProfileScreen(),
        '/solicitudEnviada': (context) => const RequestSentScreen(),
        '/nuevaPassword': (context) => NewPasswordScreen(),
        '/servicio': (context) => const ServiceScreen(),
        '/profilePro': (context) => const ProfileProScreen(),
        '/calendar': (context) => const CalendarScreen(),
        '/solicitud': (context) => SolicitudScreen(),
        '/misserviciospro': (context) => const MyServicesProScreen(),
        '/misservicios': (context) => MyServicesScreen(),
      },
      onGenerateRoute: (settings) {
        throw Exception('Ruta desconocida: ${settings.name}');
      },
    );
  }
}

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    if (kIsWeb) {
      // Espera 3 segundos y luego redirige a LoginScreen
      Future.delayed(const Duration(seconds: 3), () {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => const LoginScreen()),
        );
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: Center(
        child: kIsWeb
            ? Image.asset(
                '/images/splash.png') // Asegúrate de que la ruta sea correcta
            : const LoginScreen(), // En otras plataformas, muestra LoginScreen
      ),
    );
  }
}



      // splash: 'images/splashgif.gif',
      // backgroundColor: Colors.black,
      // nextScreen: const LoginScreen(),
      // splashIconSize: 50,
      // duration: 80000,