import history from "../history";
import { authenticationService } from "../_services";
import { TEACHER_API, INSTITUTE_API, LOAD_CLASSES, LOAD_LANGAUGES, LOAD_QUESTIONTYPES, LOAD_TOPICS_PREFIX } from "../_services/config";

// checking role from login -
function checkRole() {
  const role = authenticationService.currentUserValue?.role_id;
  const GATEWAY = role === 3 ? TEACHER_API : role === 2 && INSTITUTE_API;
  // console.log(role, GATEWAY);
  return GATEWAY;
}

export const selectorService = {
  loadCategory,
  loadSubjects,
  loadSubCategory,
  loadQuestionTypes,
  loadQuestions,
  loadComplexity,
  updateBookMark,
  loadLanguages,
  loadClasses,
  deleteQuestions,
  deletePractice,
  deleteExam,
  getQuestion,
  getQuestionTypes,
  createQuestion,
  createBulkQuestion,
  cloneQuestionToLanguage,
  editQuestion,
  myQuestion,
  createExam,
  createPractise,
  updateExam,
  attachSectionToExam,
  getExamDetails,
  getPracticeDetails,
  draftQuestion,
  savedDraftQuestion,
  submitcreateQuestion,
  attachQuestionsToExam,
  attachQuestionsToPractice,
  loadSections,
  loadSectionsForPractice,
  arrangeSectionsFromExam,
  loadReviewQuestions,
  loadReviewQuestionsPractice,
  detachQuestion,
  detachQuestionPractice,
  createBatch,
  addToBatch,
  deleteFromBatch,
  deleteBatch,
  deleteClass,
  deleteExamBatch,
  getAllBatch,
  getExamBatch,
  getPracticeBatch,
  getStudentsOfBatch,
  getClassDetails,
  markQuestions,
  markQuestionsPractice,
  publishExam,
  publishPractice,
  loadQuestionsBookmark,
  draftExam,
  draftPractice,
  livePractice,
  upcomingPractice,
  draftPracticeCategory,
  livePracticeCategory,
  upcomingPracticeCategory,
  liveExam,
  upcomingExam,
  expiredExam,
  loadUpcomingExam,
  getAllClass,
  loadLiveExams,
  createExamAttempt,
  createClass,
  addBatchToExam,
  addBatchToPractice,
  addSubjectToClass,
  removeSubjectFromClass,
  removeTopicFromClass,
  addTopicToClass,
  updateClass,
  getExamAttempt,
  updateExamStatus,
  updateBatchName,
  endExam,
  pauseExam,
  getAllUser,
  loadReport,
  getAllBatchForClass,
  addBatchesToExam,
  detachBatchesFromExam,
  detachBatchFromPractice,
  livePracticeExams,
  createPracticeAttempt,
  getPracticeAttempt,
  stopPublishedExam,
  heartbeat,
};

async function stopPublishedExam(examId) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  // /v{version}/Exams/{exam_id}/StopExam
  try {
    const res = await fetch(`${checkRole()}/Exams/${examId}/StopExam`, requestOptions);
    const data = await res.json();
    if (res.ok && +data.status.code > 0) {
      return 1;
    }
    throw "Something went wrong";
  } catch (error) {
    console.log(error);
    return 0;
  }
}

function loadReport(examId) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-student/v1/" +
    currentUser.language_code +
    "/ExamAttempts/" +
    examId +
    "/Report",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

