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/ceiling_sensor/bloc/ceiling_bloc.dart';
import 'package:syncrow_web/pages/device_managment/ceiling_sensor/bloc/ceiling_event.dart';
import 'package:syncrow_web/pages/device_managment/ceiling_sensor/bloc/ceiling_state.dart';
import 'package:syncrow_web/pages/device_managment/ceiling_sensor/factories/ceiling_sensor_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/ceiling_sensor/model/ceiling_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_space_type.dart';
import 'package:syncrow_web/pages/device_managment/shared/sensors_widgets/presence_update_data.dart';
import 'package:syncrow_web/pages/device_managment/shared/sensors_widgets/presense_nobody_time.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

class CeilingSensorBatchControlView extends StatelessWidget with HelperResponsiveLayout {
  const CeilingSensorBatchControlView({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) => CeilingSensorBlocFactory.create(
        deviceId: devicesIds.first,
      )..add(CeilingFetchDeviceStatusEvent(devicesIds)),
      child: BlocBuilder<CeilingSensorBloc, CeilingSensorState>(
        builder: (context, state) {
          if (state is CeilingLoadingInitialState || state is CeilingReportsLoadingState) {
            return const Center(child: CircularProgressIndicator());
          } else if (state is CeilingUpdateState) {
            return _buildGridView(
                context, state.ceilingSensorModel, isExtraLarge, isLarge, isMedium);
          }
          return const Center(child: Text('Error fetching status'));
        },
      ),
    );
  }

  Widget _buildGridView(BuildContext context, CeilingSensorModel model, bool isExtraLarge,
      bool isLarge, bool isMedium) {
    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: [
        PresenceSpaceType(
          description: 'Space Type',
          value: model.spaceType,
          action: (String value) => context.read<CeilingSensorBloc>().add(
                CeilingBatchControlEvent(
                  deviceIds: devicesIds,
                  code: 'scene',
                  value: value,
                ),
              ),
        ),
        PresenceUpdateData(
          value: model.sensitivity.toDouble(),
          title: 'Sensitivity:',
          minValue: 1,
          maxValue: 10,
          steps: 1,
          action: (int value) {
            context.read<CeilingSensorBloc>().add(
                  CeilingBatchControlEvent(
                    deviceIds: devicesIds,
                    code: 'sensitivity',
                    value: value,
                  ),
                );
          },
        ),
        PresenceUpdateData(
          value: model.maxDistance.toDouble(),
          title: 'Maximum Distance:',
          minValue: 0,
          maxValue: 500,
          steps: 50,
          description: 'm',
          action: (int value) => context.read<CeilingSensorBloc>().add(
                CeilingBatchControlEvent(
                  deviceIds: devicesIds,
                  code: 'moving_max_dis',
                  value: value,
                ),
              ),
        ),
        PresenceNoBodyTime(
          value: model.noBodyTime,
          title: 'Nobody Time:',
          description: '',
          action: (String value) => context.read<CeilingSensorBloc>().add(
                CeilingBatchControlEvent(
                  deviceIds: devicesIds,
                  code: 'nobody_time',
                  value: value,
                ),
              ),
        ),
        FactoryResetWidget(
          callFactoryReset: () {
            context.read<CeilingSensorBloc>().add(
                  CeilingFactoryResetEvent(
                    devicesId: devicesIds.first,
                    factoryResetModel: FactoryResetModel(devicesUuid: devicesIds),
                  ),
                );
          },
        ),
      ],
    );
  }
}
