import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

import '../../auth/widget/login_page.dart';

class ChangePassword extends StatelessWidget {
  final _passwordController = TextEditingController();
  final _newPasswordController = TextEditingController();
  final _againPasswordController = TextEditingController();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  ChangePassword({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          centerTitle: true,
          backgroundColor: Theme.of(context).colorScheme.primary,
          foregroundColor: Colors.white,
          title: const Text("Změnit heslo",
              style: TextStyle(fontWeight: FontWeight.bold))),
      body: Form(
        key: formKey,
        child: Padding(
          padding: const EdgeInsets.all(10.0),
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: TextFormField(
                  controller: _passwordController,
                  obscureText: true,
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return "Je nutné zadat stávající heslo!";
                    }
                    return null;
                  },
                  decoration: InputDecoration(
                    floatingLabelBehavior: FloatingLabelBehavior.always,
                    border: OutlineInputBorder(),
                    labelText: "Staré heslo",
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: TextFormField(
                  controller: _newPasswordController,
                  obscureText: true,
                  decoration: InputDecoration(
                    floatingLabelBehavior: FloatingLabelBehavior.always,
                    border: OutlineInputBorder(),
                    labelText: "Nové heslo",
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return "Je nutné zadat nové heslo!";
                    }

                    if (value != _againPasswordController.text) {
                      return "Hesla se neshodují!";
                    }

                    return null;
                  },
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: TextFormField(
                  controller: _againPasswordController,
                  obscureText: true,
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return "Je nutné zadat nové heslo znovu!";
                    }

                    if (value != _newPasswordController.text) {
                      return "Hesla se neshodují!";
                    }

                    return null;
                  },
                  decoration: InputDecoration(
                    floatingLabelBehavior: FloatingLabelBehavior.always,
                    border: OutlineInputBorder(),
                    labelText: "Znovu nové heslo",
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: ElevatedButton(
                    onPressed: () => changePassword(context),
                    child: Text("Změnit heslo")),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void changePassword(context) async {
    if (!formKey.currentState!.validate()) {
      return;
    }

    // TODO pasword change not working
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text("Neimplementováno!")));

    try {
      final user = FirebaseAuth.instance.currentUser!;
      final credentials = EmailAuthProvider.credential(
          email: "test@test.test", password: "testtest");

      await user.reauthenticateWithCredential(credentials);
      print("authenticated");
      await user.updatePassword(_newPasswordController.text);
      LoginPage.logoutUser();
    } catch (e) {
      print("Chybka");
      print(e);
    }
  }
}