async function pauseExam(attemptId) {
  const currentUser = authenticationService.currentUserValue;
  if (!currentUser?.jwtToken) {
    console.error("❌ No valid user token found");
    return null;
  }

  const languageCode = currentUser.language_code || "En";
  const url = `https://api.practicekea.com/api-student/v1/${languageCode}/ExamAttempts/${attemptId}/Pause`;

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${currentUser.jwtToken}`,
      },
    });

    if (!response.ok) {
      console.error(`❌ Failed to pause exam (HTTP ${response.status})`);
      // Optional: handle session expiration
      // if (response.status === 401) {
      //   window.localStorage.clear();
      //   history.push("/login");
      // }
      return null;
    }

    const data = await response.json();
    console.log("⏸️ Exam paused successfully:", data);
    return data;
  } catch (error) {
    console.error("🚨 Error pausing exam:", error);
    return null;
  }
}

async function endExam(attemptId, payload) {
  const currentUser = authenticationService.currentUserValue;
  if (!currentUser?.jwtToken) {
    console.error("❌ No valid user token found");
    return null;
  }

  const languageCode = currentUser.language_code || "En";
  const url = `https://api.practicekea.com/api-student/v1/${languageCode}/ExamAttempts/${attemptId}/End`;

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${currentUser.jwtToken}`,
      },
      body: JSON.stringify(payload),
    });

    if (!response.ok) {
      console.error(`❌ Failed to end exam (HTTP ${response.status})`);
      // Optional: handle expired sessions
      // if (response.status === 401) {
      //   window.localStorage.clear();
      //   history.push("/login");
      // }
      return null;
    }

    const data = await response.json();
    console.log("✅ Exam ended successfully:", data);
    return data;
  } catch (error) {
    console.error("🚨 Error ending exam:", error);
    return null;
  }
}

async function heartbeat(attemptId) {
  const currentUser = authenticationService.currentUserValue;
  if (!currentUser?.jwtToken) {
    console.error("❌ No valid user token found");
    return null;
  }

  const url = `https://api.practicekea.com/api-student/v1/ExamAttempts/${attemptId}/Heartbeat`;

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${currentUser.jwtToken}`,
      },
      body: JSON.stringify({
        attempt_id: attemptId,
        timestamp: new Date().toISOString(),
      }),
    });

    if (!response.ok) {
      console.error(`❌ Heartbeat failed (HTTP ${response.status})`);
      return null;
    }

    const data = await response.json();
    console.log("💓 Heartbeat sent successfully:", data);
    return data; // ✅ Return the parsed response data
  } catch (error) {
    console.error("🚨 Error sending heartbeat:", error);
    return null;
  }
}

async function updateExamStatus(attemptId, payload) {
  const currentUser = authenticationService.currentUserValue;
  if (!currentUser?.jwtToken) {
    console.error("❌ No valid user token found");
    return null;
  }

  const url = `https://api.practicekea.com/api-student/v1/ExamAttempts/${attemptId}/UpdateAnswer`;

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${currentUser.jwtToken}`,
      },
      body: payload,
    });

    if (!response.ok) {
      console.error(`❌ Failed to update exam status (HTTP ${response.status})`);
      // Optional: Handle unauthorized users
      // if (response.status === 401) {
      //   window.localStorage.clear();
      //   history.push("/login");
      // }
      return null;
    }

    const data = await response.json();
    console.log("✅ Exam status updated:", data);
    return data;
  } catch (error) {
    console.error("🚨 Error updating exam status:", error);
    return null;
  }
}

function updateBatchName(obj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  obj.classId = currentClass;
  obj.isActive = true;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/UserGroups/" + obj.id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function getPracticeAttempt(practiceId) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-institute/v1/" +
    currentUser.language_code +
    "/PracticeAttempts/" +
    practiceId +
    "/Revision",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}


async function getExamAttempt(attemptId) {
  const currentUser = authenticationService.currentUserValue;
  if (!currentUser?.jwtToken) {
    console.error("❌ No valid user token found");
    return null;
  }

  const languageCode = currentUser.language_code || "En";
  const languageName = currentUser.language_name || "English";

  const url = `https://api.practicekea.com/api-student/v1/${languageCode}/ExamAttempts/${attemptId}/Questions`;

  try {
    const response = await fetch(url, {
      method: "GET",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${currentUser.jwtToken}`,
      },
    });

    if (!response.ok) {
      console.error(`❌ Request failed with status: ${response.status}`);
      // Optional: handle unauthorized user
      // if (response.status === 401) {
      //   window.localStorage.clear();
      //   history.push("/login");
      // }
      return null;
    }

    const data = await response.json();
    console.log("✅ Exam attempt data:", data);
    return data;
  } catch (error) {
    console.error("🚨 Error fetching exam attempt:", error);
    return null;
  }
}

function getAllUser(page_size, page_number) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  return fetch(
    checkRole() + "/users?" + `pageNumber=${page_number}&pageSize=${page_size}`,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
      throw 'Something went wrong';
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
      throw "Something went wrong";
    });
}

function getAllBatch() {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/UserGroups/Classes/" +
    currentClass,
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      console.log(error);
      //  console.error('Error:', error);
    });
}

function getExamBatch(obj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" + obj.id + "/Batches",
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      console.log(error);
      //  console.error('Error:', error);
    });
}

function getPracticeBatch(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Practices/" + obj.id + "/Batches",
    requestOptions
  )
    .then((response) => {
      // console.log(response);
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      // console.log(data);
      return data;
    })
    .catch((error) => {
      // console.log(error);
      throw error;
    });
}

function getStudentsOfBatch(id) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/UserGroups/" + id + "/Users",
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      throw "Something went wrong.."
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      console.log(error);
      throw "Something went wrong, please try again"
      //  console.error('Error:', error);
    });
}

function getClassDetails(id) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/Classes/" + id + "/Structure",
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      throw "Something went wrong, please try again"
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      // console.log(data);
      return data;
    })
    .catch((error) => {
      console.log(error);
      throw error;
      //  console.error('Error:', error);
    });
}

function createPracticeAttempt(practiceId) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-institute/v1/" +
    currentUser.language_code +
    "/PracticeAttempts/" +
    practiceId + "/CreateAttempt",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

async function createExamAttempt(examId) {
  const currentUser = authenticationService.currentUserValue;
  if (!currentUser?.jwtToken) {
    console.error("❌ No valid user token found");
    return null;
  }

  const languageCode = currentUser.language_code || "En";
  const languageName = currentUser.language_name || "English";

  const url = `https://api.practicekea.com/api-student/v1/${languageCode}/ExamAttempts/${examId}`;

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: `Bearer ${currentUser.jwtToken}`,
      },
    });

    console.log(response);

    if (!response.ok) {
      console.error(`❌ Request failed with status: ${response.status}`);
      // Optionally handle unauthorized user
      // if (response.status === 401) {
      //   window.localStorage.clear();
      //   history.push("/login");
      // }
      return null;
    }

    const data = await response.json();
    console.log("✅ Exam attempt created:", data);
    return data;
  } catch (error) {
    console.error("🚨 Error creating exam attempt:", error);
    return null;
  }
}

