import 'dart:convert';
import 'package:car_provider_app/api/client/user_api_client.dart';
import 'package:car_provider_app/classes/utils.dart';
import 'package:car_provider_app/config/constants.dart';
import 'package:car_provider_app/config/routes.dart';
import 'package:car_provider_app/extensions/int_extensions.dart';
import 'package:car_provider_app/extensions/string_extensions.dart';
import 'package:car_provider_app/generated/locale_keys.g.dart';
import 'package:car_provider_app/models/user/forget_password_otp_compare.dart';
import 'package:car_provider_app/utils/navigator.dart';
import 'package:car_provider_app/utils/utils.dart';
import 'package:car_provider_app/widgets/app_bar.dart';
import 'package:car_provider_app/widgets/button/show_image_button.dart';
import 'package:car_provider_app/widgets/dialog/dialogs.dart';
import 'package:car_provider_app/widgets/dialog/otp_dialog.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';

class ForgetPasswordMethodPage extends StatefulWidget {
  String userToken;

  ForgetPasswordMethodPage(this.userToken);

  @override
  State<ForgetPasswordMethodPage> createState() => _ForgetPasswordMethodPageState();
}

class _ForgetPasswordMethodPageState extends State<ForgetPasswordMethodPage> {
  int otpType = 1;
  String userOTP = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context, title: LocaleKeys.forgetPassword.tr()),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        padding: EdgeInsets.all(40),
        child: Column(
          children: [
            LocaleKeys.selectMethod.tr().toText24(),
            12.height,
            Row(
              children: [
                Expanded(
                  child: ShowImageButton(
                    onClick: () {
                      otpType = 1;
                      forgetPasswordOTPMethod(context);
                    },
                    title: LocaleKeys.SMS.tr(),
                    icon: icons + "ic_sms.svg",
                  ),
                ),
                20.width,
                Expanded(
                  child: ShowImageButton(
                    onClick: () {
                      otpType = 1;
                      forgetPasswordOTPMethod(context);
                    },
                    title: LocaleKeys.whatsapp.tr(),
                    icon: icons + "ic_whatsapp.svg",
                  ),
                ),
              ],
            ),
            mFlex(10),
          ],
        ),
      ),
    );
  }

  Future<void> forgetPasswordOTPMethod(BuildContext context) async {
    showMDialog(context, child: OtpDialog(
      onClick: (String code) async {
        pop(context);
        Utils.showLoading(context);
        Response res = await UserApiClent().ForgetPasswordOTPCompare(widget.userToken ?? "", code);
        Utils.hideLoading(context);
        PasswordOTPCompare otpCompare = PasswordOTPCompare.fromJson(jsonDecode(res.body));
        if (otpCompare.messageStatus == 1) {
          var userToken = otpCompare.data!.userToken;
          navigateWithName(context, AppRoutes.confirmNewPasswordPage, arguments: userToken);
        } else {
          Utils.showToast(otpCompare.message ?? "");
        }
      },
    ));
  }
}
