import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tangheem/api/user_api_client.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/classes/utils.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/widgets/common_textfield_widget.dart';

class ForgotPasswordScreen extends StatefulWidget {
  static const String routeName = "/forgot_password";

  ForgotPasswordScreen({Key key}) : super(key: key);

  @override
  _ForgotPasswordScreenState createState() {
    return _ForgotPasswordScreenState();
  }
}

class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
  TextEditingController _emailController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

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

  void forgotPassword(String email) async {
    Utils.showLoading(context);
    try {
      await UserApiClient().forgotPassword(email);
      Utils.showToast("تم إرسال رابط تغيير كلمة المرور إلى بريدك الإلكتروني");
    } catch (ex) {
      if (mounted) Utils.handleException(ex, null);
      Utils.hideLoading(context);
      return;
    } finally {
      Utils.hideLoading(context);
    }
    //getOTP(email);
  }

  void verifyOTP(String email, int otp) async {
    Utils.showLoading(context);
    try {
      await UserApiClient().verifyOTP(email, otp);
    } catch (ex) {
      if (mounted) Utils.handleException(ex, null);
      Utils.hideLoading(context);
      return;
    } finally {
      Utils.hideLoading(context);
    }
    Navigator.pop(context);
    //changePassword(email, otp);
  }

  void updatePassword(String email, int otp, String password) async {
    Utils.showLoading(context);
    try {
      //await UserApiClient().updatePassword(email, otp, password);
    } catch (ex) {
      if (mounted) Utils.handleException(ex, null);
      Utils.hideLoading(context);
      return;
    } finally {
      Utils.hideLoading(context);
    }
    Navigator.pop(context);
    Utils.showToast("تم تغيير كلمة المرور بنجاح");
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorConsts.secondaryWhite,
      body: SingleChildScrollView(
        padding: EdgeInsets.all(32.0),
        physics: BouncingScrollPhysics(),
        child: Container(
          width: MediaQuery.of(context).size.width,
          decoration: BoxDecoration(borderRadius: BorderRadius.circular(8.0), color: Colors.white),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: EdgeInsets.only(top: 36, bottom: 24),
                child: SvgPicture.asset("assets/icons/key.svg", width: 100, height: 100),
              ),
              Text(
                "نسيت كلمة المرور؟",
                style: TextStyle(fontSize: 22, color: ColorConsts.primaryBlue),
              ),
              Container(
                margin: EdgeInsets.only(top: 16),
                width: double.infinity,
                padding: EdgeInsets.all(32.0),
                decoration: BoxDecoration(
                  color: ColorConsts.primaryBlue,
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(8),
                    bottomRight: Radius.circular(8),
                  ),
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    SizedBox(height: 8),
                    CommonTextFieldWidget(hint: "البريد الإلكتروني المسجل", controller: _emailController, prefixIcon: "assets/icons/email.svg"),
                    SizedBox(height: 12),
                    SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: TextButton(
                        onPressed: () {
                          if (_emailController.text.length < 1) {
                            Utils.showToast("يرجى إدخال البريد الإلكتروني");
                            return;
                          } else if (!_emailController.text.isValidEmail()) {
                            Utils.showToast("صيغة البريد الإلكتروني خاطئة");
                            return;
                          }
                          forgotPassword(_emailController.text);
                        },
                        style: TextButton.styleFrom(
                          primary: Colors.white,
                          backgroundColor: ColorConsts.secondaryPink,
                          textStyle: TextStyle(fontSize: 16, fontFamily: "DroidKufi"),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(6.0),
                          ),
                        ),
                        child: Text("إعادة تعيين كلمة المرور"),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

// void getOTP(String email) {
//   showDialog(
//     context: context,
//     barrierColor: ColorConsts.secondaryWhite.withOpacity(0.8),
//     builder: (BuildContext context) => OTPDialog(
//       onOTP: (otp) => verifyOTP(email, otp),
//     ),
//   );
// }
//
// void changePassword(String email, int otp) {
//   showDialog(
//     context: context,
//     barrierColor: ColorConsts.secondaryWhite.withOpacity(0.8),
//     builder: (BuildContext context) => ChangePasswordDialog(
//       onPassword: (password) => updatePassword(email, otp, password),
//     ),
//   );
// }
}
