import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:skillcourt/pages/club_list.dart';
import 'package:skillcourt/pages/club_registration.dart';
import 'package:skillcourt/pages/configuration.dart';
import 'package:skillcourt/pages/group_list.dart';
import 'package:skillcourt/pages/group_registration.dart';
import 'package:skillcourt/pages/home_new.dart';
import 'package:skillcourt/pages/login_screen.dart';
import 'package:skillcourt/pages/pad_config.dart';
import 'package:skillcourt/pages/pad_manager.dart';
import 'package:skillcourt/pages/play_setup/play_setup.dart';
import 'package:skillcourt/pages/play_setup/play_timer/play_timer_page.dart';
import 'package:skillcourt/pages/play_setup/save_game/save_game.dart';
import 'package:skillcourt/pages/player_list.dart';
import 'package:skillcourt/pages/player_registration.dart';
import 'package:skillcourt/pages/settings/settings.dart';
import 'package:skillcourt/pages/start_game.dart';
import 'package:skillcourt/pages/station_creation_form.dart';
import 'package:skillcourt/pages/statistics.dart';
import 'package:skillcourt/pages/user_registration.dart';
import 'package:skillcourt/game_session_model.dart';
import 'package:skillcourt/widgets/brightness_button.dart';
import 'package:skillcourt/widgets/faq_page.dart';
import 'package:skillcourt/widgets/color_picker.dart';
import '../pages/play_setup/play_setup_item.dart';

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final args = settings.arguments;

    // Define your routes
    final appRoutes = <String, WidgetBuilder>{
      BrightnessButton.routeName: (context) => BrightnessButton(),
      FAQPage.routeName: (context) => FAQPage(),
      ColorPicker.routeName: (context) => ColorPicker(),
      LoginScreen.routeName: (context) => const LoginScreen(),
      ClubRegistration.routeName: (context) => const ClubRegistration(),
      PlayerRegistration.routeName: (context) => const PlayerRegistration(),
      GroupRegistration.routeName: (context) => const GroupRegistration(),
      ConfigPage.routeName: (context) => const ConfigPage(),
      StationCreationForm.routeName: (context) => const StationCreationForm(),
      PadConfigPage.routeName: (context) => const PadConfigPage(),
      PlayerList.routeName: (context) => const PlayerList(),
      GroupList.routeName: (context) => const GroupList(),
      ClubList.routeName: (context) => const ClubList(),
      StartGame.routeName: (context) =>
          StartGame(gameSessionModel: Provider.of<GameSessionModel>(context)),
      StatisticsPage.routeName: (context) => const StatisticsPage(),
      UserRegistration.routeName: (context) => const UserRegistration(),
      PadManagerPage.routeName: (context) => const PadManagerPage(),
      SettingsPage.routeName: (context) => SettingsPage(),
      HomePageNew.routeName: (context) => const HomePageNew(),
      PlaySetUp.routeName: (context) => const PlaySetUp(),
      SaveGame.routeName: (context) =>  SaveGame(playSetUpItem: args as PlaySetUpItem,),
      PlayTimerPage.routeName: (context)=> PlayTimerPage(playSetUpItem: args as PlaySetUpItem)
    };

    // Use the map to handle route generation
    final builder = appRoutes[settings.name];
    if (builder != null) {
      if (args != null) {
        return MaterialPageRoute(builder: (context) => builder(context));
      } else {
        return MaterialPageRoute(builder: (context) => builder(context));
      }
    } else {
      return _errorRoute();
    }
  }

  static Route<dynamic> _errorRoute() {
    // Handle the case where the route is not found
    return MaterialPageRoute(
      builder: (context) => Scaffold(
        appBar: AppBar(
          title: Text('Error'),
        ),
        body: Center(
          child: Text('Error: Page not found.'),
        ),
      ),
    );
  }
}
