part of '../wall_sensor/wall_sensor_interface.dart';

class ParameterControlDialog extends StatefulWidget {
  final String title;
  final DeviceModel sensor;
  final int value;
  final int min;
  final int max;

  const ParameterControlDialog({
    super.key,
    required this.title,
    required this.sensor,
    required this.value,
    required this.min,
    required this.max,
  });

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

class ParameterControlDialogState extends State<ParameterControlDialog> {
  late int _value;

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

  @override
  Widget build(BuildContext context) {
    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.toString(),
                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 - 1).clamp(widget.min, widget.max);
                      });
                    },
                    icon: const Icon(
                      Icons.remove,
                      color: Colors.grey,
                    ),
                  ),
                  Flexible(
                    child: Slider(
                      value: _value.toDouble(),
                      onChanged: (value) {
                        setState(() {
                          _value = value.toInt();
                        });
                      },
                      min: widget.min.toDouble(),
                      max: widget.max.toDouble(),
                      label: _value.toString(),
                      inactiveColor: ColorsManager.greyColor,
                    ),
                  ),
                  IconButton(
                    onPressed: () {
                      setState(() {
                        _value = (_value + 1).clamp(widget.min, widget.max);
                      });
                    },
                    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: () {
                    Navigator.pop(context, _value);
                    if (widget.sensor.isOnline == null) {
                      CustomSnackBar.displaySnackBar('The device is offline');
                      return;
                    }
                    if (!widget.sensor.isOnline!) {
                      CustomSnackBar.displaySnackBar('The device is offline');
                      return;
                    }

                    // DevicesCubit.getInstance().deviceControl(
                    //     DeviceControlModel(
                    //         deviceId: widget.sensor.uuid,
                    //         code: widget.controlCode,
                    //         value: widget.value),
                    //     widget.sensor.uuid ?? '');
                  },
                  child: Center(
                    child: BodyMedium(
                      text: 'Confirm',
                      style:
                          context.bodyMedium.copyWith(color: ColorsManager.primaryColorWithOpacity),
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
