
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_mobile.dart';
import 'package:car_customer_app/models/user/confirm_mobile.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 ChangeMobilePage extends StatefulWidget {

  @override
  State<ChangeMobilePage> createState() => _ChangeMobilePageState();
}

class _ChangeMobilePageState extends State<ChangeMobilePage> {
  int countryID=1 ;
  String mobileNo = '';
  String password = '';

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

  Future<void> changeMobile(BuildContext context) async {
    Utils.showLoading(context);
    ChangeMobile otpRequest = await UserApiClent().ChangeMobileNoOTPRequest(countryID, mobileNo, password);
    Utils.hideLoading(context);
    if (otpRequest.messageStatus == 1) {
      showMDialog(context, child: OtpDialog(
        onClick: (String code) async {
          pop(context);
          Utils.showLoading(context);
          ConfirmMobile otpCompare = await UserApiClent().ChangeMobileNo(otpRequest.data!.userToken ?? "", code);
          Utils.hideLoading(context);
          if (otpCompare.messageStatus == 1) {
            showMDialog(
              context,
              child: MessageDialog(
                title:LocaleKeys.phoneNumberVerified.tr(),
                //"Phone Number Verified",
                onClick: () {
                  Navigator.of(context).pushNamedAndRemoveUntil(AppRoutes.loginWithPassword, (Route<dynamic> route) => false);
                },
              ),
            );
          } else {
            Utils.showToast(otpCompare.message ?? "");
          }
        },
      ));
    } else {
      Utils.showToast(otpRequest.message ?? "");
    }
  }



}
