import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../blocs/auth/auth_bloc.dart';
import '../blocs/auth/auth_event.dart';
import '../blocs/auth/auth_state.dart';
import '../blocs/session/session_bloc.dart';
import '../blocs/session/session_event.dart';
import '../theme/app_theme.dart';
import '../theme/modern_glass_button.dart';

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

  @override
  State<LoginForm> createState() => _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BlocListener<AuthBloc, AuthState>(
      listener: (context, state) {
        if (state is AuthFailure) {
          ScaffoldMessenger.of(
            context,
          ).showSnackBar(SnackBar(content: Text(state.error)));
        } else if (state is AuthAuthenticated) {
          // Start session
          context.read<SessionBloc>().add(SessionStarted(state.token));
        }
      },
      child: Center(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const Text(
                'sign in',
                style: TextStyle(fontSize: 24, color: AppTheme.primaryText),
              ),
              const SizedBox(height: 16),
              Container(
                decoration: BoxDecoration(
                  color: AppTheme.primaryBackground.withValues(alpha: 0.5),
                  borderRadius: BorderRadius.circular(16),
                  border: Border.all(
                    color: AppTheme.accentColor.withValues(alpha: 0.3),
                  ),
                ),
                child: Column(
                  children: [
                    TextField(
                      controller: _emailController,
                      textInputAction: TextInputAction.next,
                      keyboardType: TextInputType.emailAddress,
                      cursorColor: AppTheme.accentColor,
                      decoration: InputDecoration(
                        hintText: 'email',
                        hintStyle: TextStyle(color: AppTheme.secondaryText),
                        contentPadding: const EdgeInsets.all(12),
                        border: InputBorder.none,
                        prefixIcon: Icon(
                          Icons.email_outlined,
                          color: AppTheme.primaryText,
                          size: 20,
                        ),
                      ),
                      style: const TextStyle(color: AppTheme.primaryText),
                    ),
                    Divider(
                      color: AppTheme.accentColor.withValues(alpha: 0.2),
                      height: 1,
                      thickness: 1,
                    ),
                    TextField(
                      controller: _passwordController,
                      textInputAction: TextInputAction.done,
                      keyboardType: TextInputType.visiblePassword,
                      obscureText: true,
                      obscuringCharacter: '✱',
                      cursorColor: AppTheme.accentColor,
                      decoration: InputDecoration(
                        hintText: 'password',
                        hintStyle: TextStyle(color: AppTheme.secondaryText),
                        contentPadding: const EdgeInsets.all(12),
                        border: InputBorder.none,
                        prefixIcon: Icon(
                          Icons.lock_outline_rounded,
                          color: AppTheme.primaryText,
                          size: 20,
                        ),
                      ),
                      style: const TextStyle(color: AppTheme.primaryText),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 16),
              SizedBox(
                width: 150,
                child: BlocBuilder<AuthBloc, AuthState>(
                  builder: (context, state) {
                    return ModernGlassButton(
                      isLoading: state is AuthLoading,
                      onPressed: () {
                        context.read<AuthBloc>().add(
                          LoginRequested(
                            // _emailController.text,
                            // _passwordController.text,
                          ),
                        );
                      },
                      child: const Text('sign in'),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
