import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/model/community_model.dart';
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/app_layout/view/app_layout.dart';
import 'package:syncrow_app/features/auth/view/login_view.dart';
import 'package:syncrow_app/features/auth/view/otp_view.dart';
import 'package:syncrow_app/features/auth/view/sign_up_view.dart';
import 'package:syncrow_app/features/dashboard/view/dashboard_view.dart';
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_event.dart';
import 'package:syncrow_app/features/layout/view/layout_view.dart';
import 'package:syncrow_app/features/menu/view/menu_view.dart';
import 'package:syncrow_app/features/menu/view/widgets/create_home/create_home_view.dart';
import 'package:syncrow_app/features/menu/view/widgets/profile/profile_view.dart';
import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_bloc.dart';
import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_event.dart';
import 'package:syncrow_app/features/scene/view/device_functions_view.dart';
import 'package:syncrow_app/features/scene/view/scene_auto_settings.dart';
import 'package:syncrow_app/features/scene/view/scene_rooms_tabbar.dart';
import 'package:syncrow_app/features/scene/view/scene_tasks_view.dart';
import 'package:syncrow_app/features/scene/view/scene_view.dart';
import 'package:syncrow_app/features/scene/view/smart_automation_select_route.dart';
import 'package:syncrow_app/features/splash/view/splash_view.dart';

import 'routing_constants.dart';

class Router {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case Routes.splash:
        return MaterialPageRoute(
            builder: (_) => const SplashView(), settings: settings);

      // case Routes.devicesRoute:
      //   return MaterialPageRoute(
      //       builder: (_) => const DevicesView(), settings: settings);

      case Routes.profileRoute:
        return MaterialPageRoute(
            builder: (_) => const ProfileView(), settings: settings);

      case Routes.sceneRoute:
        return MaterialPageRoute(
            builder: (_) => const SceneView(), settings: settings);

      case Routes.layoutRoute:
        return MaterialPageRoute(
            builder: (_) => const LayoutPage(), settings: settings);

      case Routes.authLogin:
        return MaterialPageRoute(
            builder: (_) => const LoginView(), settings: settings);

      case Routes.otpRoute:
        return MaterialPageRoute(
            builder: (_) => const OtpView(), settings: settings);

      case Routes.authSignUp:
        return MaterialPageRoute(
            builder: (_) => const SignUpView(), settings: settings);

      case Routes.dashboardRoute:
        return MaterialPageRoute(
            builder: (_) => const DashboardView(), settings: settings);

      case Routes.homeRoute:
        return MaterialPageRoute(
            builder: (_) => const AppLayout(), settings: settings);

      case Routes.menuRoute:
        return MaterialPageRoute(
            builder: (_) => const MenuView(), settings: settings);

      case Routes.createUnit:
        return MaterialPageRoute(
            builder: (_) => const CreateUnitView(), settings: settings);
      case Routes.sceneTasksRoute:
        return MaterialPageRoute(
            builder: (_) => const SceneTasksView(), settings: settings);
      case Routes.sceneControlDevicesRoute:
        return MaterialPageRoute(
            builder: (_) => MultiBlocProvider(
                  providers: [
                    BlocProvider(
                      create: (BuildContext context) =>
                          DeviceManagerBloc()..add(FetchAllDevices()),
                    ),
                    BlocProvider(
                      create: (BuildContext context) =>
                          TabBarBloc(context.read<DeviceManagerBloc>())
                            ..add(TabBarTabChangedEvent(
                                selectedIndex: 0,
                                roomId: '-1',
                                unit: SpaceModel(
                                    id: '-1',
                                    name: '',
                                    community: Community(
                                      uuid: '-1',
                                      name: '',
                                    )))),
                    ),
                  ],
                  child: const SceneRoomsTabBarDevicesView(),
                ),
            settings: settings);
      case Routes.deviceFunctionsRoute:
        return MaterialPageRoute(
          builder: (_) => DeviceFunctionsView(),
          settings: settings,
        );
      case Routes.sceneAutoSettingsRoute:
        return MaterialPageRoute(
          builder: (_) => const SceneAutoSettings(),
          settings: settings,
        );

      case Routes.smartAutomationSelectRoute:
        return MaterialPageRoute(
          builder: (_) => const SmartAutomationSelectView(),
          settings: settings,
        );
      default:
        return MaterialPageRoute(
          builder: (_) => Scaffold(
            body: Center(child: Text('No route defined for ${settings.name}')),
          ),
        );
    }
  }
}
