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

import 'package:car_customer_app/classes/utils.dart';
import 'package:car_customer_app/config/routes.dart';
import 'package:car_customer_app/generated/locale_keys.g.dart';
import 'package:car_customer_app/models/user/change_password.dart';
import 'package:car_customer_app/models/user/confirm_password.dart';
import 'package:car_customer_app/utils/navigator.dart';
import 'package:car_customer_app/utils/utils.dart';
import 'package:car_customer_app/widgets/app_bar.dart';
import 'package:car_customer_app/widgets/dialog/dialogs.dart';
import 'package:car_customer_app/widgets/dialog/message_dialog.dart';
import 'package:car_customer_app/widgets/show_fill_button.dart';
import 'package:car_customer_app/extensions/string_extensions.dart';
import 'package:car_customer_app/extensions/int_extensions.dart';
import 'package:car_customer_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';

import '../../models/m_response.dart';

class ChangePasswordPage extends StatefulWidget {
  ChangePasswordPage({Key? key}) : super(key: key);

  @override
  State<ChangePasswordPage> createState() => _ChangePasswordPageState();
}

class _ChangePasswordPageState extends State<ChangePasswordPage> {
  String newPassword = "";
  String currentPasswor = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context, title: LocaleKeys.changePassword.tr()),
      body: SingleChildScrollView(
        child: Container(
          // width: double.infinity,
          // height: double.infinity,
          padding: EdgeInsets.all(40),
          child: Column(
            children: [
              LocaleKeys.enterNewPassword.tr().toText24(),
              12.height,
              TxtField(
                hint: LocaleKeys.enterOldPassword.tr(),
                onChanged: (v) => currentPasswor = v,
              ),
              12.height,
              TxtField(
                hint: LocaleKeys.enterNewPassword.tr(),
                onChanged: (v) => newPassword = v,
              ),
              20.height,
              ShowFillButton(
                title: LocaleKeys.confirm.tr(),
                maxWidth: double.infinity,
                onPressed: () {
                  changePassword(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> changePassword(BuildContext context) async {
    if (validateStructure(newPassword)) {
      Utils.showLoading(context);
      MResponse res = await UserApiClent().ChangePassword(currentPasswor, newPassword);
      Utils.hideLoading(context);
      if (res.messageStatus == 1) {
        Utils.showToast(LocaleKeys.passwordIsUpdated.tr());
        //("Password is Updated");
        // navigateWithName(context, AppRoutes.loginWithPassword);
        Navigator.of(context).pushNamedAndRemoveUntil(AppRoutes.loginWithPassword, (Route<dynamic> route) => false);
      } else {
        Utils.showToast(res.message ?? "");
      }
    } else {
      Utils.showToast(LocaleKeys.passwordShouldContains.tr());
      //("Password Should contains Character, Number, Capital and small letters");
    }
  }

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