import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:pharmacy_mobile/auth/auth_service.dart';
import 'package:pharmacy_mobile/widgets/button_widget.dart';
import 'package:pharmacy_mobile/widgets/input_widget.dart';
import 'package:pharmacy_mobile/widgets/page_background_widget.dart';
import 'package:pharmacy_mobile/widgets/text_widget.dart';
import 'package:pharmacy_mobile/widgets/title_widget.dart';

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

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  final _authService = AuthService();
  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();
  final _confirmPasswordController = TextEditingController();
  final FocusNode _focusNode = FocusNode();

  bool _isLoading = false;

  Future<void> _signUp() async {
    final email = _emailController.text;
    final password = _passwordController.text;
    final confirmPassword = _confirmPasswordController.text;

    try {
      await _authService.signUp(email, password);
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error: $e')));
      }
    }

    if (password != confirmPassword) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Password does not match!')));
    }

    // if (mounted) {
    //   context.showSnackBar('Check your email for a login link!');

    //   _emailController.clear();
    // }
  }

  @override
  void dispose() {
    _emailController.dispose();
    _passwordController.dispose();
    _confirmPasswordController.dispose();
    _focusNode.dispose();
    _isLoading = false;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageBackgroundWidget(
          page: 'register',
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                const Gap(96),
                const TitleWidget(firstTextSize: 20, secondTextSize: 32),
                const Gap(32),
                const TextWidget(text: 'Register'),
                const Gap(16),
                Padding(
                  padding: const EdgeInsets.only(left: 32, right: 32),
                  child: Container(
                    padding: EdgeInsets.fromLTRB(32, 32, 32, 40),
                    decoration: BoxDecoration(
                        color: const Color.fromRGBO(57, 38, 62, 0.6),
                        borderRadius: BorderRadius.all(Radius.circular(16)),
                        boxShadow: [
                          BoxShadow(
                            color: const Color.fromRGBO(0, 0, 0, 0.2), // Subtle shadow to give depth
                            spreadRadius: 0,
                            blurRadius: 4,
                            offset: Offset(0, 2),
                          )
                        ]),
                    child: Form(
                        child: Column(
                      children: [
                        InputWidget(label: 'Email', controller: _emailController),
                        const Gap(16),
                        InputWidget(
                          label: 'Password',
                          controller: _passwordController,
                          password: true,
                        ),
                        const Gap(16),
                        InputWidget(
                          label: 'Confirm Password',
                          controller: _confirmPasswordController,
                          password: true,
                        ),
                        const Gap(40),
                        // TextButton(onPressed: () => {_signIn()}, child: const Text('Login'))
                        if (_isLoading)
                          Center(child: CircularProgressIndicator(color: Colors.white))
                        else
                          ButtonWidget(text: 'Create Account', onPressed: _signUp)
                      ],
                    )),
                  ),
                ),
              ],
            ),
          )),
    );
  }
}
