import 'package:didvan/models/day_time.dart';
import 'package:didvan/views/widgets/didvan/toggle_button_time.dart';
import 'package:flutter/material.dart';

class TimeSliderPicker extends StatefulWidget {
  final DayTime selectedTime;
  final ValueChanged<DayTime> onTimeChanged;
  final bool isDisabled;

  const TimeSliderPicker({
    Key? key,
    required this.selectedTime,
    required this.onTimeChanged,
    this.isDisabled = false,
  }) : super(key: key);

  @override
  State<TimeSliderPicker> createState() => _TimeSliderPickerState();
}

class _TimeSliderPickerState extends State<TimeSliderPicker> {
  bool _isHourMode = true;

  int get _currentHour24 {
    int hour = int.tryParse(widget.selectedTime.hour) ?? 12;
    if (widget.selectedTime.meridiem == Meridiem.PM && hour != 12) hour += 12;
    if (widget.selectedTime.meridiem == Meridiem.AM && hour == 12) hour = 0;
    return hour;
  }

  int get _currentMinute => int.tryParse(widget.selectedTime.minute) ?? 0;

  void _updateTime(int newHour, int newMinute) {
    Meridiem newMeridiem = newHour >= 12 ? Meridiem.PM : Meridiem.AM;
    int hour12 = newHour > 12 ? newHour - 12 : (newHour == 0 ? 12 : newHour);

    widget.onTimeChanged(DayTime(
      hour: hour12.toString().padLeft(2, '0'),
      minute: newMinute.toString().padLeft(2, '0'),
      meridiem: newMeridiem,
    ));
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Opacity(
      opacity: widget.isDisabled ? 0.5 : 1.0,
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 8),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Text("دقیقه", style: theme.textTheme.bodySmall),
                Text("ساعت", style: theme.textTheme.bodySmall),
              ],
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              DidvanToggleButtonTime(
                title: _currentMinute.toString().padLeft(2, '0'),
                active: !_isHourMode,
                onTap: widget.isDisabled
                    ? () {}
                    : () => setState(() => _isHourMode = false),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 16),
                child: Text(":",
                    style: theme.textTheme.headlineMedium
                        ?.copyWith(fontWeight: FontWeight.bold)),
              ),
              DidvanToggleButtonTime(
                title: _currentHour24.toString().padLeft(2, '0'),
                active: _isHourMode,
                onTap: widget.isDisabled
                    ? () {}
                    : () => setState(() => _isHourMode = true),
              ),
            ],
          ),
          const SizedBox(height: 24),
          Directionality(
            textDirection: TextDirection.ltr,
            child: SliderTheme(
              data: SliderThemeData(
                trackHeight: 8,
                activeTrackColor: theme.colorScheme.primary,
                inactiveTrackColor: theme.colorScheme.outline.withOpacity(0.2),
                thumbColor: theme.colorScheme.primary,
                overlayColor: theme.colorScheme.primary.withOpacity(0.2),
                thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 12),
                valueIndicatorTextStyle: theme.textTheme.bodyMedium?.copyWith(
                  color: theme.colorScheme.onPrimary,
                ),
              ),
              child: Slider(
                value: _isHourMode
                    ? _currentHour24.toDouble()
                    : _currentMinute.toDouble(),
                min: 0,
                max: _isHourMode ? 23 : 59,
                divisions: _isHourMode ? 23 : 59,
                label: (_isHourMode ? _currentHour24 : _currentMinute)
                    .toString()
                    .padLeft(2, '0'),
                onChanged: widget.isDisabled
                    ? null
                    : (value) {
                        if (_isHourMode) {
                          _updateTime(value.round(), _currentMinute);
                        } else {
                          _updateTime(_currentHour24, value.round());
                        }
                      },
              ),
            ),
          ),
        ],
      ),
    );
  }
}
