import 'package:core_localization/core_localization.dart';
import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';

class LunchBreakDialog extends StatefulWidget {
  const LunchBreakDialog({super.key, required this.onComplete});
  final VoidCallback 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();

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

  @override
  Widget build(BuildContext context) {
    final i18n = Translations.of(context).staff.clock_in.lunch_break;
    return Dialog(
      backgroundColor: UiColors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(UiConstants.space6),
      ),
      child: AnimatedSwitcher(
        duration: const Duration(milliseconds: 300),
        child: _buildCurrentStep(i18n),
      ),
    );
  }

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

  Widget _buildStep1(TranslationsStaffClockInLunchBreakEn i18n) {
    return Padding(
      padding: const EdgeInsets.all(UiConstants.space6),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            width: 80,
            height: 80,
            decoration: const BoxDecoration(
              color: UiColors.bgSecondary,
              shape: BoxShape.circle,
            ),
            child: const Icon(
              UiIcons.coffee,
              size: 40,
              color: UiColors.iconSecondary,
            ),
          ),
          const SizedBox(height: UiConstants.space6),
          Text(
            i18n.title,
            textAlign: TextAlign.center,
            style: UiTypography.headline1m.textPrimary,
          ),
          const SizedBox(height: UiConstants.space6),
          Row(
            children: <Widget>[
              Expanded(
                child: GestureDetector(
                  onTap: () {
                    setState(() {
                      _tookLunch = false;
                      _step = 102; // Go to No Lunch Reason
                    });
                  },
                  child: Container(
                    padding: const EdgeInsets.symmetric(
                      vertical: UiConstants.space4,
                    ),
                    decoration: BoxDecoration(
                      border: Border.all(color: UiColors.border),
                      borderRadius: UiConstants.radiusLg,
                      color: UiColors.transparent,
                    ),
                    alignment: Alignment.center,
                    child: Text(
                      i18n.no,
                      style: UiTypography.body1m.textPrimary,
                    ),
                  ),
                ),
              ),
              const SizedBox(width: UiConstants.space4),
              Expanded(
                child: ElevatedButton(
                  onPressed: () {
                    setState(() {
                      _tookLunch = true;
                      _step = 2; // Go to Time Input
                    });
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: UiColors.primary,
                    padding: const EdgeInsets.symmetric(
                      vertical: UiConstants.space4,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: UiConstants.radiusLg,
                    ),
                  ),
                  child: Text(
                    i18n.yes,
                    style: UiTypography.body1m.white,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildStep2(TranslationsStaffClockInLunchBreakEn i18n) {
    // Time input
    return Padding(
      padding: const EdgeInsets.all(UiConstants.space6),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            i18n.when_title,
            style: UiTypography.headline4m,
          ),
          const SizedBox(height: UiConstants.space6),
          // Mock Inputs
          Row(
            children: <Widget>[
              Expanded(
                child: DropdownButtonFormField<String>(
                  isExpanded: true,
                  value: _breakStart,
                  items: _timeOptions
                      .map(
                        (String t) => DropdownMenuItem(
                          value: t,
                          child: Text(t, style: UiTypography.body3r),
                        ),
                      )
                      .toList(),
                  onChanged: (String? v) => setState(() => _breakStart = v),
                  decoration: InputDecoration(
                    labelText: i18n.start,
                    contentPadding: const EdgeInsets.symmetric(
                      horizontal: 10,
                      vertical: 8,
                    ),
                  ),
                ),
              ),
              const SizedBox(width: 10),
              Expanded(
                child: DropdownButtonFormField<String>(
                  isExpanded: true,
                  value: _breakEnd,
                  items: _timeOptions
                      .map(
                        (String t) => DropdownMenuItem(
                          value: t,
                          child: Text(t, style: UiTypography.body3r),
                        ),
                      )
                      .toList(),
                  onChanged: (String? v) => setState(() => _breakEnd = v),
                  decoration: InputDecoration(
                    labelText: i18n.end,
                    contentPadding: const EdgeInsets.symmetric(
                      horizontal: 10,
                      vertical: 8,
                    ),
                  ),
                ),
              ),
            ],
          ),

          const SizedBox(height: UiConstants.space6),
          ElevatedButton(
            onPressed: () {
              setState(() => _step = 3);
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: UiColors.primary,
              minimumSize: const Size(double.infinity, 48),
            ),
            child: Text(i18n.next, style: UiTypography.body1m.white),
          ),
        ],
      ),
    );
  }

  Widget _buildStep2b(TranslationsStaffClockInLunchBreakEn i18n) {
    // No lunch reason
    return Padding(
      padding: const EdgeInsets.all(UiConstants.space6),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Text(
            i18n.why_no_lunch,
            style: UiTypography.headline4m,
          ),
          const SizedBox(height: UiConstants.space4),
          ...i18n.reasons.map(
            (String reason) => RadioListTile<String>(
              title: Text(reason, style: UiTypography.body2r),
              value: reason,
              groupValue: _noLunchReason,
              onChanged: (String? val) => setState(() => _noLunchReason = val),
              activeColor: UiColors.primary,
            ),
          ),

          const SizedBox(height: UiConstants.space6),
          ElevatedButton(
            onPressed: () {
              setState(() => _step = 3);
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: UiColors.primary,
              minimumSize: const Size(double.infinity, 48),
            ),
            child: Text(i18n.next, style: UiTypography.body1m.white),
          ),
        ],
      ),
    );
  }

  Widget _buildStep3(TranslationsStaffClockInLunchBreakEn i18n) {
    // Additional Notes
    return Padding(
      padding: const EdgeInsets.all(UiConstants.space6),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            i18n.additional_notes,
            style: UiTypography.headline4m,
          ),
          const SizedBox(height: UiConstants.space4),
          TextField(
            onChanged: (String v) => _additionalNotes = v,
            style: UiTypography.body2r,
            decoration: InputDecoration(
              hintText: i18n.notes_placeholder,
              border: const OutlineInputBorder(),
            ),
            maxLines: 3,
          ),

          const SizedBox(height: UiConstants.space6),
          ElevatedButton(
            onPressed: () {
              setState(() => _step = 4);
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: UiColors.primary,
              minimumSize: const Size(double.infinity, 48),
            ),
            child: Text(i18n.submit, style: UiTypography.body1m.white),
          ),
        ],
      ),
    );
  }

  Widget _buildStep4(TranslationsStaffClockInLunchBreakEn i18n) {
    // Success
    return Padding(
      padding: const EdgeInsets.all(UiConstants.space6),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          const Icon(UiIcons.checkCircle, size: 64, color: UiColors.success),
          const SizedBox(height: UiConstants.space6),
          Text(
            i18n.success_title,
            style: UiTypography.headline1m,
          ),
          const SizedBox(height: UiConstants.space6),
          ElevatedButton(
            onPressed: widget.onComplete,
            style: ElevatedButton.styleFrom(
              backgroundColor: UiColors.primary,
              minimumSize: const Size(double.infinity, 48),
            ),
            child: Text(i18n.close, style: UiTypography.body1m.white),
          ),
        ],
      ),
    );
  }
}
