import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/font_manager.dart';

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

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

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

class DoubleParameterControlState extends State<DoubleParameterControl> {
  late int _value;

  @override
  void initState() {
    super.initState();
    _value = widget.value.clamp(widget.min, widget.max);
  }

  int? _getDivisions(int min, int max, int step) {
    final range = max - min;
    final divisions = (range / step).floor();
    return divisions > 0 ? divisions : null;
  }

  @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: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TitleMedium(
                    text: _value.toStringAsFixed(0),
                    style: context.titleMedium.copyWith(
                      color: Colors.black,
                      fontWeight: FontsManager.bold,
                    ),
                  ),
                  TitleMedium(
                    text: widget.measurement.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 - 50).clamp(widget.min, widget.max);
                      });
                    },
                    icon: const Icon(
                      Icons.remove,
                      color: Colors.grey,
                    ),
                  ),
                  Flexible(
                    child: Slider(
                      value: _value.toDouble(), 
                      min: widget.min.toDouble(), 
                      max: widget.max.toDouble(), 
                      divisions: _getDivisions(
                          widget.min, widget.max, 50), 
                      onChanged: (value) {
                        setState(() {
                          _value =
                              value.round(); 
                        });
                      },
                      label: _value.toStringAsFixed(0),
                      inactiveColor: ColorsManager.greyColor,
                    ),
                  ),
                  IconButton(
                    onPressed: () {
                      setState(() {
                        _value = (_value + 50).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;
                    }
                  },
                  child: Center(
                    child: BodyMedium(
                      text: 'Confirm',
                      style: context.bodyMedium.copyWith(
                          color: ColorsManager.primaryColorWithOpacity),
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
