import 'package:car_provider_app/api/client/user_api_client.dart';

import 'package:car_provider_app/classes/utils.dart';
import 'package:car_provider_app/config/routes.dart';
import 'package:car_provider_app/generated/locale_keys.g.dart';
import 'package:car_provider_app/models/user/confirm_password.dart';
import 'package:car_provider_app/utils/navigator.dart';
import 'package:car_provider_app/utils/utils.dart';
import 'package:car_provider_app/widgets/app_bar.dart';
import 'package:car_provider_app/widgets/dialog/dialogs.dart';
import 'package:car_provider_app/widgets/dialog/message_dialog.dart';
import 'package:car_provider_app/widgets/button/show_fill_button.dart';
import 'package:car_provider_app/extensions/string_extensions.dart';
import 'package:car_provider_app/extensions/int_extensions.dart';
import 'package:car_provider_app/widgets/txt_field.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';

import 'dart:convert';
import 'package:http/http.dart';

class ConfirmNewPasswordPage extends StatefulWidget {
  String userToken;

  ConfirmNewPasswordPage(this.userToken, {Key? key}) : super(key: key);

  @override
  State<ConfirmNewPasswordPage> createState() => _ConfirmNewPasswordPageState();
}

class _ConfirmNewPasswordPageState extends State<ConfirmNewPasswordPage> {
  String newPassword = "";
  String confirmPassword = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context,title:LocaleKeys.forgetPassword.tr()),
      body: SingleChildScrollView(
         child: Container(
          // width: double.infinity,
          // height: double.infinity,
          padding: EdgeInsets.all(40),
          child: Column(
            children: [
              LocaleKeys.newPassword.tr().toText24(),
              12.height,
              TextFormField(
                decoration: InputDecoration(
                  hintText: LocaleKeys.enterNewPassword.tr(),
                  hintStyle: TextStyle(color: Colors.grey),
                  border:  OutlineInputBorder(
                    borderRadius: const BorderRadius.all(
                      const Radius.circular(5.0),
                    ),
                  ),
                ),
                obscureText: true,
                onChanged: (v) => newPassword = v,
              ),
              12.height,
              TextFormField(
                decoration: InputDecoration(
                  hintText: LocaleKeys.confirmPassword.tr(),
                  hintStyle: TextStyle(color: Colors.grey),
                  border:  OutlineInputBorder(
                  borderRadius: const BorderRadius.all(
                      const Radius.circular(5.0),
                    ),
                  ),
                ),
                obscureText: true,
                onChanged: (v) => confirmPassword = v,
              ),
              40.height,
              ShowFillButton(
                title: LocaleKeys.confirm.tr(),
                maxWidth: double.infinity,
                onPressed: () {
                  if (validation()) confirmPasswordOTP(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> confirmPasswordOTP(BuildContext context) async {
    if(validateStructure(newPassword??"")){
    Utils.showLoading(context);
    Response res = await UserApiClent().ForgetPassword(widget.userToken, newPassword);
    Utils.hideLoading(context);
    ConfirmPassword data = ConfirmPassword.fromJson(jsonDecode(res.body));
    if (data.messageStatus == 1) {
      Utils.showToast(LocaleKeys.passwordIsUpdated.tr());
        //("Password is Updated");
      navigateWithName(context, AppRoutes.loginWithPassword);
    } else {
      Utils.showToast(data.message ?? "");
    }
    }else{
      Utils.showToast(LocaleKeys.passwordShouldContains.tr());
        //("Password Should contains Character, Number, Capital and small letters");
    }
  }

  bool validation() {
    bool isValid = true;
    if (newPassword != confirmPassword) {
      Utils.showToast("Confirmation password does not match the entered password");
      isValid = false;
    }
    return isValid;
  }

  bool validateStructure(String value){
    String  pattern = r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{6,}$';
    RegExp regExp = new RegExp(pattern);
    return regExp.hasMatch(value);
  }
}