function loadLiveExams(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-student/v1/" +
    currentUser.language_code +
    "/Batches/" +
    jsonObj.batch +
    "/LiveExams" +
    queryStr,
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      authenticationService.logout();
      window.location.reload(true);
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => { });
}

function loadUpcomingExam(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-institute/v1/" +
    currentUser.language_code +
    "/Batches/" +
    jsonObj.batch +
    "/UpcomingExams" +
    queryStr,
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      authenticationService.logout();
      window.location.reload(true);
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => { });
}

function getAllClass() {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };

  return fetch(
    "https://api.practicekea.com/api-institute/v1/Classes",
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      // authenticationService.logout();
      // window.location.reload(true);
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      console.log(error);
    });
}

//console.log(myQuestion())
function publishExam(examId, json) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(json),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" + examId + "/Publish",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function publishPractice(practiceId, json) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(json),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Practices/" +
    practiceId +
    "/Publish",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      throw error;
      //  console.error('Error:', error);
    });
}

function markQuestions(sectionId, questions) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(questions),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/ExamSections/" +
    sectionId +
    "/MarkQuestions",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      // console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

async function markQuestionsPractice(practiceId, questions) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(questions),
  };

  try {
    const res = await fetch(checkRole() + "/Practices/" + practiceId +
      "/ReviewQuestions", requestOptions);
    const data = await res.json();
    return data;
  } catch (error) {
    console.log(error);
    throw "Something went wrong, could not submit the questions."
  }
}

function detachQuestion(sectionId, question) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(question),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/ExamSections/" +
    sectionId +
    "/DetachQuestions",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        // console.log(response.json());
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function detachQuestionPractice(practiceId, question) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(question),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() + "/Practices/" + practiceId + "/DetachQuestions",
    requestOptions
  )
    .then((response) => {
      // console.log(response);
      if (response.ok) {
        // console.log(response.json());
        return response.json();
      }
      throw "Something went wrong";
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      throw error;
      //  console.error('Error:', error);
    });
}

function createClass(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/Classes",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function deleteExamBatch(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" +
    obj.id +
    "/DetachBatch",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function addBatchToExam(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" +
    obj.id +
    "/AttachBatch",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

async function addBatchToPractice(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
  };

  try {
    const res = await fetch(checkRole() + "/Practices/" + obj.id + "/AttachBatch", requestOptions);
    const data = res.json();
    return data;
  } catch (error) {
    throw error;
  }
}

function detachBatchesFromExam(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj.batches),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" +
    obj.id +
    "/DetachBatch",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

async function detachBatchFromPractice(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
  };

  try {
    const res = await fetch(checkRole() + "/Practices/" + obj.id + "/DetachBatch", requestOptions);
    const data = res.json();
    return data;
  } catch (error) {
    throw error;
  }
}

