part of 'flush_sensor_interface.dart';

class FlushParameterControlDialog extends StatefulWidget {
  final String title;
  final String? description;
  final DeviceModel sensor;
  final double value;
  final double min;
  final double max;
  final double step;

  const FlushParameterControlDialog({
    super.key,
    required this.title,
    required this.sensor,
    required this.value,
    required this.min,
    required this.max,
    required this.step,
    this.description = '',
  });

  @override
  FlushParameterControlDialogState createState() =>
      FlushParameterControlDialogState();
}

class FlushParameterControlDialogState
    extends State<FlushParameterControlDialog> {
  late double _value;

  @override
  void initState() {
    super.initState();
    _value = widget.value;
  }

  @override
  Widget build(BuildContext context) {
    final divisions = ((widget.max - widget.min) / widget.step).toInt();
    final decimalPlaces = widget.step % 1 == 0 ? 0 : 1;
    return Dialog(
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
        ),
        padding: const EdgeInsets.only(top: 20),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            BodyMedium(
              text: widget.title,
              style: context.bodyMedium.copyWith(
                color: ColorsManager.primaryColorWithOpacity,
                fontWeight: FontsManager.extraBold,
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(
                vertical: 15,
                horizontal: 50,
              ),
              child: Container(
                height: 1,
                width: double.infinity,
                color: ColorsManager.greyColor,
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(
                vertical: 10,
              ),
              child: TitleMedium(
                text:
                    "${_value.toStringAsFixed(decimalPlaces)} ${widget.description}",
                style: context.titleMedium.copyWith(
                  color: Colors.black,
                  fontWeight: FontsManager.bold,
                ),
              ),
            ),
            SizedBox(
              width: MediaQuery.sizeOf(context).width,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    onPressed: () {
                      setState(() {
                        _value = (_value - widget.step)
                            .clamp(widget.min, widget.max);
                        _value =
                            double.parse(_value.toStringAsFixed(decimalPlaces));
                      });
                    },
                    icon: const Icon(Icons.remove, color: Colors.grey),
                  ),
                  Flexible(
                    child: Slider(
                      value: _value,
                      onChanged: (value) {
                        final newValue =
                            (value / widget.step).round() * widget.step;
                        setState(() {
                          _value = newValue.clamp(widget.min, widget.max);
                        });
                      },
                      min: widget.min,
                      max: widget.max,
                      divisions: divisions,
                      label: _value.toStringAsFixed(decimalPlaces),
                    ),
                  ),
                  IconButton(
                    onPressed: () {
                      setState(() {
                        _value = (_value + widget.step)
                            .clamp(widget.min, widget.max);
                        _value =
                            double.parse(_value.toStringAsFixed(decimalPlaces));
                      });
                    },
                    icon: const Icon(Icons.add, color: Colors.grey),
                  ),
                ],
              ),
            ),
            Container(
              height: 1,
              width: double.infinity,
              color: ColorsManager.greyColor,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                InkWell(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Center(
                    child: BodyMedium(
                      text: 'Cancel',
                      style: context.bodyMedium
                          .copyWith(color: ColorsManager.greyColor),
                    ),
                  ),
                ),
                Container(
                  height: 50,
                  width: 1,
                  color: ColorsManager.greyColor,
                ),
                InkWell(
                  onTap: () {
                     if (widget.sensor.isOnline == null || !widget.sensor.isOnline!) {
                      CustomSnackBar.displaySnackBar('The device is offline');
                      return;
         }
                    Navigator.pop(context, _value);
                  },
                  child: Center(
                    child: BodyMedium(
                      text: 'Confirm',
                      style: context.bodyMedium.copyWith(
                          color: ColorsManager.primaryColorWithOpacity),
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
