import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:staff_authentication/src/presentation/widgets/common/section_title_subtitle.dart';
import 'package:staff_authentication/staff_authentication.dart';

/// A widget for setting up preferred work locations and distance.
class ProfileSetupLocation extends StatefulWidget {
  /// The list of preferred locations.
  final List<String> preferredLocations;

  /// The maximum distance in miles.
  final double maxDistanceMiles;

  /// Callback for when the preferred locations list changes.
  final ValueChanged<List<String>> onLocationsChanged;

  /// Callback for when the max distance changes.
  final ValueChanged<double> onDistanceChanged;

  /// Creates a [ProfileSetupLocation] widget.
  const ProfileSetupLocation({
    super.key,
    required this.preferredLocations,
    required this.maxDistanceMiles,
    required this.onLocationsChanged,
    required this.onDistanceChanged,
  });

  @override
  State<ProfileSetupLocation> createState() => _ProfileSetupLocationState();
}

class _ProfileSetupLocationState extends State<ProfileSetupLocation> {
  final TextEditingController _locationController = TextEditingController();

  @override
  void dispose() {
    _locationController.dispose();
    super.dispose();
  }

  /// Adds the current text from the controller as a location.
  void _addLocation() {
    final String loc = _locationController.text.trim();
    if (loc.isNotEmpty && !widget.preferredLocations.contains(loc)) {
      final List<String> updatedList = List<String>.from(widget.preferredLocations)
        ..add(loc);
      widget.onLocationsChanged(updatedList);
      _locationController.clear();
    }
  }

  @override
  /// Builds the location setup step UI.
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        SectionTitleSubtitle(
          title: t.staff_authentication.profile_setup_page.location.title,
          subtitle: t.staff_authentication.profile_setup_page.location.subtitle,
        ),
        const SizedBox(height: UiConstants.space8),

        // Add Location input
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          spacing: UiConstants.space2,
          children: <Widget>[
            Expanded(
              child: UiTextField(
                label: t
                    .staff_authentication
                    .profile_setup_page
                    .location
                    .add_location_label,
                controller: _locationController,
                hintText: t
                    .staff_authentication
                    .profile_setup_page
                    .location
                    .add_location_hint,
                onSubmitted: (_) => _addLocation(),
              ),
            ),
            UiButton.secondary(
              text:
                  t.staff_authentication.profile_setup_page.location.add_button,
              onPressed: _addLocation,
              style: OutlinedButton.styleFrom(
                minimumSize: const Size(0, 48),
                maximumSize: const Size(double.infinity, 48),
              ),
            ),
          ],
        ),

        const SizedBox(height: UiConstants.space4),

        // Location Badges
        if (widget.preferredLocations.isNotEmpty)
          Wrap(
            spacing: UiConstants.space2,
            runSpacing: UiConstants.space2,
            children: widget.preferredLocations.map((String loc) {
              return UiChip(
                label: loc,
                leadingIcon: UiIcons.mapPin,
                trailingIcon: UiIcons.close,
                onTrailingIconTap: () => _removeLocation(location: loc),
                variant: UiChipVariant.secondary,
              );
            }).toList(),
          ),

        const SizedBox(height: UiConstants.space8),
        // Slider
        Text(
          t.staff_authentication.profile_setup_page.location.max_distance(
            distance: widget.maxDistanceMiles.round().toString(),
          ),
          style: UiTypography.body2m,
        ),
        const SizedBox(height: UiConstants.space2),
        Slider(
          value: widget.maxDistanceMiles,
          min: 5,
          max: 50,
          onChanged: widget.onDistanceChanged,
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: UiConstants.space2),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                t
                    .staff_authentication
                    .profile_setup_page
                    .location
                    .min_dist_label,
                style: UiTypography.footnote1r.textSecondary,
              ),
              Text(
                t
                    .staff_authentication
                    .profile_setup_page
                    .location
                    .max_dist_label,
                style: UiTypography.footnote1r.textSecondary,
              ),
            ],
          ),
        ),
      ],
    );
  }

  /// Removes the specified [location] from the list.
  void _removeLocation({required String location}) {
    final List<String> updatedList = List<String>.from(widget.preferredLocations)
      ..remove(location);
    widget.onLocationsChanged(updatedList);
  }
}
