import 'package:autos/core/routing/route_paths.dart';
import 'package:autos/core/theme/app_theme.dart';
import 'package:autos/core/widgets/sso_icon_button.dart';
import 'package:autos/core/widgets/wave_background.dart';
import 'package:autos/presentation/providers/user_provider.dart';
import 'package:autos/presentation/screens/auth/sign_up_screen.dart';
import 'package:autos/presentation/screens/dashboard/dashboard_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fluttertoast/fluttertoast.dart';

class LoginScreen extends ConsumerStatefulWidget {
  const LoginScreen({super.key});

  @override
  ConsumerState<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends ConsumerState<LoginScreen> {
  final emailController = TextEditingController(text: "");

  ///balaputhiyavan4@gmail.com
  final passwordController = TextEditingController(text: ""); //123456
  bool isPasswordVisible = false;

  @override
  void dispose() {
    emailController.dispose();
    passwordController.dispose();
    super.dispose();
  }

  void _showToast(String message, {bool success = true}) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
      backgroundColor: success ? Colors.green : Colors.red,
      textColor: Colors.white,
      fontSize: 16.0,
    );
  }

  @override
  Widget build(BuildContext context) {
    final userState = ref.watch(userProvider);

    final size = MediaQuery.of(context).size;
    final double verticalSpace = size.height * 0.02;
    final double horizontalPadding = size.width * 0.06;

    ref.listen(userProvider, (previous, next) {
      if (next.hasValue && next.value != null) {
        _showToast("Login Successful");
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (_) => const DashboardScreen()),
        );
      } else if (next.hasError) {
        _showToast("⚠️ ${next.error}", success: false);
      }
    });

    return Scaffold(
      resizeToAvoidBottomInset: true,
      body: WaveBackground(
        child: Padding(
          padding: EdgeInsets.symmetric(
            horizontal: horizontalPadding,
            vertical: verticalSpace,
          ),
          child: Center(
            child: SingleChildScrollView(
              physics: const BouncingScrollPhysics(),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(height: size.height * 0.07),

                  /// Logo
                  Image.asset(
                    'assets/auth/autos_transp.png',
                    height: size.height * 0.14,
                  ),

                  /// Sign In Text
                  Row(
                    children: [
                      const Text(
                        "Sign In",
                        style: TextStyle(
                          fontSize: 32,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const Spacer(),
                    ],
                  ),

                  SizedBox(height: verticalSpace),

                  /// Email Field
                  TextField(
                    controller: emailController,
                    decoration: const InputDecoration(
                      hintText: "Email",
                      prefixIcon: Icon(Icons.email_outlined),
                    ),
                  ),

                  SizedBox(height: verticalSpace * 1.5),

                  /// Password Field
                  TextField(
                    controller: passwordController,
                    obscureText: !isPasswordVisible,
                    decoration: InputDecoration(
                      hintText: "Password",
                      prefixIcon: const Icon(Icons.lock_outline),
                      suffixIcon: IconButton(
                        onPressed: () {
                          setState(() {
                            isPasswordVisible = !isPasswordVisible;
                          });
                        },
                        icon: Icon(
                          isPasswordVisible
                              ? Icons.visibility
                              : Icons.visibility_off,
                        ),
                      ),
                    ),
                  ),

                  /// Forgot Password
                  Row(
                    children: [
                      const Spacer(),
                      TextButton(
                        onPressed: () {
                          Navigator.pushNamed(
                            context,
                            AppRoutePaths.forgotPassword,
                          );
                        },
                        child: Text(
                          "Forgot Password?",
                          style: Theme.of(context).textTheme.bodySmall,
                        ),
                      ),
                    ],
                  ),

                  SizedBox(height: verticalSpace * 1.5),

                  /// Login Button
                  userState.isLoading
                      ? const CircularProgressIndicator()
                      : ElevatedButton(
                          onPressed: () async {
                            await ref
                                .read(userProvider.notifier)
                                .login(
                                  emailController.text.trim(),
                                  passwordController.text.trim(),
                                );
                          },
                          style: ElevatedButton.styleFrom(
                            backgroundColor: AppTheme.primary,
                            minimumSize: Size(
                              double.infinity,
                              size.height * 0.05,
                            ),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(40),
                            ),
                          ),
                          child: const Text(
                            "Sign In",
                            style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: Colors.white,
                            ),
                          ),
                        ),

                  SizedBox(height: verticalSpace * 1.5),

                  /// Divider or
                  Row(
                    children: const [
                      Expanded(
                        child: Divider(
                          thickness: 0.5,
                          color: Colors.grey,
                          endIndent: 10,
                        ),
                      ),
                      Text("or"),
                      Expanded(
                        child: Divider(
                          thickness: 0.5,
                          color: Colors.grey,
                          indent: 10,
                        ),
                      ),
                    ],
                  ),

                  SizedBox(height: verticalSpace * 1.5),

                  /// SSO Buttons
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Flexible(
                        child: SsoIconButton(
                          assetPath: 'assets/auth/sso-google.png',
                          shadow: const BoxShadow(
                            color: Colors.black12,
                            blurRadius: 12,
                          ),
                          onPressed: () {},
                        ),
                      ),
                      SizedBox(width: size.width * 0.1),
                      Flexible(
                        child: SsoIconButton(
                          assetPath: 'assets/auth/sso-apple.png',
                          shadow: const BoxShadow(
                            color: Colors.black12,
                            blurRadius: 12,
                          ),
                          onPressed: () {},
                        ),
                      ),
                    ],
                  ),

                  SizedBox(height: verticalSpace * 1),

                  /// Sign Up Link
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        "Don't have an account? ",
                        style: Theme.of(context).textTheme.bodyMedium,
                      ),
                      TextButton(
                        onPressed: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (_) => const SignUpScreen(),
                            ),
                          );
                        },
                        child: Text(
                          "Sign Up",
                          style: TextStyle(
                            color: Theme.of(context).colorScheme.primary,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ],
                  ),

                  SizedBox(height: verticalSpace * 2),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