function addBatchesToExam(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj.batches),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" +
    obj.id +
    "/AttachBatch",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function addSubjectToClass(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/Classes/" +
    obj.id +
    "/Subjects",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        // console.log(response);
        return response.json();
      }
      throw "Something went wrong, please try again";
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      // console.log(data);
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
      throw error;
    });
}

async function removeSubjectFromClass(id) {
  // /v{version}/Subjects/{subject_id}
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "DELETE",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };

  try {
    const res = await fetch(checkRole() + "/Subjects/" + id, requestOptions);
    if (!res.ok) {
      throw "Something went wrong";
    }
    const data = await res.json();
    return data;
  } catch (error) {
    console.log(error);
    throw "Something went wrong, please try again later."
  }
}

async function removeTopicFromClass(id) {
  // /v{version}/Categories/{category_id}
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "DELETE",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };

  try {
    const res = await fetch(checkRole() + "/Categories/" + id, requestOptions);
    if (!res.ok) {
      throw "Something went wrong";
    }
    const data = await res.json();
    return data;
  } catch (error) {
    console.log(error);
    throw "Something went wrong, please try again later."
  }
}

function addTopicToClass(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/Subjects/" +
    obj.id +
    "/Categories",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function updateClass(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  console.log(requestOptions);
  return fetch(
    "https://api.practicekea.com/api-institute/v1/Classes/" + obj.id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function createBatch(obj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  obj.class_id = currentClass;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    checkRole() + "/UserGroups/Classes/" +
    currentClass,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push('/login');
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function getAllBatchForClass(class_id) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/UserGroups/Classes/" +
    class_id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push('/login');
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function addToBatch(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    checkRole() + "/UserGroups/" + obj.batch_id + "/AttachUsers",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        // console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
      throw "Something went wrong, please try again";
    });
}

function deleteFromBatch(obj) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/UserGroups/" +
    obj.batch_id +
    "/DetachUsers",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
      throw "Something went wrong, please try again";
    });
}

function deleteBatch(id) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "DELETE",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },

    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/UserGroups/" + id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function deleteClass(id) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "DELETE",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },

    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-institute/v1/Classes/" + id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response);
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function loadReviewQuestions(sectionId) {
  const currentUser = authenticationService.currentUserValue;

  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/ExamSections/" +
    sectionId +
    "/Questions",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      authenticationService.logout();
      window.location.reload(true);
    })
    .then((data) => {
      // console.log(data)
      return data;
    })
    .catch((error) => { });
}

function loadReviewQuestionsPractice(practiceId) {
  const currentUser = authenticationService.currentUserValue;

  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Practices/" +
    practiceId +
    "/Questions",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // authenticationService.logout();
      window.location.reload(true);
    })
    .then((data) => {
      // console.log(data)
      return data;
    })
    .catch((error) => {
      throw error;
    });
}

function loadSections(examId) {
  const currentUser = authenticationService.currentUserValue;

  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" + examId,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      authenticationService.logout();
      window.location.reload(true);
    })
    .then((data) => {
      // console.log(data)
      return data;
    })
    .catch((error) => { });
}

function loadSectionsForPractice(practiceId) {
  const currentUser = authenticationService.currentUserValue;

  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Practices/" + practiceId,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      authenticationService.logout();
      window.location.reload(true);
    })
    .then((data) => {
      // console.log(data)
      return data;
    })
    .catch((error) => {
      throw error;
    });
}

function getQuestion(question_id, questionLang = undefined) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  if (!questionLang) {
    return fetch(checkRole() + "/" + currentUser.language_code + "/Questions/" + question_id, requestOptions)
      .then((response) => {
        // console.log(response);
        if (response.ok) {
          return response.json();
        }
        // authenticationService.logout();
        // window.location.reload(true);
      })
      .then((data) => {
        // console.log(data);
        return data;
      })
      .catch((error) => { });
  } else {
    return fetch(checkRole() + "/" + questionLang + "/Questions/" + question_id, requestOptions)
      .then((response) => {
        // console.log(response);
        if (response.ok) {
          return response.json();
        }
        // authenticationService.logout();
        // window.location.reload(true);
      })
      .then((data) => {
        // console.log(data);
        return data;
      })
      .catch((error) => { });
  }
}

