import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'auth_provider.dart';

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

  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen>
    with SingleTickerProviderStateMixin {
  final TextEditingController emailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );
    _animation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeIn,
    );
    _controller.forward();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'LOGIN PAGE',
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontFamily: 'Quicksand', // Mengubah font menjadi 'Quicksand'
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.brown[100],
      ),

      backgroundColor:
          Colors.brown[100], // Mengubah warna latar belakang menjadi coklat
      body: SingleChildScrollView(
        child: FadeTransition(
          opacity: _animation,
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                SizedBox(
                  height: 200, // Ubah ukuran asset logo.png menjadi lebih besar
                  child: Image.asset('assets/images/logo.png'),
                ),
                const SizedBox(height: 50),
                TextField(
                  controller: emailController,
                  decoration: InputDecoration(
                    labelText: 'Email',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                    ),
                    prefixIcon: const Icon(Icons.email),
                    contentPadding: EdgeInsets.symmetric(
                        vertical: 15.0,
                        horizontal: 15.0), // Menyesuaikan padding konten
                  ),
                ),
                const SizedBox(height: 20),
                TextField(
                  controller: passwordController,
                  decoration: InputDecoration(
                    labelText: 'Password',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(15.0),
                    ),
                    prefixIcon: const Icon(Icons.lock),
                    contentPadding: EdgeInsets.symmetric(
                        vertical: 15.0,
                        horizontal: 15.0), // Menyesuaikan padding konten
                  ),
                  obscureText: true,
                ),
                const SizedBox(height: 23),
                ElevatedButton(
                  onPressed: () async {
                    bool success = await context.read<AuthProvider>().signIn(
                          email: emailController.text,
                          password: passwordController.text,
                          context: context,
                        );
                    if (success) {
                      Navigator.pushReplacementNamed(context, '/home');
                    }
                  },
                  child: const Text('Login'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pushReplacementNamed(context, '/register');
                  },
                  child: const Text('Belum punya akun? Registrasi'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
