import 'package:animations/animations.dart';
import 'package:fforte/enums/databases.dart';
import 'package:fforte/screens/Excursion/widgets/anzahlen.dart';
import 'package:fforte/screens/Excursion/widgets/bima_nutzer.dart';
import 'package:fforte/screens/Excursion/widgets/hinweise.dart';
import 'package:fforte/screens/Excursion/widgets/hund_u_leine.dart';
import 'package:fforte/screens/Excursion/widgets/letzter_niederschlag.dart';
import 'package:fforte/screens/Excursion/widgets/spur_gefunden.dart';
import 'package:fforte/screens/Excursion/widgets/strecke_u_spurbedingungen.dart';
import 'package:fforte/screens/sharedWidgets/datum.dart';
import 'package:fforte/screens/sharedWidgets/var_text_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class ExcursionMain extends StatefulWidget {
  const ExcursionMain({super.key});

  @override
  State<ExcursionMain> createState() => _ExcursionMainState();
}

class _ExcursionMainState extends State<ExcursionMain> {
  // all TextEditingController because its easier

  Map<String, TextEditingController> getTextFields() {
    Map<String, TextEditingController> rmap = {
      // Step 1
      "LogDat": TextEditingController(),
      "Rudel": TextEditingController(),
      "Teilnehm": TextEditingController(),
      "Jahr": TextEditingController(),
      "Dauer": TextEditingController(),
      "MHund": TextEditingController(),
      "MLeine": TextEditingController(),
      "BLand": TextEditingController(),
      "Lkr": TextEditingController(),
      "BeiOrt": TextEditingController(),
      "BimaNr": TextEditingController(),
      "BimaName": TextEditingController(),
      "BimaNutzer": TextEditingController(),
      "BimaAGV": TextEditingController(),

      // Step 2
      "Wetter": TextEditingController(),
      "Temperat": TextEditingController(),
      "RegenVor": TextEditingController(),
      "KmAuto": TextEditingController(),
      "KmFuss": TextEditingController(),
      "KmRad": TextEditingController(),
      "KmTotal": TextEditingController(),
      "KmAuProz": TextEditingController(),
      "KmFuProz": TextEditingController(),
      "KmRaProz": TextEditingController(),

      // Spur maybe own step?
      "SpGut": TextEditingController(),
      "SpMittel": TextEditingController(),
      "SpSchlecht": TextEditingController(),
      "SpurFund": TextEditingController(),
      "SpurLang": TextEditingController(),
      "SpurTiere": TextEditingController(),
      "SpSicher": TextEditingController(),
      "WelpenSp": TextEditingController(),
      "WelpenAnz": TextEditingController(),
      "WpSicher": TextEditingController(),

      "LosungGes": TextEditingController(),
      "LosungAnz": TextEditingController(),
      "LosungGen": TextEditingController(),
      "UrinAnz": TextEditingController(),
      "UrinGen": TextEditingController(),
      "OestrAnz": TextEditingController(),
      "OestrGen": TextEditingController(),
      "HaarAnz": TextEditingController(),
      "HaarGen": TextEditingController(),
      "LosungKm": TextEditingController(),
      "GenetiKm": TextEditingController(),
      "Hinweise": TextEditingController(),

      // Step 3
      "Bemerk": TextEditingController(),
      "IntKomm": TextEditingController(),
      "FallNum": TextEditingController(),
    };

    return rmap;
  }

  int currentStep = 0;

