import 'package:flutter/material.dart';
import 'package:submanager/screens/main_screen.dart';
import 'package:submanager/screens/add_subscription_screen.dart';
import 'package:submanager/screens/detail_screen.dart';
import 'package:submanager/screens/sms_scan_screen.dart';
import 'package:submanager/screens/analysis_screen.dart';
import 'package:submanager/screens/settings_screen.dart';
import 'package:submanager/screens/splash_screen.dart';
import 'package:submanager/screens/sms_permission_screen.dart';
import 'package:submanager/models/subscription_model.dart';
import 'package:submanager/screens/payment_card_management_screen.dart';
import '../l10n/app_localizations.dart';

class AppRoutes {
  static const String splash = '/splash';
  static const String main = '/';
  static const String addSubscription = '/add-subscription';
  static const String subscriptionDetail = '/subscription-detail';
  static const String smsScanner = '/sms-scanner';
  static const String analysis = '/analysis';
  static const String settings = '/settings';
  static const String smsPermission = '/sms-permission';
  static const String paymentCardManagement = '/payment-card-management';

  static Map<String, WidgetBuilder> getRoutes() {
    return {
      splash: (context) => const SplashScreen(),
      main: (context) => const MainScreen(),
      addSubscription: (context) => const AddSubscriptionScreen(),
      smsScanner: (context) => const SmsScanScreen(),
      analysis: (context) => const AnalysisScreen(),
      settings: (context) => const SettingsScreen(),
      smsPermission: (context) => const SmsPermissionScreen(),
      paymentCardManagement: (context) => const PaymentCardManagementScreen(),
    };
  }

  static Route<dynamic> generateRoute(RouteSettings routeSettings) {
    switch (routeSettings.name) {
      case splash:
        return _buildRoute(const SplashScreen(), routeSettings);

      case main:
        return _buildRoute(const MainScreen(), routeSettings);

      case addSubscription:
        return _buildRoute(const AddSubscriptionScreen(), routeSettings);

      case subscriptionDetail:
        final subscription = routeSettings.arguments as SubscriptionModel?;
        if (subscription != null) {
          return _buildRoute(
              DetailScreen(subscription: subscription), routeSettings);
        }
        return _errorRoute();

      case smsScanner:
        return _buildRoute(const SmsScanScreen(), routeSettings);

      case analysis:
        return _buildRoute(const AnalysisScreen(), routeSettings);

      case settings:
        return _buildRoute(const SettingsScreen(), routeSettings);

      case smsPermission:
        return _buildRoute(const SmsPermissionScreen(), routeSettings);
      case paymentCardManagement:
        return _buildRoute(const PaymentCardManagementScreen(), routeSettings);

      default:
        return _errorRoute();
    }
  }

  static Route<dynamic> _buildRoute(Widget page, RouteSettings settings) {
    return MaterialPageRoute(
      builder: (_) => page,
      settings: settings,
    );
  }

  static Route<dynamic> _errorRoute() {
    return MaterialPageRoute(
      builder: (context) => Scaffold(
        body: Center(
          child: Text(AppLocalizations.of(context).pageNotFound),
        ),
      ),
    );
  }

  static void navigateTo(BuildContext context, String routeName,
      {Object? arguments}) {
    Navigator.pushNamed(context, routeName, arguments: arguments);
  }

  static void navigateAndReplace(BuildContext context, String routeName,
      {Object? arguments}) {
    Navigator.pushReplacementNamed(context, routeName, arguments: arguments);
  }

  static void navigateAndRemoveUntil(BuildContext context, String routeName,
      {Object? arguments}) {
    Navigator.pushNamedAndRemoveUntil(
      context,
      routeName,
      (route) => false,
      arguments: arguments,
    );
  }

  static void pop(BuildContext context, {dynamic result}) {
    if (Navigator.canPop(context)) {
      Navigator.pop(context, result);
    }
  }

  static bool canPop(BuildContext context) {
    return Navigator.canPop(context);
  }
}
