import 'package:cheminova/widgets/custom_button.dart';
import 'package:cheminova/widgets/input_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import '../../utils/show_snackbar.dart';
import 'controller/auth_controller.dart';


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

  @override
  State<ChangePasswordScreen> createState() => _ChangePasswordScreenState();
}

class _ChangePasswordScreenState extends State<ChangePasswordScreen> {
  final authController = Get.put(AuthController());


  void dispose() {
    authController.currentpassController.dispose();
    authController.newpassController.dispose();
    authController.confirmpassController.dispose();
    super.dispose();
  }

  void validateAndChangePassword() async {
    String oldPassword = authController.currentpassController.text.trim();
    String newPassword = authController.newpassController.text.trim();
    String confirmPassword = authController.confirmpassController.text.trim();

    if (newPassword.isEmpty || confirmPassword.isEmpty || oldPassword.isEmpty) {
      showSnackbar('All fields are required');
      return;
    }

    if (newPassword.length < 8) {
      showSnackbar('Password must be at least 8 characters long');
      return;
    }

    if (!RegExp(r'[!@#$%^&*(),.?":{}|<>]').hasMatch(newPassword)) {
      showSnackbar('Password must contain at least one special character');
      return;
    }


    if (newPassword != confirmPassword) {
      showSnackbar('New Password and Confirm Password do not match');
      return;
    }


    authController.changePassword();
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,

        elevation: 0,
        leading: GestureDetector(
          onTap: () => Get.back(),
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: SizedBox(
              width: 20,
                height: 20,
                child: SvgPicture.asset('assets/svg/menu.svg')),
          ),
        ),
        actions: [
          GestureDetector(
            onTap: () => Get.back(),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: SvgPicture.asset('assets/svg/back_arrow.svg'),
            ),
          ),
        ],
      ),
      body: Stack(
        alignment: Alignment.topCenter,
        children: [
          Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.cover,
                image: AssetImage(
                  'assets/images/image_1.png',
                ),
              ),
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(50.0),
                bottomRight: Radius.circular(50.0),
              ),
            ),
            child: SizedBox(
              width: Get.width,
              height: Get.height * 0.7,
            ),
          ),
          Center(
            child: SingleChildScrollView(
              child: Card(
                margin: const EdgeInsets.symmetric(horizontal: 24),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(19),
                  side: const BorderSide(color: Color(0xFFFDFDFD)),
                ),
                color: const Color(0xFFB4D1E5).withOpacity(0.9),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    children: [

                      Container(
                        padding: const EdgeInsets.only(bottom: 10),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          'Change Password',
                          style: GoogleFonts.getFont(
                            'Roboto',
                            fontWeight: FontWeight.w500,
                            fontSize: 30,
                            height: 1.2,
                          ),
                        ),
                      ),
                      InputField(
                        hintText: "Current Password",
                        labelText: "Current Password",
                        controller: authController.currentpassController,
                        obscureText: true,
                        keyboardType: TextInputType.text,
                      ),

                      const SizedBox(height: 15),
                      InputField(
                        hintText: "New Password",
                        labelText: "New Password",
                        obscureText: true,
                        controller: authController.newpassController,
                        keyboardType: TextInputType.text,
                      ),
                      const SizedBox(height: 15),
                      InputField(
                        hintText: "Confirm Password",
                        labelText: "Confirm Password",
                        obscureText: true,
                        controller: authController.confirmpassController,
                        keyboardType: TextInputType.text,
                      ),
                      const SizedBox(height: 30),
                      CustomButton(
                        text: "RESET PASSWORD",
                        onPressed: () async {

                       validateAndChangePassword();

                        },
                        isLoading: authController.isLoading.value,
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
