import 'package:car_customer_app/api/client/user_api_client.dart';
import 'package:car_customer_app/classes/utils.dart';
import 'package:car_customer_app/config/routes.dart';
import 'package:car_customer_app/extensions/int_extensions.dart';
import 'package:car_customer_app/extensions/string_extensions.dart';
import 'package:car_customer_app/generated/locale_keys.g.dart';
import 'package:car_customer_app/models/user/change_email.dart';
import 'package:car_customer_app/models/user/confirm_email.dart';
import 'package:car_customer_app/utils/navigator.dart';
import 'package:car_customer_app/widgets/app_bar.dart';
import 'package:car_customer_app/widgets/dialog/dialogs.dart';
import 'package:car_customer_app/widgets/dialog/message_dialog.dart';
import 'package:car_customer_app/widgets/dialog/otp_dialog.dart';
import 'package:car_customer_app/widgets/show_fill_button.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';

import 'dart:convert';
import 'package:http/http.dart';

import '../../widgets/txt_field.dart';

class ChangeEmailPage extends StatefulWidget {
  @override
  State<ChangeEmailPage> createState() => _ChangeEmailPageState();
}

class _ChangeEmailPageState extends State<ChangeEmailPage> {
  String password = "";
  String email = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context, title: LocaleKeys.changeEmail.tr()),
      body: SingleChildScrollView(
        child: Container(
          // width: double.infinity,
          // height: double.infinity,
          padding: EdgeInsets.all(20),
          child: Column(
            children: [
              LocaleKeys.enterEmail.tr().toText24(),
              12.height,
              TxtField(
                hint: LocaleKeys.enterNewEmail.tr(),
                onChanged: (v) => email = v,
              ),
              12.height,
              TxtField(
                hint: LocaleKeys.enterCurrentPassword.tr(),
                onChanged: (v) => password = v,
              ),
              40.height,
              ShowFillButton(
                title: LocaleKeys.confirm.tr(),
                maxWidth: double.infinity,
                onPressed: () {
                  changeEmail(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> changeEmail(BuildContext context) async {
    Utils.showLoading(context);
    ChanEmail otpRequest = await UserApiClent().ChangeEmailOTPRequest(email, password);
    Utils.hideLoading(context);
    if (otpRequest.messageStatus == 1) {
      showMDialog(context, child: OtpDialog(
        onClick: (String code) async {
          pop(context);
          Utils.showLoading(context);
          ConfirmEmail otpCompare = await UserApiClent().ChangeEmail(otpRequest.data!.userToken ?? "", code);
          Utils.hideLoading(context);
          if (otpCompare.messageStatus == 1) {
            showMDialog(
              context,
              child: MessageDialog(
                title: LocaleKeys.emailChangedSuccessfully.tr(),
                //"Email is Changed Successfully",
                onClick: () {
                  Navigator.of(context).pushNamedAndRemoveUntil(AppRoutes.loginWithPassword, (Route<dynamic> route) => false);
                },
              ),
            );
          } else {
            Utils.showToast(otpCompare.message ?? "");
          }
        },
      ));
    } else {
      Utils.showToast(otpRequest.message ?? "");
    }
  }
}
