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/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart';
import 'package:syncrow_web/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/flush_mounted_presence_sensor/models/flush_mounted_presence_sensor_model.dart';
import 'package:syncrow_web/pages/device_managment/shared/batch_control/factory_reset.dart';
import 'package:syncrow_web/pages/device_managment/shared/sensors_widgets/presence_update_data.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

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

  final List<String> devicesIds;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => FlushMountedPresenceSensorBlocFactory.create(
        deviceId: devicesIds.first,
      )..add(FlushMountedPresenceSensorFetchBatchStatusEvent(devicesIds)),
      child: BlocBuilder<FlushMountedPresenceSensorBloc,
          FlushMountedPresenceSensorState>(
        builder: (context, state) {
          if (state is FlushMountedPresenceSensorLoadingInitialState ||
              state is FlushMountedPresenceSensorDeviceReportsLoadingState) {
            return const Center(child: CircularProgressIndicator());
          } else if (state is FlushMountedPresenceSensorUpdateState) {
            return _buildGridView(context, state.model);
          }
          return const Center(child: Text('Error fetching status'));
        },
      ),
    );
  }

  Widget _buildGridView(
    BuildContext context,
    FlushMountedPresenceSensorModel model,
  ) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return GridView(
      padding: const EdgeInsets.symmetric(horizontal: 50),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: isLarge || isExtraLarge
            ? 3
            : isMedium
                ? 2
                : 1,
        mainAxisExtent: 140,
        crossAxisSpacing: 12,
        mainAxisSpacing: 12,
      ),
      children: [
        PresenceUpdateData(
          value: model.sensitivity.toDouble(),
          title: 'Sensitivity:',
          minValue: 0,
          maxValue: 9,
          steps: 1,
          action: (int value) => context.read<FlushMountedPresenceSensorBloc>().add(
                FlushMountedPresenceSensorBatchControlEvent(
                  deviceIds: devicesIds,
                  code: FlushMountedPresenceSensorModel.codeSensitivity,
                  value: value,
                ),
              ),
        ),
        PresenceUpdateData(
          value: (model.nearDetection / 100).clamp(0.0, double.infinity),
          title: 'Nearest Detect Dist:',
          description: 'm',
          minValue: 0.0,
          maxValue: 9.5,
          steps: 0.1,
          valuesPercision: 1,
          action: (double value) =>
              context.read<FlushMountedPresenceSensorBloc>().add(
                    FlushMountedPresenceSensorBatchControlEvent(
                      deviceIds: devicesIds,
                      code: FlushMountedPresenceSensorModel.codeNearDetection,
                      value: (value * 100).toInt(),
                    ),
                  ),
        ),
        PresenceUpdateData(
          value: (model.farDetection / 100).clamp(0.0, double.infinity),
          title: 'Max Detect Dist:',
          description: 'm',
          minValue: 0.0,
          maxValue: 9.5,
          steps: 0.1,
          valuesPercision: 1,
          action: (double value) =>
              context.read<FlushMountedPresenceSensorBloc>().add(
                    FlushMountedPresenceSensorBatchControlEvent(
                      deviceIds: devicesIds,
                      code: FlushMountedPresenceSensorModel.codeFarDetection,
                      value: (value * 100).toInt(),
                    ),
                  ),
        ),
        PresenceUpdateData(
          value: model.sensiReduce.toDouble(),
          title: 'Trigger Level:',
          minValue: 0,
          maxValue: 3,
          steps: 1,
          action: (int value) => context.read<FlushMountedPresenceSensorBloc>().add(
                FlushMountedPresenceSensorBatchControlEvent(
                  deviceIds: devicesIds,
                  code: FlushMountedPresenceSensorModel.codeSensiReduce,
                  value: value,
                ),
              ),
        ),
        PresenceUpdateData(
          value: model.occurDistReduce.toDouble(),
          title: 'Indent Level:',
          minValue: 0,
          maxValue: 3,
          steps: 1,
          action: (int value) => context.read<FlushMountedPresenceSensorBloc>().add(
                FlushMountedPresenceSensorBatchControlEvent(
                  deviceIds: devicesIds,
                  code: FlushMountedPresenceSensorModel.codeOccurDistReduce,
                  value: value,
                ),
              ),
        ),
        PresenceUpdateData(
          value: (model.presenceDelay / 10).toDouble(),
          title: 'Target Confirm Time:',
          description: 's',
          minValue: 0.0,
          maxValue: 0.5,
          steps: 0.1,
          valuesPercision: 1,
          action: (double value) =>
              context.read<FlushMountedPresenceSensorBloc>().add(
                    FlushMountedPresenceSensorBatchControlEvent(
                      deviceIds: devicesIds,
                      code: FlushMountedPresenceSensorModel.codePresenceDelay,
                      value: (value * 10).toInt(),
                    ),
                  ),
        ),
        PresenceUpdateData(
          value: ((model.noneDelay / 10).toDouble()),
          description: 's',
          title: 'Disappe Delay:',
          minValue: 20,
          maxValue: 300,
          steps: 1,
          action: (double value) =>
              context.read<FlushMountedPresenceSensorBloc>().add(
                    FlushMountedPresenceSensorBatchControlEvent(
                      deviceIds: devicesIds,
                      code: FlushMountedPresenceSensorModel.codeNoneDelay,
                      value: (value * 10).round(),
                    ),
                  ),
        ),
        FactoryResetWidget(
          callFactoryReset: () {
            context.read<FlushMountedPresenceSensorBloc>().add(
                  FlushMountedPresenceSensorFactoryResetEvent(
                    deviceId: devicesIds.first,
                    factoryReset: FactoryResetModel(devicesUuid: devicesIds),
                  ),
                );
          },
        ),
      ],
    );
  }
}