function loadComplexity() {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-admin/v1/QuestionTypes",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}
function deleteQuestions(questionArray) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "DELETE",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify({ idList: questionArray }),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(checkRole() + "/" + currentUser.language_code + "/Questions", requestOptions)
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function deletePractice(practiceId) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "DELETE",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Practices/" + practiceId,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      throw error;
      //  console.error('Error:', error);
    });
}

function deleteExam(examId) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "DELETE",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" + examId,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

async function createQuestion(question) {
  // console.log("here");
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(question),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  // return fetch(
  //   checkRole() +
  //     "/" + currentUser.language_code +
  //     "/Questions",
  //   requestOptions
  // )
  //   .then((response) => {
  //     // console.log(response);
  //     if (response.ok) {
  //       return response.json();
  //     }
  //     throw "Something went wrong, please try again."
  //     // window.localStorage.clear();
  //     // history.push("/login");
  //   })
  //   .then((data) => {
  //     // console.log(data);
  //     return data;
  //   })
  //   .catch((error) => {
  //      console.error('Error:', error);
  //      throw error;
  //   });
  try {
    const res = await fetch(checkRole() + "/" + currentUser.language_code + "/Questions", requestOptions);
    if (res.ok) {
      const data = await res.json();
      return data;
    }
    // error handling here
    throw "Something is wrong, please try again"
  } catch (err) {
    throw err;
  }
}

async function createBulkQuestion(questions) {
  // /v{version}/{language}/AddBulkQuestions
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(questions),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }

  try {
    const res = await fetch(checkRole() + "/" + currentUser.language_code + "/AddBulkQuestions", requestOptions);
    if (res.ok) {
      const data = await res.json();
      return data;
    }
    // error handling here
    throw "Uploading Failed! Questions might have been invalidated, please check the questions before submitting again."
  } catch (err) {
    throw err;
  }

}

async function editQuestion(questionID, questionModifications) {
  // console.log("here");
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(questionModifications),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "en";
    currentUser.language_name = "English";
  }
  try {
    const res = await fetch(checkRole() + "/" + currentUser.language_code + "/Questions/" + questionID, requestOptions);
    if (res.ok) {
      const data = await res.json();
      return data;
    }
    // error handling here
    throw "Something is wrong, please try again"
  } catch (err) {
    throw err;
  }
}

async function cloneQuestionToLanguage(questionID, questionObj) {
  // /v{version}/{language}/Questions/{question_id}/CloneQuestionLanguage
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(questionObj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "en";
    currentUser.language_name = "English";
  }
  try {
    const res = await fetch(checkRole() + "/" + currentUser.language_code + "/Questions/" + questionID + "/CloneQuestionLanguage", requestOptions);
    if (res.ok) {
      const data = await res.json();
      return data;
    }
    // error handling here
    throw "Something went wrong, please try again"
  } catch (err) {
    throw err;
  }
}

function attachSectionToExam(examId, sections) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(sections),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Exams/" + examId + "/Sections",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function getExamDetails(examId) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() +
    "/Exams/" + examId,
    requestOptions
  )
    .then((response) => {
      // console.log(response);
      if (response.ok) {
        return response.json();
      }
      window.location.reload(true);
    })
    .then((data) => {
      // console.log(data);
      return data;
    })
    .catch((error) => { });
}

function getPracticeDetails(practiceId) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Practices/" + practiceId,
    requestOptions
  )
    .then((response) => {
      console.log(response);
      if (response.ok) {
        return response.json();
      }
      // window.location.reload(true);
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      throw error;
    });
}

function arrangeSectionsFromExam(examId, sections) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(sections),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v"+
    checkRole() +
    "/Exams/" + examId + "/ArrangeSections",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      throw 'Something went wrong';
      //  console.error('Error:', error);
    });
}

function attachQuestionsToExam(examId, questions) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(questions),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v"+
    checkRole() +
    "/ExamSections/" +
    examId +
    "/AttachQuestions",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function attachQuestionsToPractice(practiceId, questions) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(questions),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/Practices/" + practiceId +
    "/AttachQuestions",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      throw error;
      //  console.error('Error:', error);
    });
}

function createExam(exam) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(exam),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1/" +
    checkRole() + "/" +
    currentUser.language_code +
    "/Classes/" +
    currentClass +
    "/Exams",
    requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw new Error("Class not set properly, please try setting your class");
      }
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function createPractise(obj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(obj),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() + "/" + currentUser.language_code +
    "/Classes/" +
    currentClass +
    "/Practices",
    requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw new Error("Class not set properly, please try setting your class");
      }
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
      throw error
    });
}

