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/generated/locale_keys.g.dart';
import 'package:car_provider_app/models/user/basic_otp.dart';
import 'package:car_provider_app/models/user/register_user.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/message_dialog.dart';
import 'package:car_provider_app/widgets/dialog/otp_dialog.dart';
import 'package:car_provider_app/extensions/int_extensions.dart';
import 'package:car_provider_app/extensions/string_extensions.dart';
import 'package:car_provider_app/extensions/widget_extensions.dart';
import 'package:car_provider_app/widgets/txt_field.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';

class LoginVerifyAccountPage extends StatelessWidget {
  int otpType = 1;
  String phoneNum = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context,title: LocaleKeys.login.tr()),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        padding: EdgeInsets.all(40),
        child: Column(
          children: [
            LocaleKeys.verifyAccount.tr().toText24(),
            mFlex(1),
            TxtField(
              hint: "966501234567",
              value: phoneNum,
              onChanged: (v) {
                phoneNum = v;
              },
            ),
            mFlex(2),
            Row(
              children: [
                Expanded(
                  child: ShowImageButton(
                    onClick: () {
                      otpType = 1;
                      performBasicOtp(context);
                      // showMDialog(context, child: OtpDialog(
                      //   onClick: (String code) {
                      //     pop(context);
                      //     delay(300).then(
                      //       (value) => showMDialog(
                      //         context,
                      //         child: MessageDialog(
                      //           title: "Phone Number Verified",
                      //           onClick: () {
                      //             otpType=1;
                      //             performBasicOtp(context);
                      //             // navigateWithName(context, AppRoutes.completeProfile);
                      //           },
                      //         ),
                      //       ),
                      //     );
                      //   },
                      // ));
                    },
                    title: 'With SMS',
                    icon: icons + "ic_sms.png",
                  ),
                ),
                20.width,
                Expanded(
                  child: ShowImageButton(
                    onClick: () {
                      otpType = 1;
                      performBasicOtp(context);
                      // showMDialog(context, child: OtpDialog(
                      //   onClick: (String code) {
                      //     pop(context);
                      //     delay(300).then(
                      //       (value) => showMDialog(
                      //         context,
                      //         child: MessageDialog(
                      //           title: "Phone Number Verified",
                      //           onClick: () {
                      //             otpType=2;
                      //             performBasicOtp(context);
                      //             // navigateWithName(context, AppRoutes.completeProfile);
                      //           },
                      //         ),
                      //       ),
                      //     );
                      //   },
                      // ));
                    },
                    title: 'With Whatsapp',
                    icon: icons + "ic_whatsapp.png",
                  ),
                ),
              ],
            ),
            mFlex(10),
          ],
        ),
      ),
    );
  }

  Future<void> performBasicOtp(BuildContext context) async {
    Utils.showLoading(context);
    BasicOtp basicOtp = await UserApiClent().basicOtp(phoneNum, otpType: otpType);
    Utils.hideLoading(context);
    if (basicOtp.messageStatus == 1) {
      showMDialog(context, child: OtpDialog(
        onClick: (String code) async {
          pop(context);
          Utils.showLoading(context);
          RegisterUser user = await UserApiClent().basicVerify(phoneNum, code, basicOtp.data!.userToken ?? "");
          Utils.hideLoading(context);
          if (user.messageStatus == 1) {
            Utils.showToast(user.message ?? "");
            showMDialog(
              context,
              child: MessageDialog(
                title: LocaleKeys.phoneNumberVerified.tr(),
                onClick: () {
                  pop(context);
                  navigateWithName(context, AppRoutes.profile1, arguments: user);
                },
              ),
            );
          } else {
            Utils.showToast(user.message ?? "");
          }
        },
      ));
    } else {
      Utils.showToast(basicOtp.message ?? "");
    }
  }
}
