import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class WorkingHoursDialog extends StatefulWidget {
  const WorkingHoursDialog({super.key});

  @override
  State<WorkingHoursDialog> createState() => _WorkingHoursDialogState();
}

class _WorkingHoursDialogState extends State<WorkingHoursDialog> {
  final Map<String, Map<String, dynamic>> _days = {
    'شنبه': {'english': 'Saturday', 'isSelected': false},
    'یکشنبه': {'english': 'Sunday', 'isSelected': false},
    'دوشنبه': {'english': 'Monday', 'isSelected': false},
    'سه‌شنبه': {'english': 'Tuesday', 'isSelected': false},
    'چهارشنبه': {'english': 'Wednesday', 'isSelected': false},
    'پنجشنبه': {'english': 'Thursday', 'isSelected': false},
    'جمعه': {'english': 'Friday', 'isSelected': false},
  };

  TimeOfDay? _startTime;
  TimeOfDay? _endTime;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text("تنظیم روز و ساعت کاری", textAlign: TextAlign.center),
      content: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text("روزهای فعالیت را انتخاب کنید:"),
            const SizedBox(height: 8),
            Wrap(
              spacing: 4.0,
              runSpacing: 8.0,
              children: _days.keys.map((dayName) {
                return ChoiceChip(
                  label: Text(dayName),
                  selected: _days[dayName]!['isSelected'],
                  onSelected: (isSelected) {
                    setState(() {
                      _days[dayName]!['isSelected'] = isSelected;
                    });
                  },
                );
              }).toList(),
            ),
            const Divider(height: 32),
            const Text("بازه ساعت کاری را مشخص کنید:"),
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildTimePicker("پایان", _endTime, (time) {
                  setState(() => _endTime = time);
                }),
                _buildTimePicker("شروع", _startTime, (time) {
                  setState(() => _startTime = time);
                }),
              ],
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: const Text("انصراف"),
        ),
        ElevatedButton(
          onPressed: () {
            if (_startTime != null && _endTime != null) {
              final selectedDays = _days.entries
                  .where((entry) => entry.value['isSelected'])
                  .map((entry) => entry.value['english'] as String)
                  .toList();
              
              if (selectedDays.isNotEmpty) {
                final format = NumberFormat("00");
                final startTimeString = "${format.format(_startTime!.hour)}:${format.format(_startTime!.minute)}";
                final endTimeString = "${format.format(_endTime!.hour)}:${format.format(_endTime!.minute)}";

                Navigator.of(context).pop({
                  'days': selectedDays,
                  'startTime': startTimeString,
                  'endTime': endTimeString,
                });
              }
            }
          },
          child: const Text("تایید"),
        ),
      ],
    );
  }

  Widget _buildTimePicker(String title, TimeOfDay? time, Function(TimeOfDay) onTimeChanged) {
    return Column(
      children: [
        Text(title),
        const SizedBox(height: 8),
        OutlinedButton(
          onPressed: () async {
            final newTime = await showTimePicker(
              context: context,
              initialTime: time ?? TimeOfDay.now(),
            );
            if (newTime != null) {
              onTimeChanged(newTime);
            }
          },
          child: Text(time?.format(context) ?? "انتخاب"),
        ),
      ],
    );
  }
}