  @override
  Widget build(BuildContext context) {
    List<Step> getSteps() => [
          Step(
              title: Text(AppLocalizations.of(context)!.dateandtime),
              content: Column(
                children: [
                  // ---------- Date
                  Datum(
                    initDatum: DateTime.now(),
                    onDateChanged: (date) {
                      getTextFields()["LogDat"]!.text = date.toString();
                    },
                    name: AppLocalizations.of(context)!.date,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Pack
                  VarTextField(
                    textController: getTextFields()["Rudel"]!,
                    localization: AppLocalizations.of(context)!.rudel,
                    dbName: "Rudel",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Participants
                  VarTextField(
                    textController: getTextFields()["Teilnehm"]!,
                    localization: AppLocalizations.of(context)!.teilnehmer,
                    dbName: "Teilnehm",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Duration
                  VarTextField(
                    textController: getTextFields()["Dauer"]!,
                    localization: AppLocalizations.of(context)!.dauer,
                    dbName: "Dauer",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Dog(leash)
                  HundULeine(onMHundChanged: (mHund, mLeine) {
                    getTextFields()["MHund"]!.text = mHund;
                    getTextFields()["MLeine"]!.text = mLeine;
                  }),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- State
                  VarTextField(
                    textController: getTextFields()["BLand"]!,
                    localization: AppLocalizations.of(context)!.bland,
                    dbName: "BLand",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Country
                  VarTextField(
                    textController: getTextFields()["Lkr"]!,
                    localization: AppLocalizations.of(context)!.lkr,
                    dbName: "Lkr",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- By State
                  VarTextField(
                    textController: getTextFields()["BeiOrt"]!,
                    localization: AppLocalizations.of(context)!.beiort,
                    dbName: "BeiOrt",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Bima number
                  const Divider(),
                  const SizedBox(
                    height: 10,
                  ),
                  VarTextField(
                    textController: getTextFields()["BimaNr"]!,
                    localization: AppLocalizations.of(context)!.bimaNr,
                    dbName: "BimaNr",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Bima name
                  VarTextField(
                    textController: getTextFields()["BimaName"]!,
                    localization: AppLocalizations.of(context)!.bimaName,
                    dbName: "BimaName",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Bima user
                  BimaNutzer(onBimaNutzerChanged: (value) {
                    setState(() {
                      getTextFields()["BimaNutzer"]!.text = value;
                    });
                  }),
                  const SizedBox(
                    height: 10,
                  ),
                  // ---------- Bima AGV
                  VarTextField(
                    textController: getTextFields()["BimaAGV"]!,
                    localization: AppLocalizations.of(context)!.bimaAGV,
                    dbName: "BimaAGV",
                    required: false,
                    dbDesignation: DatabasesEnum.excursion,
                  )
                ],
              )),
          Step(
            title: const Text("step2"),
            content: Column(
              children: [
                  // ---------- Weather
                  // TODO CHANGE TO LOCALIZATION
                VarTextField(
                  textController: getTextFields()["Wetter"]!,
                  localization: "Wetter",
                  dbName: "Wetter",
                  required: false,
                  dbDesignation: DatabasesEnum.excursion,
                ),
                const SizedBox(height: 10),
                  // ---------- Temperature
                VarTextField(
                  textController: getTextFields()["Temperat"]!,
                  localization: "Temperatur",
                  dbName: "Temperat",
                  required: false,
                  dbDesignation: DatabasesEnum.excursion,
                ),
                const SizedBox(height: 10),
                  // ---------- Last precipitation
                LetzterNiederschlag(controller: getTextFields()["RegenVor"]!),
                const SizedBox(height: 20),
                  // ---------- Track conditions
                StreckeUSpurbedingungen(
                  kmAutoController: getTextFields()["KmAuto"]!,
                  kmFussController: getTextFields()["KmFuss"]!,
                  kmRadController: getTextFields()["KmRad"]!,
                  spGutController: getTextFields()["SpGut"]!,
                  spMittelController: getTextFields()["SpMittel"]!,
                  spSchlechtController: getTextFields()["SpSchlecht"]!,
                ),
                const SizedBox(
                  height: 20,
                ),
                const Divider(),
                  // ---------- Track found
                SpurGefunden(
                    spurFund: getTextFields()["SpurFund"]!,
                    spurLang: getTextFields()["SpurLang"]!,
                    spurTiere: getTextFields()["SpurTiere"]!,
                    spSicher: getTextFields()["SpSicher"]!,
                    welpenSp: getTextFields()["WelpenSp"]!,
                    welpenAnz: getTextFields()["WelpenAnz"]!,
                    wpSicher: getTextFields()["WpSicher"]!),
                const SizedBox(
                  height: 20,
                ),
                  // ---------- Counts
                Anzahlen(
                  losungAnz: getTextFields()["LosungAnz"]!,
                  losungGes: getTextFields()["LosungGes"]!,
                  losungGen: getTextFields()["LosungGen"]!,
                  urinAnz: getTextFields()["UrinAnz"]!,
                  urinGen: getTextFields()["UrinGen"]!,
                  oestrAnz: getTextFields()["OestrAnz"]!,
                  oestrGen: getTextFields()["OestrGen"]!,
                  haarAnz: getTextFields()["HaarAnz"]!,
                  haarGen: getTextFields()["HaarGen"]!,
                ),
                const SizedBox(
                  height: 20,
                ),
                const Divider(),
                  // ---------- Clues
                  // TODO Add title
                Hinweise(
                  hinweise: getTextFields()["Hinweise"]!,
                ),
              ],
            ),
          ),
          Step(
            title: const Text("step3"),
            content: Column(
              children: [
                  // ---------- Remarks
                VarTextField(
                  textController: getTextFields()["Bemerk"]!,
                  localization: "Bemerkungen",
                  dbName: "Bemerk",
                  required: false,
                  dbDesignation: DatabasesEnum.excursion,
                ),
                const SizedBox(
                  height: 20,
                ),
                  // ---------- Internal communication
                VarTextField(
                  textController: getTextFields()["IntKomm"]!,
                  localization: "Interne Kommunikation",
                  dbName: "IntKomm",
                  required: false,
                  dbDesignation: DatabasesEnum.excursion,
                ),
              ],
            ),
          ),
        ];

      // Begin of widget tree
    return Scaffold(
        appBar: AppBar(
          title: Text(AppLocalizations.of(context)!.excursion),
        ),
        body: PageTransitionSwitcher(
          duration: const Duration(microseconds: 800),
          transitionBuilder: (Widget child, Animation<double> animation,
              Animation<double> secondaryAnimation) {
            return SharedAxisTransition(
              animation: animation,
              secondaryAnimation: secondaryAnimation,
              transitionType: SharedAxisTransitionType.vertical,
              child: child,
            );
          },
          child: Stepper(
            key: ValueKey<int>(currentStep),
            steps: getSteps(),
            currentStep: currentStep,
            onStepTapped: (value) {
              setState(() {
                currentStep = value;
              });
            },
            onStepContinue: () {
              final isLastStep = currentStep == getSteps().length - 1;

              if (!isLastStep) {
                setState(() {
                  currentStep += 1;
                });
              }
            },
            onStepCancel: () {
              if (currentStep == 0) {
                Navigator.pop(context);
              } else {
                setState(() {
                  currentStep -= 1;
                });
              }
            },
          ),
        ));
  }
}
