import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:prosappco/main.dart';
import 'package:prosappco/src/provider/google_sign.dart';
import 'package:prosappco/src/screens/login.dart';
import 'package:prosappco/src/screens/welcome.dart';
import 'package:provider/provider.dart';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:prosappco/src/screens/register.dart';

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

  @override
  State<LoginEmailScreen> createState() => _LoginEmailScreenState();
}

class _LoginEmailScreenState extends State<LoginEmailScreen> {
  bool _obscureText = true;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Color(0xFFD6F4FF),
      body: Stack(
        children: [
          Container(
            margin: const EdgeInsets.only(top: 130),
            width: double.infinity,
            height: 800,
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    topRight: Radius.circular(50),
                    topLeft: Radius.circular(50))),
          ),
          Container(
            margin: const EdgeInsets.only(top: 20, left: 30),
            child: const Image(
              image: AssetImage('images/logo_prosapp.png'),
              width: 180,
              height: 100,
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 20),
            margin: const EdgeInsets.only(
              top: 125,
              left: 25,
            ),
            child: Row(
              children: <Widget>[
                IconButton(
                  icon: Icon(
                    Icons.arrow_back,
                    size: 30,
                  ),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                Text(
                  'Iniciar sesión',
                  style: TextStyle(
                    color: Color(0xFF262626),
                    fontSize: 38.0,
                    fontWeight: FontWeight.bold,
                  ),
                  textAlign: TextAlign.right,
                ),
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 20),
            margin: const EdgeInsets.only(top: 210, left: 50, right: 50),
            child: Column(children: [
              Padding(
                padding: EdgeInsets.only(bottom: 20),
                child: ElevatedButton(
                    onPressed: () async {
                      try {
                        final UserCredential userCredential =
                            await signInWithGoogle();
                        // Si la autenticación es exitosa, realiza una acción
                        // Por ejemplo, navegar a otra pantalla:
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => WelcomeScreen()));
                      } catch (e) {
                        // Si ocurre un error durante la autenticación, muestra un mensaje al usuario
                        ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                          content: Text(
                              'Error al iniciar sesion intentalo de nuevo'),
                        ));
                      }
                    },
                    style: ElevatedButton.styleFrom(
                      primary: Color(0xFF2BA4EC),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(50),
                      ),
                      elevation: 0,
                      minimumSize: Size(230, 60),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          'Entrar con Google ',
                          style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            fontSize: 18,
                          ),
                        ),
                        SizedBox(width: 5),
                        FaIcon(FontAwesomeIcons.google),
                      ],
                    )),
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Divider(
                        color: Colors.black38,
                        thickness: 1,
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 10),
                      child: Text("ó"),
                    ),
                    Expanded(
                      child: Divider(
                        color: Colors.black38,
                        thickness: 1,
                      ),
                    ),
                  ],
                ),
              ),
              const Padding(
                  padding: EdgeInsets.only(bottom: 5),
                  child: Align(
                    alignment: Alignment.topLeft,
                    child: Text('Email',
                        style: TextStyle(
                            fontSize: 18.0, color: Color(0xFF65676B))),
                  )),
              const Padding(
                padding: EdgeInsets.only(bottom: 20),
                child: TextField(
                  decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0xFFECECEC)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(50),
                          )),
                      focusedBorder: OutlineInputBorder(
                          borderSide: BorderSide(color: Color(0xFFECECEC)),
                          borderRadius: BorderRadius.all(
                            Radius.circular(50),
                          )),
                      hintText: 'Hello@gmail.com',
                      fillColor: Color.fromARGB(255, 239, 239, 239),
                      filled: true,
                      prefixIcon: Icon(Icons.email_outlined)),
                ),
              ),
              const Padding(
                  padding: EdgeInsets.only(bottom: 5),
                  child: Align(
                    alignment: Alignment.topLeft,
                    child: Text('Password',
                        style: TextStyle(
                            fontSize: 18.0, color: Color(0xFF65676B))),
                  )),
              Padding(
                padding: EdgeInsets.only(bottom: 50),
                child: TextField(
                  obscureText: _obscureText,
                  decoration: InputDecoration(
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0xFFECECEC)),
                        borderRadius: BorderRadius.all(
                          Radius.circular(50),
                        )),
                    focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0xFFECECEC)),
                        borderRadius: BorderRadius.all(
                          Radius.circular(50),
                        )),
                    hintText: 'Contraseña',
                    fillColor: Color.fromARGB(255, 239, 239, 239),
                    filled: true,
                    prefixIcon: Icon(Icons.lock_outline),
                    suffixIcon: IconButton(
                      icon: Icon(
                        _obscureText ? Icons.visibility_off : Icons.visibility,
                        color: Colors.grey,
                      ),
                      onPressed: () {
                        setState(() {
                          _obscureText = !_obscureText;
                        });
                      },
                    ),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(bottom: 40),
                child: Center(
                    child: ElevatedButton(
                  onPressed: () {},
                  child: Text(
                    'Iniciar',
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 18,
                    ),
                  ),
                  style: ElevatedButton.styleFrom(
                    primary: Color(0xFF2BA4EC),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(50),
                    ),
                    elevation: 0,
                    minimumSize: Size(230, 60),
                  ),
                )),
              ),
              RichText(
                text: TextSpan(
                  style: TextStyle(
                    fontSize: 16.0,
                    color: Color(0xFF65676B),
                    fontFamily: 'Poppins',
                  ),
                  children: [
                    TextSpan(text: 'No estas registrado? '),
                    WidgetSpan(
                      child: GestureDetector(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => RegisterScreen()),
                          );
                        },
                        child: Text(
                          'Registrarse',
                          style: TextStyle(
                            fontSize: 16.0,
                            color: Color(0xFF2BA4EC),
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              )
            ]),
          ),
        ],
      ),
    ));
  }
}