function updateExam(exam) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(exam),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() +
    // currentUser.language_code +
    "/Exams/" +
    exam.id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function updateBookMark(questionArray, isBookmarkValue) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "POST",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify({
      idList: questionArray,
      isBookmark: isBookmarkValue,
    }),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(checkRole() + "/" + currentUser.language_code + "/Questions/Bookmark", requestOptions).then((response) => {
    if (response.ok) {
      return response.json();
    }
    // window.localStorage.clear();
    // history.push("/login");
  })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

async function loadQuestionTypes() {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  const response = await fetch(LOAD_QUESTIONTYPES, requestOptions);
  const data = await response.json();
  return data;
}

function getQuestionTypes(questionType) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    "https://api.practicekea.com/api-admin/v1/QuestionTypes/" + questionType,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function loadLanguages() {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  return fetch(
    // "https://api.practicekea.com/api-admin/v1/Languages",
    LOAD_LANGAUGES,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

async function loadClasses() {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  // const response = await fetch(`${checkRole()}/Classes`, requestOptions);
  const response = await fetch("https://api.practicekea.com/api-student/v1/UserGroups/list", requestOptions)
  const data = await response.json();
  return data;
}

function loadSubCategory(category) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() +
    "/" + currentUser.language_code +
    "/Categories/" +
    category +
    "/SubCategories",
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      throw new Error("Network response was not ok.");
    })
    .then((data) => {
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function loadQuestions(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  // console.log(currentUser);
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  if (jsonObj.type !== undefined && jsonObj.type !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr = queryStr + "type=" + jsonObj.type;
  }
  if (jsonObj.module_id !== undefined && jsonObj.module_id !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr =
      queryStr + "module_id=" + jsonObj.module_id + "&module=" + jsonObj.module;
  }
  if (jsonObj.complexity !== undefined && jsonObj.complexity !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr = queryStr + "complexity=" + jsonObj.complexity;
  }
  if (jsonObj.authorId !== undefined && jsonObj.authorId !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr = queryStr + "author_id=" + jsonObj.authorId;
  }
  if (queryStr !== "") {
    queryStr = "?" + queryStr;
    if (jsonObj.pageSize !== undefined) {
      queryStr = queryStr + `&pageSize=${jsonObj.pageSize}`;
    }
    if (jsonObj.page !== undefined)
      queryStr = queryStr + `&pageNumber=${jsonObj.page}`;
  } else {
    queryStr = "?";
    if (jsonObj.pageSize !== undefined) {
      queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
    }
    if (jsonObj.page !== undefined) {
      queryStr = queryStr + `&pageNumber=${jsonObj.page}`;
    }
  }
  if (jsonObj.translationMissing) {
    queryStr === "" ? queryStr = "?" + "translation_missing=" + jsonObj.translationMissing
      : queryStr = queryStr + "&translation_missing=" + jsonObj.translationMissing

  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    checkRole() + "/" +
    currentUser.language_code +
    "/Classes/" +
    currentClass +
    "/Questions" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw new Error("Class not set properly, please try setting your class");
      }
      // console.log(response); // logs the response from api after fetching all the questions
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function draftExam(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() +
    "/Classes/" +
    currentClass +
    "/DraftExams" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw new Error("No class is set, please set a class and try again.");
      }
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function draftPractice(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() + "/Classes/" + currentClass +
    "/DraftSubjectPractices" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      throw "Something went wrong, please try again."
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
      throw error;
    });
}

function livePractice(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() + "/Classes/" + currentClass +
    "/LiveSubjectPractices" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function upcomingPractice(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() + "/Classes/" + currentClass +
    "/UpcomingSubjectPractices" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function draftPracticeCategory(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() + "/Classes/" + currentClass +
    "/DraftCategoryPractices" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function livePracticeCategory(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() + "/Classes/" + currentClass +
    "/LiveCategoryPractices" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function upcomingPracticeCategory(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1"+
    checkRole() + "/Classes/" + currentClass +
    "/UpcomingCategoryPractices" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function liveExam(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() +
    "/Classes/" +
    currentClass +
    "/LiveExams" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw ("No class has been set, please reload the page or set a class and try again.");
      }
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      if (!data) throw ("Something went wrong, please reload the page and try again.");
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
      throw error;
    });
}

function livePracticeExams(module) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  queryStr = queryStr + `module=${module}`;
  queryStr = queryStr + `&module_id=-1`;

  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-institute/v1/" +
    "Classes/" +
    currentClass +
    "/LivePractices" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}


function upcomingExam(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() +
    "/Classes/" +
    currentClass +
    "/UpcomingExams" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (!currentClass) throw ("No class has been set, please reload the page or try setting the class.");
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function expiredExam(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  queryStr = "?";
  if (jsonObj.pageSize !== undefined) {
    queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
  }
  if (jsonObj.pageNumber !== undefined) {
    queryStr = queryStr + `&pageNumber=${jsonObj.pageNumber}`;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }

  return fetch(
    // "https://api.practicekea.com/api-institute/v1" +
    checkRole() +
    "/Classes/" +
    currentClass +
    "/HistoryExams" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw "The class is not set. Please set the class or try reloading the page."
      }
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function loadQuestionsBookmark(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  let queryStr = "";
  if (jsonObj.type !== undefined && jsonObj.type !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr = queryStr + "type=" + jsonObj.type;
  }
  if (jsonObj.module_id !== undefined && jsonObj.module_id !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr =
      queryStr + "module_id=" + jsonObj.module_id + "&module=" + jsonObj.module;
  }
  if (jsonObj.complexity !== undefined && jsonObj.complexity !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr = queryStr + "complexity=" + jsonObj.complexity;
  }
  if (queryStr !== "") {
    queryStr = "?" + queryStr;
    if (jsonObj.pageSize !== undefined) {
      queryStr = queryStr + `&pageSize=${jsonObj.pageSize}`;
    }
    if (jsonObj.page !== undefined)
      queryStr = queryStr + `&pageNumber=${jsonObj.page}`;
  } else {
    queryStr = "?";
    if (jsonObj.pageSize !== undefined) {
      queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
    }
    if (jsonObj.page !== undefined) {
      queryStr = queryStr + `&pageNumber=${jsonObj.page}`;
    }
  }
  if (jsonObj.translationMissing) {
    queryStr === "" ? queryStr = "?" + "translation_missing=" + jsonObj.translationMissing
      : queryStr = queryStr + "&translation_missing=" + jsonObj.translationMissing
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }

  return fetch(
    checkRole() + "/" + currentUser.language_code + "/Classes/" + currentClass + "/BookmarkedQuestions" +
    queryStr,
    requestOptions
    //return fetch("https://api.practicekea.com/api-institute/v1/en/Classes/1/Questions?module=subject&module_id=1", requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw new Error("Class not set properly, please try setting your class");
      }
      // console.log(response);
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

async function loadCategory(subject) {
  if (subject != "") {
    const currentUser = authenticationService.currentUserValue;
    // console.log(subject);
    const requestOptions = {
      method: "GET",
      headers: {
        Accept: "application/json",
        Authorization: "Bearer ".concat(currentUser.jwtToken),
      },
    };
    if (currentUser.language_code === null) {
      currentUser.language_code = "En";
      currentUser.language_name = "English";
    }

    try {
      const response = await fetch(`${checkRole()}/Subjects/` + subject + "/Categories", requestOptions)
      const data = await response.json();
      return data;
    } catch (error) {
      console.error(error);
    }
  }
}

async function loadSubjects(classes) {
  const currentUser = await authenticationService.currentUserValue;
  const currentClass = await authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  //return was in front of fetch originally
  try {
    if (!currentClass) throw 'Class has not been set. Please set a class or try reloading the page again'; // error handling

    const response = await fetch(`${checkRole()}/Classes` + "/" + currentClass + "/Subjects", requestOptions)
    const data = await response.json();
    return data;

  } catch (error) {
    console.error(error);
  }
}

function myQuestion(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  let queryStr = "";
  if (jsonObj.type !== undefined && jsonObj.type !== "") {
    queryStr = "type=" + jsonObj.type;
  }
  if (jsonObj.module_id !== undefined && jsonObj.module_id !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr =
      queryStr + "module_id=" + jsonObj.module_id + "&module=" + jsonObj.module;
  }
  if (jsonObj.complexity !== undefined && jsonObj.complexity !== "") {
    if (queryStr !== "") queryStr = queryStr + "&";
    queryStr = queryStr + "complexity=" + jsonObj.complexity;
  }
  if (queryStr !== "") {
    queryStr = "author_id=" + currentUser.id + "&" + queryStr;
  }
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  if (queryStr !== "") {
    if (jsonObj.pageSize !== undefined) {
      queryStr = queryStr + `&pageSize=${jsonObj.pageSize}`;
    }
    if (jsonObj.page !== undefined)
      queryStr = queryStr + `&pageNumber=${jsonObj.page}`;
  } else {
    if (jsonObj.pageSize !== undefined) {
      queryStr = queryStr + `pageSize=${jsonObj.pageSize}`;
    }
    if (jsonObj.page !== undefined) {
      queryStr = queryStr + `&pageNumber=${jsonObj.page}`;
    }
  }
  return fetch(
    "https://api.practicekea.com/api-institute/v1/en/Classes/" +
    currentClass +
    "/Questions?" +
    queryStr,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      //  console.log('Success:', data);
      return data;

      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}

function draftQuestion(jsonObj) {
  const currentUser = authenticationService.currentUserValue;
  const currentClass = authenticationService.currentClassValue;
  const requestOptions = {
    method: "GET",
    headers: {
      Accept: "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
  };
  let queryStr = "";
  if (jsonObj.type !== undefined && jsonObj.type !== "") {
    queryStr = "?type=" + jsonObj.type;
  }
  if (jsonObj.module_id !== undefined && jsonObj.module_id !== "") {
    if (queryStr !== "")
      queryStr =
        queryStr +
        "&module_id=" +
        jsonObj.module_id +
        "&module=" +
        jsonObj.module;
    else
      queryStr =
        "?module_id=" + jsonObj.module_id + "&module=" + jsonObj.module;
  }
  if (jsonObj.complexity !== undefined && jsonObj.complexity !== "") {
    queryStr !== "" ? queryStr = queryStr + "&complexity=" + jsonObj.complexity
      : queryStr = "?complexity=" + jsonObj.complexity;
  }
  if (queryStr !== "") {
    queryStr =
      queryStr + "&pageSize=" + jsonObj.pageSize + "&page=" + jsonObj.page;
  } else {
    queryStr =
      queryStr +
      "?pageNumber=" +
      jsonObj.page +
      "&pageSize=" +
      jsonObj.pageSize;
  }
  if (jsonObj.translationMissing) {
    queryStr === "" ? queryStr = "?" + "translation_missing=" + jsonObj.translationMissing
      : queryStr = queryStr + "&translation_missing=" + jsonObj.translationMissing
  }

  // console.log(queryStr);
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }

  return fetch(
    checkRole() + "/" + currentUser.language_code + "/Classes/" +
    currentClass +
    "/DraftQuestions" +
    queryStr,
    requestOptions
  )
    .then((response) => {
      if (!currentClass) {
        throw new Error("Class not set properly, please try setting your class");
      }
      if (response.ok) {
        return response.json();
      }
      // window.localStorage.clear();
      // history.push("/login");
    })
    .then((data) => {
      return data;
      // console.log("result", data.result.jwtToken);
      // sessionStorage.setItem('login', true);
      // sessionStorage.setItem('token', data.result.jwtToken);
    })
    .catch((error) => {
      console.error('Error:', error);
    });
}

function savedDraftQuestion(id, content) {
  const currentUser = authenticationService.currentUserValue;

  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(content),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-institute/v1/" +
    currentUser.language_code +
    "/Questions/" +
    id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        console.log(response.json());
        return response.json();
      }
      // window.localStorage.clear();
      //  history.push('/draftQuestions');
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}
function submitcreateQuestion(id, content) {
  const currentUser = authenticationService.currentUserValue;
  const requestOptions = {
    method: "PUT",
    headers: {
      "Access-Control-Allow-Origin": "*",
      Accept: "application/json",
      "Content-Type": "application/json",
      Authorization: "Bearer ".concat(currentUser.jwtToken),
    },
    body: JSON.stringify(content),
    // body: JSON.stringify(this.state)
    //body: JSON.stringify({ emailId:"sa@odiware.com", userPassword:"aaaa" })
  };
  if (currentUser.language_code === null) {
    currentUser.language_code = "En";
    currentUser.language_name = "English";
  }
  return fetch(
    "https://api.practicekea.com/api-institute/v1/" +
    currentUser.language_code +
    "/Questions/" +
    id,
    requestOptions
  )
    .then((response) => {
      if (response.ok) {
        // history.push("/draftQuestions");
        return response.json();
        // return response.json();
      }
      // window.localStorage.clear();
    })
    .then((data) => {
      console.log(data);
      return data;
    })
    .catch((error) => {
      //  console.error('Error:', error);
    });
}
