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

/// A location input field for the one-time order form.
/// Matches the prototype input field style.
class OneTimeOrderLocationInput extends StatefulWidget {
  /// Creates a [OneTimeOrderLocationInput].
  const OneTimeOrderLocationInput({
    required this.label,
    required this.value,
    required this.onChanged,
    super.key,
  });

  /// The label text to display above the field.
  final String label;

  /// The current location value.
  final String value;

  /// Callback when the location value changes.
  final ValueChanged<String> onChanged;

  @override
  State<OneTimeOrderLocationInput> createState() =>
      _OneTimeOrderLocationInputState();
}

class _OneTimeOrderLocationInputState extends State<OneTimeOrderLocationInput> {
  late final TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: widget.value);
  }

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

  @override
  void didUpdateWidget(OneTimeOrderLocationInput oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.value != _controller.text) {
      _controller.text = widget.value;
    }
  }

  @override
  Widget build(BuildContext context) {
    return UiTextField(
      label: widget.label,
      controller: _controller,
      onChanged: widget.onChanged,
      hintText: 'Enter address',
      prefixIcon: UiIcons.mapPin,
    );
  }
}
