import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/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/web_layout/web_scaffold.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => DeviceManagementBloc()..add(FetchDevices()),
      child: WebScaffold(
        appBarTitle: FittedBox(
          child: Text(
            'Device Management',
            style: Theme.of(context).textTheme.headlineLarge,
          ),
        ),
        rightBody: const NavigateHomeGridView(),
        enableMenuSideba: isLargeScreenSize(context),
        scaffoldBody: BlocBuilder<DeviceManagementBloc, DeviceManagementState>(
          builder: (context, state) {
            if (state is DeviceManagementLoading) {
              return const Center(child: CircularProgressIndicator());
            } else if (state is DeviceManagementLoaded ||
                state is DeviceManagementFiltered) {
              final devices = state is DeviceManagementLoaded
                  ? state.devices
                  : (state as DeviceManagementFiltered).filteredDevices;

              return DeviceManagementBody(devices: devices);
            } else {
              return const Center(child: Text('Error fetching Devices'));
            }
          },
        ),
      ),
    );
  }
}
