import 'package:fforte/enums/databases.dart';
import 'package:fforte/screens/sharedWidgets/var_text_field.dart';
import 'package:flutter/material.dart';

class Hinweise extends StatefulWidget {
  final TextEditingController hinweise;

  const Hinweise({super.key, required this.hinweise});

  @override
  State<Hinweise> createState() => _HinweiseState();
}

class _HinweiseState extends State<Hinweise> {
  // Vars for Checkboxes 
  bool liegestelleChecked = false;
  bool kadaverChecked = false;
  bool sichtungChecked = false;
  bool heulenChecked = false;
  bool sonstigesChecked = false;


  // for sonstiges textfield
  TextEditingController sonstigesController = TextEditingController();

  @override
  void initState() {
    super.initState();
    sonstigesController.addListener(updateController);
  }

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

  void updateController() {
    Map<String, bool> props = {
    "liegestelle": liegestelleChecked,
    "kadaver": kadaverChecked,
    "sichtung": sichtungChecked,
    "heulen": heulenChecked,
    "sonstiges": sonstigesChecked
  };
    bool firstRun = true;

    widget.hinweise.text = "";

    for (String key in props.keys) {
    if (!firstRun) {
        widget.hinweise.text += ";";
      }
    if (key == "sonstiges") {
        widget.hinweise.text += "$key:${sonstigesController.text}";

      } else {
        widget.hinweise.text += "$key:${props[key]}";
      }

    firstRun = false;

  }
    print(widget.hinweise.text);
  }


  @override
  Widget build(BuildContext context) {
    return 
      Column(
        children: [
          CheckboxListTile(
            title: Text("Liegestelle"),
            value: liegestelleChecked, 
            onChanged: (bool? value) {
              setState(() => liegestelleChecked = value ?? false);
              updateController();
            } 
          ),

          CheckboxListTile(
            title: Text("Wildtierkadaver"),
            value: kadaverChecked, 
            onChanged: (bool? value) {
              setState(() => kadaverChecked = value ?? false);
              updateController();
            } 
          ),

          CheckboxListTile(
            title: Text("Sichtung"),
            value: sichtungChecked, 
            onChanged: (bool? value) {
              setState(() => sichtungChecked = value ?? false);
              updateController();
            } 
          ),

          CheckboxListTile(
            title: Text("Heulen"),
            value: heulenChecked, 
            onChanged: (bool? value) {
              setState(() => heulenChecked = value ?? false);
              updateController();
            } 
          ),
          CheckboxListTile(
            title: Text("Sonstiges"),
            value: sonstigesChecked, 
            onChanged: (bool? value) {
              setState(() => sonstigesChecked = value ?? false);
              updateController();
            } 
          ),

          if (sonstigesChecked)
          VarTextField(textController: sonstigesController, localization: "Sonstiges", dbName: "HinweiseSonstiges", required: false, dbDesignation: DatabasesEnum.excursion)
        ],
      );

  }
}
