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/int_extensions.dart';
import 'package:tangheem/extensions/string_extensions.dart';
import 'package:tangheem/extensions/widget_extensions.dart';
import 'package:tangheem/ui/screens/contact_us_screen.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) {
    bool isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;

    Widget _button = 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(
          backgroundColor: ColorConsts.darkText,
          foregroundColor: Colors.white,
          textStyle: TextStyle(fontSize: 18),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(25.0),
          ),
        ),
        child: Text("إعادة تعيين كلمة المرور"),
      ),
    ).paddingOnly(left: isPortrait ? 11 : 0, right: isPortrait ? 11 : 0, bottom: 28);

    Widget _contactUs = Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        SvgPicture.asset("assets/icons/new/contact_us.svg", width: 26, height: 24, color: ColorConsts.greyC4Color),
        8.height,
        "اتصل بنا".toText(14, color: ColorConsts.greyC4Color),
      ],
    ).onPress(() {
      Navigator.pushNamed(context, ContactUsScreen.routeName);
    });

    return Scaffold(
      backgroundColor: ColorConsts.greyF8Color,
      body: isPortrait
          ? SingleChildScrollView(
              padding: EdgeInsets.only(top: 100, bottom: 24, left: 24, right: 24),
              physics: BouncingScrollPhysics(),
              child: Column(
                children: [
                  Image.asset("assets/icons/new/Tangeem-logo.png", width: 116, height: 62),
                  30.height,
                  Container(
                    width: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0), color: Colors.white),
                    margin: EdgeInsets.only(top: 6, bottom: 20),
                    padding: EdgeInsets.only(top: 50, bottom: 20, left: 11, right: 11),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        SvgPicture.asset("assets/icons/reset.svg", width: 100, height: 72),
                        25.height,
                        "استعادة كلمة المرور".toText(18, color: ColorConsts.darkText),
                        18.height,
                        "يمكنك إعادة تعيين كلمة المرور عبر حساب بريدك الإلكتروني".toText(13, color: ColorConsts.grey9FColor, textAlign: TextAlign.center).paddingOnly(left: 77, right: 77),
                        25.height,
                        CommonTextFieldWidget(hint: "البريد الإلكتروني المسجل", controller: _emailController, isEmail: true),
                      ],
                    ),
                  ),
                  _button,
                  _contactUs,
                ],
              ),
            )
          : SizedBox(
              height: MediaQuery.of(context).size.height,
              child: Row(
                children: [
                  Expanded(flex: 1, child: Image.asset("assets/icons/new/Tangeem-logo.png", width: 136, height: 82).paddingOnly(right: 40)),
                  Expanded(
                    flex: 2,
                    child: Container(
                      width: MediaQuery.of(context).size.width,
                      //   height: MediaQuery.of(context).size.height,
                      decoration: BoxDecoration(borderRadius: BorderRadius.circular(20.0), color: Colors.white),
                      margin: EdgeInsets.all(24),
                      padding: EdgeInsets.only(left: 24, right: 0),
                      child: Row(
                        children: [
                          Expanded(
                            flex: 2,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                SvgPicture.asset("assets/icons/reset.svg", width: 120, height: 92),
                                25.height,
                                "استعادة كلمة المرور".toText(18, color: ColorConsts.darkText, fontFamily: false),
                              ],
                            ),
                          ),
                          Expanded(
                            flex: 3,
                            child: SingleChildScrollView(
                              physics: BouncingScrollPhysics(),
                              padding: EdgeInsets.only(
                                top: 50,
                                bottom: 50,
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  "يمكنك إعادة تعيين كلمة المرور عبر حساب بريدك الإلكتروني".toText(18, color: ColorConsts.grey9FColor, textAlign: TextAlign.center),
                                  24.height,
                                  CommonTextFieldWidget(hint: "البريد الإلكتروني المسجل", controller: _emailController, isEmail: true),
                                  12.height,
                                  _button,
                                  _contactUs
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
    );
  }
}
