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

class LunchBreakDialog extends StatefulWidget {
  final VoidCallback onComplete;

  const LunchBreakDialog({super.key, required this.onComplete});

  @override
  State<LunchBreakDialog> createState() => _LunchBreakDialogState();
}

class _LunchBreakDialogState extends State<LunchBreakDialog> {
  int _step = 1;
  bool? _tookLunch;
  // ignore: unused_field
  String? _breakStart = '12:00pm';
  // ignore: unused_field
  String? _breakEnd = '12:30pm';
  // ignore: unused_field
  String? _noLunchReason;
  // ignore: unused_field
  String _additionalNotes = '';

  final List<String> _timeOptions = _generateTimeOptions();
  final List<String> _noLunchReasons = [
    'Unpredictable Workflows',
    'Poor Time Management',
    'Lack of coverage or short Staff',
    'No Lunch Area',
    'Other (Please specify)',
  ];

  static List<String> _generateTimeOptions() {
    List<String> options = [];
    for (int h = 0; h < 24; h++) {
      for (int m = 0; m < 60; m += 15) {
        final hour = h % 12 == 0 ? 12 : h % 12;
        final ampm = h < 12 ? 'am' : 'pm';
        final timeStr = '$hour:${m.toString().padLeft(2, '0')}$ampm';
        options.add(timeStr);
      }
    }
    return options;
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
      child: AnimatedSwitcher(
        duration: const Duration(milliseconds: 300),
        child: _buildCurrentStep(),
      ),
    );
  }

  Widget _buildCurrentStep() {
    switch (_step) {
      case 1:
        return _buildStep1();
      case 2:
        return _buildStep2();
      case 102: // 2b: No lunch reason
        return _buildStep2b();
      case 3:
        return _buildStep3();
      case 4:
        return _buildStep4();
      default:
        return const SizedBox.shrink();
    }
  }

  Widget _buildStep1() {
    return Padding(
      padding: const EdgeInsets.all(24),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 80,
            height: 80,
            decoration: BoxDecoration(
              color: Colors.grey.shade100,
              shape: BoxShape.circle,
            ),
            child: const Icon(
              LucideIcons.coffee,
              size: 40,
              color: Color(0xFF6A7382),
            ),
          ),
          const SizedBox(height: 24),
          const Text(
            "Did You Take\na Lunch?",
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 24,
              fontWeight: FontWeight.bold,
              color: Color(0xFF121826),
            ),
          ),
          const SizedBox(height: 24),
          Row(
            children: [
              Expanded(
                child: OutlinedButton(
                  onPressed: () {
                    setState(() {
                      _tookLunch = false;
                      _step = 102; // Go to No Lunch Reason
                    });
                  },
                  style: OutlinedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    side: BorderSide(color: Colors.grey.shade300),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  child: const Text(
                    "No",
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Color(0xFF121826),
                    ),
                  ),
                ),
              ),
              const SizedBox(width: 16),
              Expanded(
                child: ElevatedButton(
                  onPressed: () {
                    setState(() {
                      _tookLunch = true;
                      _step = 2; // Go to Time Input
                    });
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF0032A0),
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  child: const Text(
                    "Yes",
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildStep2() {
    // Time input
    return Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              "When did you take lunch?",
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
             const SizedBox(height: 24),
             // Mock Inputs
             Row(
               children: [
                 Expanded(
                   child: DropdownButtonFormField<String>(
                     value: _breakStart,
                     items: _timeOptions.map((t) => DropdownMenuItem(value: t, child: Text(t))).toList(),
                     onChanged: (v) => setState(() => _breakStart = v),
                      decoration: const InputDecoration(labelText: 'Start'),
                   ),
                 ),
                 const SizedBox(width: 16),
                 Expanded(
                   child: DropdownButtonFormField<String>(
                     value: _breakEnd,
                     items: _timeOptions.map((t) => DropdownMenuItem(value: t, child: Text(t))).toList(),
                     onChanged: (v) => setState(() => _breakEnd = v),
                      decoration: const InputDecoration(labelText: 'End'),
                   ),
                 ),
               ],
             ),

            const SizedBox(height: 24),
            ElevatedButton(
              onPressed: () {
                setState(() => _step = 3);
              },
               style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF0032A0),
                     minimumSize: const Size(double.infinity, 48),
                ),
              child: const Text("Next", style: TextStyle(color: Colors.white)),
            ),
          ],
        ));
  }

   Widget _buildStep2b() {
    // No lunch reason
    return Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const Text(
              "Why didn't you take lunch?",
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
             const SizedBox(height: 16),
             ..._noLunchReasons.map((reason) => RadioListTile<String>(
               title: Text(reason),
               value: reason,
               groupValue: _noLunchReason,
               onChanged: (val) => setState(() => _noLunchReason = val),
             )),

            const SizedBox(height: 24),
            ElevatedButton(
              onPressed: () {
                setState(() => _step = 3);
              },
              style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF0032A0),
                     minimumSize: const Size(double.infinity, 48),
                ),
              child: const Text("Next", style: TextStyle(color: Colors.white)),
            ),
          ],
        ));
  }

  Widget _buildStep3() {
    // Additional Notes
     return Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              "Additional Notes",
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
             const SizedBox(height: 16),
             TextField(
               onChanged: (v) => _additionalNotes = v,
               decoration: const InputDecoration(
                 hintText: 'Add any details...',
                 border: OutlineInputBorder(),
               ),
               maxLines: 3,
             ),

            const SizedBox(height: 24),
            ElevatedButton(
              onPressed: () {
                setState(() => _step = 4);
              },
               style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF0032A0),
                     minimumSize: const Size(double.infinity, 48),
                ),
              child: const Text("Submit", style: TextStyle(color: Colors.white)),
            ),
          ],
        ));
  }
  
  Widget _buildStep4() {
    // Success
     return Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
             const Icon(LucideIcons.checkCircle, size: 64, color: Colors.green),
            const SizedBox(height: 24),
            const Text(
              "Break Logged!",
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 24),
            ElevatedButton(
              onPressed: widget.onComplete,
               style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF0032A0),
                     minimumSize: const Size(double.infinity, 48),
                ),
              child: const Text("Close", style: TextStyle(color: Colors.white)),
            ),
          ],
        ));
  }
}
