import 'package:flutter/material.dart';

import '../../../../theming/darkAppTheme.dart';
import '../../../../widget/loadingSpinner.dart';

class FeedbackForm extends StatefulWidget {
  final Future<void> Function(String, {Map<String, dynamic>? extras}) callback;
  final ScrollController? scrollController;
  const FeedbackForm({required this.scrollController, required this.callback, super.key});

  @override
  State<FeedbackForm> createState() => _FeedbackFormState();
}

class _FeedbackFormState extends State<FeedbackForm> {
  final TextEditingController _feedbackInput = TextEditingController();
  bool _textFieldEmpty = false;
  bool _isSending = false;

  @override
  void initState() {
    super.initState();
    _feedbackInput.addListener(() {
      setState(() {
        _textFieldEmpty = _feedbackInput.text.isEmpty;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: DarkAppTheme.theme,
      child: Visibility(
        visible: !_isSending,
        replacement: const LoadingSpinner(infoText: "Daten werden ermittelt"),
        child: SingleChildScrollView(
          controller: widget.scrollController,
          padding: const EdgeInsets.all(20),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text("Bitte gib keine geheimen Daten wie z.B. Passwörter weiter!", style: TextStyle(fontSize: 10)),
              const SizedBox(height: 10),
              TextField(
                controller: _feedbackInput,
                autofocus: true,
                decoration: InputDecoration(
                    border: const OutlineInputBorder(),
                    label: const Text("Dein Feedback"),
                    errorText: _textFieldEmpty ? "Bitte gib eine Beschreibung an" : null
                ),
                minLines: 1,
                maxLines: 2,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  TextButton(
                    onPressed: () async {
                      if(_isSending) return;
                      if(_feedbackInput.text.isEmpty) {
                        setState(() {
                          _textFieldEmpty = true;
                        });
                        return;
                      }

                      setState(() {
                        _isSending = true;
                      });

                      widget.callback(_feedbackInput.text);
                    },
                    child: const Text("Senden"),
                  ),
                ],
              ),
              const SizedBox(height: 40),
              const Center(
                  child: Column(
                    children: [
                      Text(
                        "Feedback, mal süß wie Kuchen, mal sauer wie Gurken, doch immer ein Schlüssel fürs Wachsen und Lernen.",
                        textAlign: TextAlign.center,
                      ),
                      SizedBox(height: 10),
                      Icon(Icons.emoji_objects_outlined)
                    ],
                  )
              ),
            ],
          ),
        ),
      ),
    );
  }
}