import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/shared/device_controls_container.dart';
import 'package:syncrow_web/pages/device_managment/shared/increament_decreament.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class PresenceNoBodyTime extends StatefulWidget {
  const PresenceNoBodyTime({
    super.key,
    required this.title,
    required this.value,
    required this.action,
    this.description,
  });

  final String title;
  final String value;
  final Function(String) action;
  final String? description;

  @override
  State<PresenceNoBodyTime> createState() => _PresenceUpdateDataState();
}

class _PresenceUpdateDataState extends State<PresenceNoBodyTime> {
  late String _currentValue;
  late String _numericValue;
  late String _unit;

  final List<String> nobodyTimeRange = [
    'none',
    '10s',
    '30s',
    '1min',
    '2min',
    '5min',
    '10min',
    '30min',
    '1hour'
  ];

  @override
  void initState() {
    super.initState();
    _currentValue = widget.value;
    _numericValue = _extractNumericValue(_currentValue);
    _unit = _extractUnit(_currentValue);
  }

  String _extractNumericValue(String value) {
    if (value == 'none') return '0';
    return value.replaceAll(RegExp(r'[a-zA-Z]'), '').trim();
  }

  String _extractUnit(String value) {
    if (value == 'none') return '';
    if (value.endsWith('s')) return 's';
    if (value.endsWith('min')) return 'min';
    if (value.endsWith('hour')) return 'hr';
    return '';
  }

  void _onValueChanged(String newValue) {
    setState(() {
      _currentValue = newValue;
      _numericValue = _extractNumericValue(newValue);
      _unit = _extractUnit(newValue);
    });
    widget.action(newValue);
  }

  void _incrementValue() {
    int currentIndex = nobodyTimeRange.indexOf(_currentValue);
    if (currentIndex < nobodyTimeRange.length - 1) {
      String newValue = nobodyTimeRange[currentIndex + 1];
      _onValueChanged(newValue);
    }
  }

  void _decrementValue() {
    int currentIndex = nobodyTimeRange.indexOf(_currentValue);
    if (currentIndex > 0) {
      String newValue = nobodyTimeRange[currentIndex - 1];
      _onValueChanged(newValue);
    }
  }

  @override
  void didUpdateWidget(PresenceNoBodyTime oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.value != widget.value) {
      setState(() {
        _currentValue = widget.value;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return DeviceControlsContainer(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            widget.title,
            style: Theme.of(context).textTheme.bodySmall!.copyWith(
                color: ColorsManager.blackColor,
                fontWeight: FontWeight.w400,
                fontSize: 10),
          ),
          IncrementDecrementWidget(
            value: _numericValue,
            description: _unit,
            descriptionColor: ColorsManager.blackColor,
            onIncrement: _incrementValue,
            onDecrement: _decrementValue,
          ),
        ],
      ),
    );
  }
}
