// import request from "requestretry";
// import env from "./env";
// import _logger from "./logger";
// const logger = _logger("api.js");
// @ts-nocheck
import { httpApp } from "@/plugins/axiosApp";
import { store } from "@/store";

// async function execute(uri, method, form) {
//   // await new Promise((res) => setTimeout(res, 3000));
//   console.log("sending...");
//   console.log(uri, method, globalThis, globalThis.env);
//   // console.log(globalThis.electron.env);
//   // Object.assign(env, globalThis.electron.env);
//   return new Promise((resolve, reject) => {
//     request(
//       {
//         url: env.server.host + uri,
//         method: method,
//         json: true,
//         timeout: 10000,
//         form: form || {},
//         headers: {
//           "auth-info":
//             "loginname=" + env.loginName + ";password=" + env.password,
//         },
//         maxAttempts: 50,
//         retryDelay: 500,
//         retryStrategy: request.RetryStrategies.HTTPOrNetworkError,
//       },
//       function (error, response, body) {
//         if (response.statusCode == 200) {
//           resolve(body);
//         } else {
//           const message =
//             response.statusCode +
//             " " +
//             (error || "") +
//             (response.headers["error-info"] || "");
//           logger.error(message);
//           reject(message);
//         }
//       }
//     );
//   });
// }

export function login() {
  // return execute("/api/user/login", "GET");
  return httpApp.get("/api/user/login");
}

export function getExams(pageNumber: number, pageSize: number) {
  // let uri = "/api/exams";
  // const param = [];
  // if (pageNumber != undefined) {
  //   param.push("pageNumber=" + pageNumber);
  // }
  // if (pageSize != undefined) {
  //   param.push("pageSize=" + pageSize);
  // }
  // if (param.length > 0) {
  //   uri = uri + "?" + param.join("&");
  // }
  // return execute(uri, "GET");

  return httpApp.get(
    "/api/exams?" +
      new URLSearchParams({
        pageNumber: "" + pageNumber,
        pageSize: "" + pageSize,
      })
  );
}

export function getStudents(
  examId: number,
  pageNumber: number,
  pageSize: number,
  params: any
) {
  const form = {
    examId: examId,
    pageNumber: pageNumber,
    pageSize: pageSize,
  };
  if (params != undefined) {
    for (const key in params) {
      if (params[key] && params[key] != "") {
        form[key] = params[key];
      }
    }
  }
  return httpApp.post("/api/exam/students", new URLSearchParams(form));
}

export function countStudents(examId: number, params) {
  params = params || {};
  params.examId = examId;
  return httpApp.post("/api/exam/students/count", new URLSearchParams(params));
}

export function getPackages(examId, upload, withUrl) {
  let uri = "/api/package/count/" + examId;
  const param = [];
  if (upload != undefined) {
    param.push("upload=" + (upload ? "true" : "false"));
  }
  if (withUrl != undefined) {
    param.push("withUrl=" + (withUrl ? "true" : "false"));
  }
  if (param.length > 0) {
    uri = uri + "?" + param.join("&");
  }
  return httpApp.get(uri);
}

export default {
  // execute,
  login,
  // getExams,
  // getStudents,
  // countStudents,
  // getPackages,
};
