import 'dart:convert';

import 'package:car_customer_app/api/shared_prefrence.dart';
import 'package:car_customer_app/api/client/user_api_client.dart';
import 'package:car_customer_app/classes/app_state.dart';
import 'package:car_customer_app/classes/utils.dart';
import 'package:car_customer_app/config/constants.dart';
import 'package:car_customer_app/config/routes.dart';
import 'package:car_customer_app/generated/locale_keys.g.dart';
import 'package:car_customer_app/models/user/login_password.dart';
import 'package:car_customer_app/models/user/register_user.dart';
import 'package:car_customer_app/models/user/user.dart';
import 'package:car_customer_app/utils/navigator.dart';
import 'package:car_customer_app/utils/utils.dart';
import 'package:car_customer_app/widgets/app_bar.dart';
import 'package:car_customer_app/widgets/button/show_image_button.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/extensions/int_extensions.dart';
import 'package:car_customer_app/extensions/string_extensions.dart';
import 'package:car_customer_app/extensions/widget_extensions.dart';
import 'package:car_customer_app/widgets/dialog/otp_dialog.dart';
import 'package:car_customer_app/widgets/txt_field.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';

class LoginMethodSelectionPage extends StatelessWidget {
  String userToken;

  LoginMethodSelectionPage(this.userToken);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(context, title: LocaleKeys.log_in.tr()),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        padding: EdgeInsets.all(20),
        child: SingleChildScrollView(
          child: Column(
            children: [
              LocaleKeys.loginSelection.tr().toText20(),
              30.height,
              LocaleKeys.welcomeBack.tr().toText20(),
              40.height,
              Row(
                children: [
                  Expanded(
                    child: ShowImageButton(
                      onClick: () {
                        performBasicOtp(context);
                      },
                      title: LocaleKeys.fingerPrint.tr(),
                      icon: icons + "ic_fingerprint.svg",
                    ),
                  ),
                  20.width,
                  Expanded(
                    child: ShowImageButton(
                      onClick: () {
                        performBasicOtp(context);
                      },
                      title: LocaleKeys.faceRecognition.tr(),
                      icon: icons + "ic_face.svg",
                    ),
                  ),
                ],
              ),
              40.height,
              Row(
                children: [
                  Expanded(
                    child: ShowImageButton(
                      onClick: () {
                        performBasicOtp(context);
                      },
                      title: LocaleKeys.SMS.tr(),
                      icon: icons + "ic_sms.svg",
                    ),
                  ),
                  20.width,
                  Expanded(
                    child: ShowImageButton(
                      onClick: () {
                        // navigateWithName(context, AppRoutes.dashboard);
                        performBasicOtp(context);
                      },
                      title: LocaleKeys.whatsapp.tr(),
                      icon: icons + "ic_whatsapp.svg",
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> performBasicOtp(BuildContext context) async {
    Utils.showLoading(context);
    Response response = await UserApiClent().login_V2_OTP(userToken, "1");
    Utils.hideLoading(context);
    LoginPassword user = LoginPassword.fromJson(jsonDecode(response.body));
    if (user.messageStatus == 1) {
      showMDialog(context, child: OtpDialog(
        onClick: (String code) async {
          pop(context);
          Utils.showLoading(context);
          Response response2 = await UserApiClent().login_V2_OTPVerify(user.data!.userToken??"", code);
          Utils.hideLoading(context);
          RegisterUser verifiedUser = RegisterUser.fromJson(jsonDecode(response2.body));
          if (verifiedUser.messageStatus == 1) {
            User user = User.fromJson(jsonDecode(response2.body));
            if(user.data!.userInfo!.roleId==4){


              AppState().setUser = user;
              SharedPrefManager.setUserToken(user.data!.accessToken ?? "");
              SharedPrefManager.setUserId(user.data!.userInfo!.userId ?? "");
              SharedPrefManager.setRefreshToken(user.data!.refreshToken??"");
              SharedPrefManager.setData(jsonEncode(user.data!.userInfo!.toJson()));
              navigateReplaceWithName(context, AppRoutes.dashboard);
            }else{
              Utils.showToast(LocaleKeys.onlyCustomerApp.tr());
                //("Sorry, Only Customer's can log in this app");
            }

          } else {
            Utils.showToast(verifiedUser.message??"");
          }
        },
      ));
    } else {
      Utils.showToast(user.message ?? "");
    }
  }
}
