import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/bloc/device_mgmt_bloc/device_managment_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/widgets/device_managment_body.dart';
import 'package:syncrow_web/pages/device_managment/shared/navigate_home_grid_view.dart';
import 'package:syncrow_web/pages/routines/bloc/create_routine_bloc/create_routine_bloc.dart';
import 'package:syncrow_web/pages/routines/bloc/create_routine_bloc/create_routine_event.dart';
import 'package:syncrow_web/pages/routines/bloc/routine_bloc/routine_bloc.dart';
import 'package:syncrow_web/pages/routines/view/create_new_routine_view.dart';
import 'package:syncrow_web/pages/routines/view/routines_view.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';
import 'package:syncrow_web/utils/theme/responsive_text_theme.dart';
import 'package:syncrow_web/web_layout/web_scaffold.dart';

class DeviceManagementPage extends StatefulWidget with HelperResponsiveLayout {
  const DeviceManagementPage({super.key});

  @override
  State<DeviceManagementPage> createState() => _DeviceManagementPageState();
}

class _DeviceManagementPageState extends State<DeviceManagementPage> {

@override
  void initState() {
    context.read<SpaceTreeBloc>().add(InitialEvent());
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
          create: (context) =>
              DeviceManagementBloc()..add(FetchDevices(context)),
        ),
      ],
      child: WebScaffold(
        appBarTitle: Text(
          'Device Management',
          style: ResponsiveTextTheme.of(context).deviceManagementTitle,
        ),
        centerBody:
            BlocBuilder<RoutineBloc, RoutineState>(builder: (context, state) {
          return Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: null,
                ),
                onPressed: !state.routineTab
                    ? null
                    : () {
                        BlocProvider.of<CreateRoutineBloc>(context)
                            .add(const ResetSelectedEvent());

                        context.read<RoutineBloc>().add(
                            const TriggerSwitchTabsEvent(isRoutineTab: false));
                        context
                            .read<DeviceManagementBloc>()
                            .add(FetchDevices(context));
                      },
                child: Text(
                  'Devices',
                  style: context.textTheme.titleMedium?.copyWith(
                    color: !state.routineTab
                        ? ColorsManager.whiteColors
                        : ColorsManager.grayColor,
                    fontWeight:
                        !state.routineTab ? FontWeight.w700 : FontWeight.w400,
                  ),
                ),
              ),
              TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: null,
                ),
                onPressed: state.routineTab
                    ? null
                    : () {
                        BlocProvider.of<CreateRoutineBloc>(context)
                            .add(const ResetSelectedEvent());

                        context.read<RoutineBloc>().add(
                            const TriggerSwitchTabsEvent(isRoutineTab: true));
                      },
                child: Text(
                  'Routines',
                  style: context.textTheme.titleMedium?.copyWith(
                    color: state.routineTab
                        ? ColorsManager.whiteColors
                        : ColorsManager.grayColor,
                    fontWeight:
                        state.routineTab ? FontWeight.w700 : FontWeight.w400,
                  ),
                ),
              ),
            ],
          );
        }),
        rightBody: const NavigateHomeGridView(),
        scaffoldBody:
            BlocBuilder<RoutineBloc, RoutineState>(builder: (context, state) {
          if (state.routineTab) {
            return const RoutinesView();
          }
          if (state.createRoutineView) {
            return const CreateNewRoutineView();
          }

          return BlocBuilder<DeviceManagementBloc, DeviceManagementState>(
            builder: (context, deviceState) {
              if (deviceState is DeviceManagementLoading) {
                return const DeviceManagementBody(devices: []);
              } else if (deviceState is DeviceManagementLoaded) {
                return DeviceManagementBody(devices: deviceState.devices);
              } else if (deviceState is DeviceManagementFiltered) {
                return DeviceManagementBody(
                    devices: deviceState.filteredDevices);
              } else {
                return const DeviceManagementBody(devices: []);
              }
            },
          );
        }),
      ),
    );
  }
}
