import 'package:ernaehrung/android/pages/nav_pages/main_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:hive_flutter/hive_flutter.dart';

import '../models/user.dart';

class OnboardingPage extends StatefulWidget {
  const OnboardingPage({Key? key}) : super(key: key);

  @override
  State<OnboardingPage> createState() => _OnboardingPageState();
}

class _OnboardingPageState extends State<OnboardingPage> {
  InputDecoration decoration(String hintText) {
    return InputDecoration(
        border: const OutlineInputBorder(
            borderRadius: BorderRadius.all(Radius.circular(10.0))),
        filled: true,
        hintStyle: TextStyle(color: Colors.grey.shade400),
        hintText: hintText,
        labelText: hintText,
        fillColor: Colors.white70);
  }

  @override
  Widget build(BuildContext context) {
    final formKey = GlobalKey<FormBuilderState>();

    return Scaffold(
      appBar: AppBar(
        title: const Text("Welcome"),
        backgroundColor: Colors.grey.shade100,
      ),
      body: FormBuilder(
          key: formKey,
          child: Padding(
            padding: const EdgeInsets.all(8),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Container(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
                    child: FormBuilderTextField(
                      name: 'vorname',
                      decoration: decoration("Vorname"),
                      keyboardType: TextInputType.text,
                      maxLength: 30,
                      autovalidateMode: AutovalidateMode.onUserInteraction,
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(),
                        FormBuilderValidators.minLength(2,
                            errorText:
                                "Die Name sollte mindestens 2 Zeichen lang sein"),
                        FormBuilderValidators.maxLength(30,
                            errorText:
                                "Die Name sollte maximal 30 Zeichen lang sein")
                      ]),
                    ),
                  ),
                  Container(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
                    child: FormBuilderTextField(
                      name: 'nachname',
                      decoration: decoration("Nachname"),
                      keyboardType: TextInputType.text,
                      maxLength: 30,
                      autovalidateMode: AutovalidateMode.onUserInteraction,
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(),
                        FormBuilderValidators.minLength(2,
                            errorText:
                                "Die Nachname sollte mindestens 2 Zeichen lang sein"),
                        FormBuilderValidators.maxLength(30,
                            errorText:
                                "Die Nachname sollte maximal 30 Zeichen lang sein")
                      ]),
                    ),
                  ),
                  Container(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
                    child: FormBuilderTextField(
                      name: 'gewicht',
                      decoration: decoration("Gewicht"),
                      keyboardType: TextInputType.number,
                      maxLength: 7,
                      autovalidateMode: AutovalidateMode.onUserInteraction,
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(),
                        FormBuilderValidators.numeric(
                            errorText:
                                "Der Gewicht sollte mindestens 10 kg sein"),
                        FormBuilderValidators.max(200,
                            errorText:
                                "Der Gewicht sollte maximal 200 kg sein"),
                        FormBuilderValidators.min(10,
                            errorText:
                                "Der Gewicht sollte mindestens 10 kg sein")
                      ]),
                    ),
                  ),
                  Container(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
                    child: FormBuilderTextField(
                      name: 'groesse',
                      decoration: decoration("Größe"),
                      keyboardType: TextInputType.number,
                      maxLength: 7,
                      autovalidateMode: AutovalidateMode.onUserInteraction,
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(),
                        FormBuilderValidators.numeric(
                            errorText: "Die Größe sollte mindestens 60cm sein"),
                        FormBuilderValidators.max(230,
                            errorText: "Die Größe sollte maximal 230cm sein"),
                        FormBuilderValidators.min(60,
                            errorText: "Die Größe sollte mindestens 60cm sein")
                      ]),
                    ),
                  ),
                  Container(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
                    child: FormBuilderTextField(
                      name: 'alter',
                      decoration: decoration("Alter"),
                      keyboardType: TextInputType.number,
                      maxLength: 7,
                      autovalidateMode: AutovalidateMode.onUserInteraction,
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(),
                        FormBuilderValidators.numeric(
                            errorText:
                                "Das Alter sollte mindestens 6 Jahre alt sein"),
                        FormBuilderValidators.max(99,
                            errorText:
                                "Das Alter sollte maximal 99 Jahre alt sein"),
                        FormBuilderValidators.min(6,
                            errorText:
                                "Das Alter sollte mindestens 6 Jahre alt sein")
                      ]),
                    ),
                  ),
                  Container(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
                    child: FormBuilderTextField(
                      name: 'kalorien',
                      decoration:
                          decoration("gewünschte Kalorienzufuhr: min. 1000"),
                      keyboardType: TextInputType.number,
                      maxLength: 7,
                      autovalidateMode: AutovalidateMode.onUserInteraction,
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(),
                        FormBuilderValidators.numeric(
                            errorText:
                                "Die Kalorienanzahl sollte mindestens 1000 Kcal sein"),
                        FormBuilderValidators.max(30000),
                        FormBuilderValidators.min(1000,
                            errorText:
                                "Die Kalorienanzahl sollte mindestens 1000 Kcal sein")
                      ]),
                    ),
                  ),
                  Container(
                      margin: const EdgeInsets.symmetric(
                          vertical: 8, horizontal: 0),
                      width: double.infinity,
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          minimumSize: const Size.fromHeight(40), //
                          backgroundColor: const Color(0xFF6E7BFB),
                          foregroundColor: const Color(0xFFffffff),
                          shape: const StadiumBorder(),
                        ),
                        onPressed: () {
                          final Box box = Hive.box<User>("USER_BOX");
                          box.put("USER", User(
                              formKey.currentState?.fields['vorname']?.value,
                              formKey.currentState?.fields['nachname']?.value,
                              int.parse(formKey.currentState?.fields['gewicht']?.value),
                              int.parse(formKey.currentState?.fields['groesse']?.value),
                              int.parse(formKey.currentState?.fields['alter']?.value),
                              int.parse(formKey.currentState?.fields['kalorien']?.value)
                          ));

                          Navigator
                              .of(context)
                              .pushReplacement(MaterialPageRoute(builder: (BuildContext context) => const MainPage()));
                        },
                        child: const Text("Eingaben bestätigen"),
                      )),
                ],
              ),
            ),
          )),
    );
  }
}
