import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/new_models/general_response_model.dart';
import 'package:test_sa/models/new_models/update_password.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/new_views/common_widgets/app_text_form_field.dart';
import 'package:test_sa/new_views/pages/login_page.dart';

import '../../controllers/providers/api/user_provider.dart';
import '../../controllers/validator/validator.dart';

class ResetPasswordView extends StatefulWidget {
  static const String routeName = "/resetPassword";

  ResetPasswordView({Key? key}) : super(key: key);

  @override
  State<ResetPasswordView> createState() => _ResetPasswordViewState();
}

class _ResetPasswordViewState extends State<ResetPasswordView> {
  bool _passwordVisible = false;
  bool _confirmPasswordVisible = false;

  String newPassword = "";
  String confirmPassword = "";

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Scaffold(
        body: Column(
          children: [
            SingleChildScrollView(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  context.translation.resetPassword.heading2(context).custom(color: AppColor.white936, fontWeight: FontWeight.w500),
                  8.height,
                  context.translation.pleaseEnterTheNewPassword.bodyText2(context).custom(color: AppColor.neutral120, fontWeight: FontWeight.w500),
                  32.height,
                  AppTextFormField(
                    initialValue: "",
                    showBorder: true,
                    validator: (value) => Validator.hasValue(value) ? null : context.translation.requiredField,
                    labelText: context.translation.password,
                    obscureText: !_passwordVisible,
                    suffixIcon: Icon(
                      // Based on passwordVisible state choose the icon
                      _passwordVisible ? Icons.visibility : Icons.visibility_off,
                      color: context.isDark ? AppColor.neutral10 : AppColor.neutral20,
                    ).paddingOnly(end: 12).onPress(() {
                      setState(() {
                        _passwordVisible = !_passwordVisible;
                      });
                    }),
                    onSaved: (value) {
                      newPassword = value;
                    },
                  ),
                  16.height,
                  AppTextFormField(
                    initialValue: "",
                    labelText: context.translation.confirmPassword,
                    obscureText: !_confirmPasswordVisible,
                    showBorder: true,
                    validator: (value) => Validator.isValidPassword(value!)
                        ? null
                        : value.isEmpty
                            ? context.translation.requiredField
                            : context.translation.passwordLengthMessage,
                    onSaved: (value) {
                      confirmPassword = value;
                    },
                    suffixIcon: Icon(
                      _confirmPasswordVisible ? Icons.visibility : Icons.visibility_off,
                      color: context.isDark ? AppColor.neutral10 : AppColor.neutral20,
                    ).paddingOnly(end: 12).onPress(() {
                      setState(() {
                        _confirmPasswordVisible = !_confirmPasswordVisible;
                      });
                    }),
                  ),
                ],
              ),
            ).center.expanded,
            AppFilledButton(label: context.translation.resetPassword, maxWidth: true, onPressed: () => _resetPassword(context)),
          ],
        ).paddingOnly(start: 16, end: 16, bottom: 24, top: 24),
      ),
    );
  }

  Future<void> _resetPassword(BuildContext context) async {
    if (!_formKey.currentState!.validate()) return;
    _formKey.currentState?.save();
    if (newPassword != confirmPassword) {
      Fluttertoast.showToast(msg: 'Password not matched try again');
    } else {
      UserProvider userProvider = Provider.of<UserProvider>(context,listen: false);
      UpdatePasswordModel updatePasswordModel =
          UpdatePasswordModel(password: newPassword, confirmPassword: confirmPassword, userId: userProvider.verifyOtpModel.userId, token: userProvider.verifyOtpModel.token);
      GeneralResponseModel generalResponseModel = await userProvider.updateNewPassword(context: context, updatePasswordModel: updatePasswordModel);
      if (generalResponseModel.isSuccess==true) {
        newPassword = '';
        confirmPassword = '';
        Navigator.of(context).pushNamedAndRemoveUntil(LoginPage.routeName, (routes) => true);
      }
    }
  }
}
