import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class HundULeine extends StatefulWidget {
  // 1. with dog (ja, bzw name oder nein) 2. with leash
  // if nothing selected null
  final Function(String, String) onMHundChanged;

  const HundULeine({super.key, required this.onMHundChanged});

  @override
  HundULeineState createState() => HundULeineState();
}

class HundULeineState extends State<HundULeine> {
  String? _selectedMHundValue;
  String _selectedMLeineValue = "nein";
  TextEditingController controller = TextEditingController();
  bool visible = false;

  void onValueChanged(String mHund, String mLeine) {
    setState(() {
      visible = mHund == "ja" ? true : false;
      _selectedMHundValue = mHund;
      _selectedMLeineValue = mLeine;
    });

    if (mHund != "nein" && controller.text != "") mHund = controller.text;

    if (mHund == "nein") {
      _selectedMHundValue = "nein";
      widget.onMHundChanged(mHund, "nein");
    } else {
      widget.onMHundChanged(mHund, mLeine);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Align(
          alignment: Alignment.bottomLeft,
          child: Text(AppLocalizations.of(context)!.mHund)),
        ListTile(
          visualDensity: const VisualDensity(vertical: -4),
          title: Text(AppLocalizations.of(context)!.ja),
          leading: Radio<String>(
            value: "ja",
            groupValue: _selectedMHundValue,
            onChanged: (value) {
              onValueChanged(value!, _selectedMLeineValue);
            },
          ),
        ),
        ListTile(
          visualDensity: const VisualDensity(vertical: -4),
          title: Text(AppLocalizations.of(context)!.nein),
          leading: Radio<String>(
            value: "nein",
            groupValue: _selectedMHundValue,
            onChanged: (value) {
              onValueChanged(value!, _selectedMLeineValue);
            },
          ),
        ),
        if (visible) ...[
          TextField(
            controller: controller,
            onChanged: (value) {
              onValueChanged("ja", _selectedMLeineValue);
            },
            decoration:
                InputDecoration(hintText: AppLocalizations.of(context)!.name),
          ),
          Text(AppLocalizations.of(context)!.mLeine),
          ListTile(
            visualDensity: const VisualDensity(vertical: -4),
            title: Text(AppLocalizations.of(context)!.ja),
            leading: Radio<String>(
              value: "ja",
              groupValue: _selectedMLeineValue,
              onChanged: (value) {
                onValueChanged("ja", value!);
              },
            ),
          ),
          ListTile(
            visualDensity: const VisualDensity(vertical: -4),
            title: Text(AppLocalizations.of(context)!.nein),
            leading: Radio<String>(
              value: "nein",
              groupValue: _selectedMLeineValue,
              onChanged: (value) {
                onValueChanged("ja", value!);
              },
            ),
          ),
        ]
      ],
    );
  }
}
