import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/model/pharmacies/LacumAccountInformation.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';

class LacumService extends BaseService{
  bool isFinished = true;
  bool hasError = false;
  String errorMsg = '';
  String successMsg = '';

  LacumAccountInformation? lacumInformation;
  LacumAccountInformation? lacumGroupInformation;

  Future getLacumAccountInformation(String identificationNo) async {
    hasError = false;
    super.error = "";

    Map<String, dynamic> body = Map();
    body['IdentificationNo'] = identificationNo;

    try {
      await baseAppClient.post(GET_LACUM_ACCOUNT_INFORMATION,
          onSuccess: (response, statusCode) async {
            lacumInformation = LacumAccountInformation.fromJson(response);
          }, onFailure: (String error, int statusCode) {
            hasError = true;
            super.error = error;
          }, body: body);
    } catch (error) {
      throw error;
    }
  }

  Future getLacumGroupInformation(String identificationNo) async {
    hasError = false;
    super.error = "";

    Map<String, dynamic> body = Map();
    body['IdentificationNo'] = identificationNo;
    body['AccountNumber'] = "${lacumInformation!.yahalaAccountNo}";
    body['IsDetailsRequired'] = true;

    try {
      await baseAppClient.post(GET_LACUM_GROUP_INFORMATION,
          onSuccess: (response, statusCode) async {
            lacumGroupInformation = LacumAccountInformation.fromJson(response);
          }, onFailure: (String error, int statusCode) {
            hasError = true;
            super.error = error;
          }, body: body);
    } catch (error) {
      throw error;
    }
  }

  Future makeAccountActivate() async {
    hasError = false;
    super.error = "";
    int? yahalaAccountNo = lacumInformation?.yahalaAccountNo;

    Map<String, dynamic> body = Map();
    body['CreatedBy'] = 103;
    body['YahalaAccountNumber'] = yahalaAccountNo;

    try {
      await baseAppClient.post(LACUM_ACCOUNT_ACTIVATE,
          onSuccess: (response, statusCode) async {
            // lacumInformation = LacumAccountInformation.fromJson(response);
            // lacumInformation.yahalaAccountNo = yahalaAccountNo;
          }, onFailure: (String error, int statusCode) {
            hasError = true;
            super.error = error;
          }, body: body);
    } catch (error) {
      throw error;
    }
  }

  Future makeAccountDeactivate() async {
    hasError = false;
    super.error = "";
    int? yahalaAccountNo = lacumInformation?.yahalaAccountNo;

    Map<String, dynamic> body = Map();
    body['CreatedBy'] = 103;
    body['YahalaAccountNumber'] = yahalaAccountNo;

    try {
      await baseAppClient.post(LACUM_ACCOUNT_DEACTIVATE,
          onSuccess: (response, statusCode) async {
            // lacumInformation = LacumAccountInformation.fromJson(response);
            // lacumInformation.yahalaAccountNo = yahalaAccountNo;
          }, onFailure: (String error, int statusCode) {
            hasError = true;
            super.error = error;
          }, body: body);
    } catch (error) {
      throw error;
    }
  }

  Future createLakumAccount(String name, String phone, int accountID, String patientIdentificationID, var prefLang) async {
    hasError = false;
    super.error = "";
    if(prefLang == null){
      var languageID =
      await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'en');
      prefLang = languageID == 'ar' ? 1 : 2;
    }


    Map<String, dynamic> body = Map();
    body['PrefLang'] = prefLang;
    body['AccountID'] = accountID;
    body['FullName'] = name;
    body['MobileNo'] = phone;
    body['PatientIdentificationID'] = patientIdentificationID;
    body['PatientID'] = user.patientID;

    try {
      await baseAppClient.post(CREATE_LAKUM_ACCOUNT,
          onSuccess: (response, statusCode) async {
            successMsg = LacumAccountInformation.fromJson(response).message!;
          }, onFailure: (String error, int statusCode) {
            hasError = true;
            super.error = error;
          }, body: body);
    } catch (error) {
      throw error;
    }
  }
}