import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/login_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/dialogs/otp_dialog.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/basic_member_information_model.dart';
import 'package:mohem_flutter_app/models/generic_response_model.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/input_widget.dart';

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

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

class _ForgotPasswordScreenState extends State<ForgotPasswordScreen> {
  TextEditingController employeeId = TextEditingController();
  BasicMemberInformationModel? _basicMemberInformation;
  GenericResponseModel? genericResponseModel;

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

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

  void performForgotPassword() async {
    if (employeeId.text.isEmpty) {
      return;
    }
    Utils.showLoading(context);
    try {
      _basicMemberInformation = await LoginApiClient().getBasicUserInformation("CS", employeeId.text);
      genericResponseModel = await LoginApiClient().sendPublicActivationCode(_basicMemberInformation?.pMOBILENUMBER, employeeId.text);
      Utils.hideLoading(context);
      OtpDialog(
        context,
        1,
        int.tryParse(_basicMemberInformation?.pMOBILENUMBER ?? ""),
        (value,TextEditingController _pinPutController) async {
          Utils.showLoading(context);
         try{
           GenericResponseModel? genericResponseModel = await LoginApiClient().checkPublicActivationCode(value, employeeId.text);
           if (genericResponseModel?.errorMessage != null) {
             Utils.showToast(genericResponseModel?.errorMessage ?? "");
             return;
           }
           Utils.hideLoading(context);
           await Navigator.pushNamed(context, AppRoutes.newPassword, arguments: employeeId.text);
           Navigator.pop(context);
           Navigator.pop(context);
         }catch(ex){
           print(ex);
           _pinPutController.clear();
           otpFieldClear.value = "";
           Utils.hideLoading(context);
           Utils.handleException(ex, context, null);
         }
        },
        () => {
          Navigator.pop(context),
        },
        onResendCode: () {
          performForgotPassword();
        },
      ).displayDialog(context);
    } catch (ex) {
      print(ex);
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: MyColors.darkIconColor),
          onPressed: () => Navigator.pop(context),
        ),
      ),
      body: Column(
        children: [
          //const SizedBox(height: 23),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(21.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Expanded(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        LocaleKeys.forgotPassword.tr().toText24(isBold: true),
                        LocaleKeys.loginCodeWillSentToMobileNumber.tr().toText16(),
                        16.height,
                        InputWidget(
                          LocaleKeys.employeeId.tr(),
                          "123456",
                          employeeId,
                          onChange: (value) {
                            setState(() {});
                          },
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
          DefaultButton(
                  LocaleKeys.changePassword.tr(),
                  employeeId.text.isEmpty
                      ? null
                      : () async {
                          performForgotPassword();
                        })
              .insideContainer
        ],
      ),
    );
  }
}
