import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
import 'package:syncrow_web/pages/device_managment/shared/batch_control/factory_reset.dart';
// import 'package:syncrow_web/pages/device_managment/shared/batch_control/firmware_update.dart';
import 'package:syncrow_web/pages/device_managment/shared/sensors_widgets/presence_update_data.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/wall_bloc.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/wall_event.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/wall_state.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/factories/wall_sensor_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/wall_sensor/model/wall_sensor_model.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

class WallSensorBatchControlView extends StatelessWidget with HelperResponsiveLayout {
  const WallSensorBatchControlView({super.key, required this.devicesIds});

  final List<String> devicesIds;

  @override
  Widget build(BuildContext context) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return BlocProvider(
      create: (context) => WallSensorBlocFactory.create(deviceId: devicesIds.first)
        ..add(WallSensorFetchBatchStatusEvent(devicesIds)),
      child: BlocBuilder<WallSensorBloc, WallSensorState>(
        builder: (context, state) {
          if (state is WallSensorLoadingInitialState || state is DeviceReportsLoadingState) {
            return const Center(child: CircularProgressIndicator());
          } else if (state is WallSensorUpdateState) {
            return _buildGridView(context, state.wallSensorModel, isExtraLarge, isLarge, isMedium);
          } else if (state is DeviceReportsFailedState) {
            final model = context.read<WallSensorBloc>().deviceStatus;
            return _buildGridView(context, model, isExtraLarge, isLarge, isMedium);
          }
          return const Center(child: Text('Error fetching status'));
        },
      ),
    );
  }

  Widget _buildGridView(
      BuildContext context, WallSensorModel model, bool isExtraLarge, bool isLarge, bool isMedium) {
    return GridView(
      padding: const EdgeInsets.symmetric(horizontal: 50, vertical: 20),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: isLarge || isExtraLarge
            ? 3
            : isMedium
                ? 2
                : 1,
        mainAxisExtent: 140,
        crossAxisSpacing: 12,
        mainAxisSpacing: 12,
      ),
      children: [
        PresenceUpdateData(
          value: model.motionSensitivity.toDouble(),
          title: 'Motion Detection Sensitivity:',
          minValue: 1,
          maxValue: 5,
          steps: 1,
          action: (int value) {
            context.read<WallSensorBloc>().add(
                  WallSensorBatchControlEvent(
                    deviceIds: devicesIds,
                    code: 'motion_sensitivity_value',
                    value: value,
                  ),
                );
          },
        ),
        PresenceUpdateData(
          value: model.motionlessSensitivity.toDouble(),
          title: 'Motionless Detection Sensitivity:',
          minValue: 1,
          maxValue: 5,
          steps: 1,
          action: (int value) => context.read<WallSensorBloc>().add(
                WallSensorBatchControlEvent(
                  deviceIds: devicesIds,
                  code: 'motionless_sensitivity',
                  value: value,
                ),
              ),
        ),
        PresenceUpdateData(
            value: model.noBodyTime.toDouble(),
            title: 'Nobody Time:',
            minValue: 10,
            maxValue: 10000,
            steps: 1,
            description: 'sec',
            action: (int value) => context.read<WallSensorBloc>().add(WallSensorBatchControlEvent(
                  deviceIds: devicesIds,
                  code: 'no_one_time',
                  value: value,
                ))),
        PresenceUpdateData(
          value: model.farDetection.toDouble(),
          title: 'Far Detection:',
          minValue: 75,
          maxValue: 600,
          steps: 75,
          description: 'cm',
          action: (int value) => context.read<WallSensorBloc>().add(
                WallSensorBatchControlEvent(
                  deviceIds: devicesIds,
                  code: 'far_detection',
                  value: value,
                ),
              ),
        ),
        // FirmwareUpdateWidget(deviceId: devicesIds.first, version: 2),
        FactoryResetWidget(
          callFactoryReset: () {
            context.read<WallSensorBloc>().add(
                  WallSensorFactoryResetEvent(
                    deviceId: devicesIds.first,
                    factoryReset: FactoryResetModel(devicesUuid: devicesIds),
                  ),
                );
          },
        ),
      ],
    );
  }
}
