import 'dart:convert';

import 'package:hmg_flutter_template/classes/utils.dart';
import 'package:hmg_flutter_template/exceptions/api_exception.dart';
import 'package:hmg_flutter_template/main.dart';
import 'package:hmg_flutter_template/model/base/generic_response_model2.dart';
import 'package:hmg_flutter_template/model/login/member_login_model.dart';
import 'package:hmg_flutter_template/model/login/project_info_model.dart';
import 'package:hmg_flutter_template/services/api_client.dart';
import 'package:hmg_flutter_template/classes/consts.dart';
import 'package:hmg_flutter_template/model/base/generic_response_model.dart';
import 'package:hmg_flutter_template/model/login/imei_details_model.dart';
import 'package:hmg_flutter_template/services/firebase_service.dart';
import 'package:injector/injector.dart';

abstract class ILoginApiRepo {
  Future<List<GetIMEIDetailsModel>> getDeviceInfoByIMEI();

  Future<List<ProjectInfoModel>> getProjectInfo(String userId);

  Future<MemberLoginModel> memberLogin(String userId, String password, int projectId);

  Future<GenericResponseModel> sendActivationCode(MemberLoginModel memberLoginModel, int facilityID, int sendOtpType);

  Future<GenericResponseModel> sendActivationCodeForSlientLogin(MemberLoginModel memberLoginModel, int facilityID, int sendOtpType);

  Future<GenericResponseModel> checkActivationCode(String activationCode, int sendOtpType, bool isFromSilentLogin);
}

class LoginApiRepo implements ILoginApiRepo {
  final IFirebaseService _firebaseService = Injector.appInstance.get<IFirebaseService>();

  @override
  Future<List<GetIMEIDetailsModel>> getDeviceInfoByIMEI() async {
    String url = "${ApiConsts.baseUrlServices}DoctorApplication.svc/REST/DoctorApp_GetDeviceDetailsByIMEI";
    String token = await _firebaseService.getFirebaseToke();
    logger.d("firebase_token: " + token);
    appState.imei = token;

    Map<String, dynamic> postParams = {};
    postParams.addAll(appState.postParamsJson);
    postParams["stamp"] = DateTime.now().toIso8601String();
    postParams["IMEI"] = appState.imei;
    GenericResponseModel response;
    try {
      response = await Injector.appInstance.get<IApiClient>().postJsonForObject((json) => GenericResponseModel.fromJson(json), url, postParams);
    } catch (e) {
      rethrow;
    }
    return response.listDoctorDeviceDetails ?? [];
  }

  @override
  Future<List<ProjectInfoModel>> getProjectInfo(String userId) async {
    String url = "${ApiConsts.baseUrlServices}DoctorApplication.svc/REST/GetProjectInfo";

    Map<String, dynamic> postParams = {};
    postParams.addAll(appState.postParamsJson);
    postParams["MemberID"] = "13777";
    GenericResponseModel response;
    try {
      response = await Injector.appInstance.get<IApiClient>().postJsonForObject((json) => GenericResponseModel.fromJson(json), url, postParams);
    } catch (e) {
      rethrow;
    }
    return response.projectInfo ?? [];
  }

  @override
  Future<MemberLoginModel> memberLogin(String userId, String password, int projectId) async {
    String url = "${ApiConsts.baseUrlServices}Sentry.svc/REST/MemberLogIN_New";

    Map<String, dynamic> postParams = {};
    postParams.addAll(appState.postParamsJson);
    postParams["UserID"] = userId;
    postParams["Password"] = password;
    postParams["ProjectID"] = projectId;
    MemberLoginModel response;
    try {
      response = await Injector.appInstance.get<IApiClient>().postJsonForObject((json) => MemberLoginModel.fromJson(json), url, postParams);
    } catch (e) {
      rethrow;
    }
    return response;
  }

