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

import 'hub_address_autocomplete.dart';

/// A dialog for adding a new hub.
class AddHubDialog extends StatefulWidget {
  /// Callback when the "Create Hub" button is pressed.
  final void Function(
    String name,
    String address, {
    String? placeId,
    double? latitude,
    double? longitude,
  }) onCreate;

  /// Callback when the dialog is cancelled.
  final VoidCallback onCancel;

  /// Creates an [AddHubDialog].
  const AddHubDialog({
    required this.onCreate,
    required this.onCancel,
    super.key,
  });

  @override
  State<AddHubDialog> createState() => _AddHubDialogState();
}

class _AddHubDialogState extends State<AddHubDialog> {
  late final TextEditingController _nameController;
  late final TextEditingController _addressController;
  late final FocusNode _addressFocusNode;
  Prediction? _selectedPrediction;

  @override
  void initState() {
    super.initState();
    _nameController = TextEditingController();
    _addressController = TextEditingController();
    _addressFocusNode = FocusNode();
  }

  @override
  void dispose() {
    _nameController.dispose();
    _addressController.dispose();
    _addressFocusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: UiColors.bgOverlay,
      child: Center(
        child: SingleChildScrollView(
          child: Container(
            width: MediaQuery.of(context).size.width * 0.9,
            padding: const EdgeInsets.all(UiConstants.space5),
            decoration: BoxDecoration(
              color: UiColors.bgPopup,
              borderRadius: BorderRadius.circular(UiConstants.radiusBase),
              boxShadow: const <BoxShadow>[
                BoxShadow(color: UiColors.popupShadow, blurRadius: 20),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Text(
                  t.client_hubs.add_hub_dialog.title,
                  style: UiTypography.headline3m.textPrimary,
                ),
                const SizedBox(height: UiConstants.space5),
                _buildFieldLabel(t.client_hubs.add_hub_dialog.name_label),
                TextField(
                  controller: _nameController,
                  style: UiTypography.body1r.textPrimary,
                  decoration: _buildInputDecoration(
                    t.client_hubs.add_hub_dialog.name_hint,
                  ),
                ),
                const SizedBox(height: UiConstants.space4),
                _buildFieldLabel(t.client_hubs.add_hub_dialog.address_label),
                HubAddressAutocomplete(
                  controller: _addressController,
                  hintText: t.client_hubs.add_hub_dialog.address_hint,
                  focusNode: _addressFocusNode,
                  onSelected: (Prediction prediction) {
                    _selectedPrediction = prediction;
                  },
                ),
                const SizedBox(height: UiConstants.space8),
                Row(
                  children: <Widget>[
                    Expanded(
                      child: UiButton.secondary(
                        onPressed: widget.onCancel,
                        text: t.common.cancel,
                      ),
                    ),
                    const SizedBox(width: UiConstants.space3),
                    Expanded(
                      child: UiButton.primary(
                        onPressed: () {
                          if (_nameController.text.isNotEmpty) {
                            widget.onCreate(
                              _nameController.text,
                              _addressController.text,
                              placeId: _selectedPrediction?.placeId,
                              latitude: double.tryParse(
                                _selectedPrediction?.lat ?? '',
                              ),
                              longitude: double.tryParse(
                                _selectedPrediction?.lng ?? '',
                              ),
                            );
                          }
                        },
                        text: t.client_hubs.add_hub_dialog.create_button,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildFieldLabel(String label) {
    return Padding(
      padding: const EdgeInsets.only(bottom: UiConstants.space2),
      child: Text(label, style: UiTypography.body2m.textPrimary),
    );
  }

  InputDecoration _buildInputDecoration(String hint) {
    return InputDecoration(
      hintText: hint,
      hintStyle: UiTypography.body2r.textPlaceholder,
      filled: true,
      fillColor: UiColors.input,
      contentPadding: const EdgeInsets.symmetric(
        horizontal: UiConstants.space4,
        vertical: 14,
      ),
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(UiConstants.radiusBase),
        borderSide: const BorderSide(color: UiColors.border),
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(UiConstants.radiusBase),
        borderSide: const BorderSide(color: UiColors.border),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(UiConstants.radiusBase),
        borderSide: const BorderSide(color: UiColors.ring, width: 2),
      ),
    );
  }
}
