import 'dart:convert';

import 'package:car_provider_app/api/client/user_api_client.dart';
import 'package:car_provider_app/classes/app_state.dart';
import 'package:car_provider_app/classes/colors.dart';
import 'package:car_provider_app/classes/utils.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/models/m_response.dart';
import 'package:car_provider_app/models/user/verify_email.dart';
import 'package:car_provider_app/models/user/verify_email_otp.dart';
import 'package:car_provider_app/utils/navigator.dart';
import 'package:car_provider_app/widgets/app_bar.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:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:sizer/sizer.dart';

import '../../generated/locale_keys.g.dart';

class EditAccountPage extends StatefulWidget {
  @override
  State<EditAccountPage> createState() => _EditAccountPageState();
}

class _EditAccountPageState extends State<EditAccountPage> {
  String userID = "";
  String email = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context, title: LocaleKeys.editAccount.tr()),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        padding: EdgeInsets.all(20),
        child: Column(
          children: [
            20.height,
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                SvgPicture.asset(
                  "assets/images/ic_lock.svg",
                  color: MyColors.darkPrimaryColor,
                  width: 16,
                ),
                20.width,
                Expanded(child: LocaleKeys.changePassword.tr().toText12(isBold: true)),
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    textStyle: const TextStyle(color: Colors.white),
                    backgroundColor: MyColors.darkPrimaryColor,
                    padding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
                  ),
                  onPressed: () {
                    navigateWithName(context, AppRoutes.changePassword);
                  },
                  child: Text(
                    LocaleKeys.change.tr(),
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
              ],
            ),
            15.height,
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                SvgPicture.asset(
                  "assets/images/ic_mobile.svg",
                  color: MyColors.darkPrimaryColor,
                  width: 16,
                ),
                20.width,
                Expanded(child: LocaleKeys.changeMobile.tr().toText12(isBold: true)),
                LocaleKeys.verify.tr().toText12(color: Colors.green),
                20.width,
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    textStyle: const TextStyle(color: Colors.white),
                    backgroundColor: MyColors.darkPrimaryColor,
                    padding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
                  ),
                  onPressed: () {
                    navigateWithName(context, AppRoutes.changeMobilePage);
                  },
                  child: Text(
                    LocaleKeys.change.tr(),
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                )
              ],
            ),
            20.height,
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                SvgPicture.asset(
                  "assets/images/ic_email.svg",
                  color: MyColors.darkPrimaryColor,
                  width: 16,
                ),
                20.width,
                Expanded(child: LocaleKeys.changeEmail.tr().toText12(isBold: true)),
                InkWell(
                  child: ((AppState().getUser.data!.userInfo!.isEmailVerified ?? false) ? LocaleKeys.verified.tr() : LocaleKeys.verify.tr()).toText12(color: Colors.green),
                  onTap: (AppState().getUser.data!.userInfo!.isEmailVerified ?? false)
                      ? null
                      : () {
                          verifyEmail(context);
                        },
                ),
                20.width,
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    textStyle: const TextStyle(color: Colors.white),
                    backgroundColor: MyColors.darkPrimaryColor,
                    padding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
                  ),
                  onPressed: () {
                    navigateWithName(context, AppRoutes.changeEmailPage);
                  },
                  child: Text(
                    LocaleKeys.change.tr(),
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
              ],
            )

            // ListTile(
            //   leading: SvgPicture.asset("assets/images/ic_mobile.svg"),
            //   title: "Change Mobile".toText12(),
            //   onTap: () {
            //     navigateWithName(context, AppRoutes.changeMobilePage);
            //   },
            // ),
            // ListTile(
            //   leading: Icon(Icons.email_outlined, color: Colors.blue,),
            //   title: "Change Email".toText12(),
            //   onTap: () {
            //     navigateWithName(context, AppRoutes.changeEmailPage);
            //   },
            // ),
          ],
        ),
      ),
    );
  }

  Future<void> verifyEmail(BuildContext context) async {
    Utils.showLoading(context);
    VerifyEmail otpRequest = await UserApiClent().EmailVerify(email, userID);
    Utils.hideLoading(context);
    if (otpRequest.messageStatus == 1) {
      showMDialog(context, child: OtpDialog(
        onClick: (String code) async {
          pop(context);
          Utils.showLoading(context);
          MResponse otpCompare = await UserApiClent().EmailVerifyOTPVerify(otpRequest.data!.userToken ?? "", code);
          Utils.hideLoading(context);
          if (otpCompare.messageStatus == 1) {
            AppState().getUser.data!.userInfo!.isEmailVerified = true;
            setState(() {});
            Utils.showToast(LocaleKeys.emailVerified.tr());
            //  Navigator.of(context).pushNamedAndRemoveUntil(AppRoutes.dashboard, (Route<dynamic> route) => false);
            // showMDialog(
            //   context,
            //   child: MessageDialog(
            //     title: "Email is Verified",
            //     onClick: () {
            //       Navigator.of(context).pushNamedAndRemoveUntil(AppRoutes.dashboard, (Route<dynamic> route) => false);
            //     },
            //   ),
            // );
          } else {
            Utils.showToast(otpCompare.message ?? "");
          }
        },
      ));
    } else {
      Utils.showToast(otpRequest.message ?? "");
    }
  }
}