  @override
  Future<GenericResponseModel> sendActivationCode(MemberLoginModel memberLoginModel, int facilityID, int sendOtpType) async {
    String url = "${ApiConsts.baseUrlServices}DoctorApplication.svc/REST/SendActivationCodeForDoctorApp";
    Map<String, dynamic> postParams = {};

    postParams.addAll(appState.postParamsJson);
    postParams["MemberID"] = memberLoginModel.listMemberInformation!.first.memberId;
    postParams["facilityId"] = facilityID;
    postParams["OTP_SendType"] = sendOtpType.toString();
    postParams["LoginDoctorID"] = memberLoginModel.doctorId;
    postParams["DoctorID"] = memberLoginModel.doctorId;
    postParams["TokenID"] = appState.logInTokenID;
    GenericResponseModel response;
    print(jsonEncode(postParams));
    // return GenericResponseModel();
    try {
      response = await Injector.appInstance.get<IApiClient>().postJsonForObject((json) => GenericResponseModel.fromJson(json), url, postParams);

    } catch (e) {
      rethrow;
    }
    return response;
  }

  @override
  Future<GenericResponseModel> sendActivationCodeForSlientLogin(MemberLoginModel memberLoginModel, int facilityID, int sendOtpType) async {
    String url = "${ApiConsts.baseUrlServices}DoctorApplication.svc/REST/SendVerificationCode";
    Map<String, dynamic> postParams = {};

    postParams.addAll(appState.postParamsJson);
    postParams["MobileNumber"] = memberLoginModel.mobileNumber;
    postParams["ZipCode"] = memberLoginModel.zipCode;
    postParams["IsMobileFingerPrint"] = 1;
    postParams["IMEI"] = appState.imei;
    postParams["LoginDoctorID"] = memberLoginModel.doctorId;
    postParams["DoctorID"] = memberLoginModel.doctorId;
    postParams["MemberID"] = memberLoginModel.listMemberInformation!.first.memberId;
    postParams["facilityId"] = facilityID;
    postParams["OTP_SendType"] = sendOtpType;
    postParams["LoginDoctorID"] = memberLoginModel.doctorId;
    postParams["DoctorID"] = memberLoginModel.doctorId;
    postParams["TokenID"] = appState.logInTokenID;
    GenericResponseModel response;
    print(jsonEncode(postParams));
    // return GenericResponseModel();
    try {
      response = await Injector.appInstance.get<IApiClient>().postJsonForObject((json) => GenericResponseModel.fromJson(json), url, postParams);
    } catch (e) {
      rethrow;
    }
    return response;
  }

  @override
  Future<GenericResponseModel> checkActivationCode(String activationCode, int sendOtpType, bool isFromSilentLogin) async {
    String url = "${ApiConsts.baseUrlServices}DoctorApplication.svc/REST/CheckActivationCodeForDoctorApp";
    Map<String, dynamic> postParams = {};
    MemberLoginModel memberLoginModel = appState.memberBeforeLogin ?? MemberLoginModel();
    postParams.addAll(appState.postParamsJson);
    postParams["MobileNumber"] = memberLoginModel.mobileNumber;
    postParams["ProjectID"] = appState.projectID;
    postParams["LogInTokenID"] = appState.logInTokenID;
    postParams["activationCode"] = activationCode;
    postParams["OTP_SendType"] = sendOtpType;
    postParams["facilityId"] = appState.projectID;
    postParams["MemberID"] = appState.memberBeforeLogin!.listMemberInformation!.first.memberId;
    postParams["Password"] = appState.password;
    postParams["IsForSilentLogin"] = isFromSilentLogin;
    postParams["LoginDoctorID"] = appState.memberBeforeLogin?.doctorId;
    postParams["DoctorID"] = appState.memberBeforeLogin?.doctorId;
    postParams["IMEI"] = appState.imei;
    postParams["ZipCode"] = appState.memberBeforeLogin?.zipCode;
    postParams["TokenID"] = appState.logInTokenID;

    GenericResponseModel response;
    try {
      response = await Injector.appInstance.get<IApiClient>().postJsonForObject((json) => GenericResponseModel.fromJson(json), url, postParams);
    } catch (e) {
      rethrow;
    }
    return response;
  }
}
