import 'package:elektronika/app/pages/auth/otp/otp_screen_new.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../app.dart';

class ForgotPasswordDialog extends StatefulWidget {
  @override
  State<ForgotPasswordDialog> createState() => _ForgotPasswordDialogState();
}

class _ForgotPasswordDialogState extends State<ForgotPasswordDialog> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  TextEditingController phoneCtrl = new TextEditingController();

  bool isLoading = false;

  Future<void> _onSubmit() async {
    if (formKey.currentState!.validate()) {
      KeyboardUtil.hideKeyboard(context);

      final maskedPhoneNumber = phoneCtrl.value.text;
      final phone = maskedPhoneNumber.replaceAll(RegExp(r'\D+'), '');

      final Map<String, dynamic> params = {
        'phone': phone,
      };

      setState(() => isLoading = true);
      // final verificationCode = await AuthApi.getVerificationCode(phone);
      final isOk = await AuthApi.resetPassword(params);
      setState(() => isLoading = false);

      if (!isOk) {
        showSnack('error'.tr, 'can_not_send_sms'.tr, SnackType.ERROR);
        await Future.delayed(Duration(seconds: 4));
        return;
      }

      /* Get.to(
        () => OTPPage(
          params: params,
          verificationCode: 'verificationCode',
          isRegister: false,
        ),
      ); */
      Get.back();
      Get.to(
        () => OTPPageNew(params: params),
      );
    }
  }

  @override
  void dispose() {
    phoneCtrl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(8.0),
        ),
      ),
      titlePadding: EdgeInsets.zero,
      scrollable: true,
      title: Container(
        padding: const EdgeInsets.symmetric(vertical: 22, horizontal: 16),
        decoration: new BoxDecoration(
          color: ThemeColor.color3A3A3A,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(8),
            topRight: Radius.circular(8),
          ),
        ),
        child: Row(
          children: [
            Text(
              'recover_password'.tr,
              style: new TextStyle(
                color: ThemeColor.white,
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            Spacer(),
            GestureDetector(
              onTap: Get.back,
              child: Icon(
                Icons.close,
                color: ThemeColor.white,
                size: 18.sp,
              ),
            ),
          ],
        ),
      ),
      contentPadding: const EdgeInsets.all(16),
      content: Form(
        key: formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            AppTheme.appSizeDivider15H,
            Text('phone_number'.tr, style: AppTheme.styleColorC4C4),
            AppTheme.appSizeDivider15H,
            MyTextFormField(
              controller: phoneCtrl,
              keyboardType: TextInputType.phone,
              hasPrefixIcon: true,
              inputFormatters: [maskFormatterPhone],
            ),
            AppTheme.appSizeDivider15H,
            AppTheme.appSizeDivider15H,
            isLoading
                ? CustomLoader()
                : ColoredButton(
                    title: 'send'.tr,
                    callback: _onSubmit,
                    btnColor: ThemeColor.mainColor,
                  ),
          ],
        ),
      ),
    );
  }
}
