import config from "./config/config";
import logging from "./config/logging";
import * as Xcron from "node-cron";
import { get_order } from "./lib_order";
import axios from "axios";

export const NAME_SPACE = "OneEtlTat Gateway";
const VERSION = "1.0";

const pre_url_lab = config.base_url + "/etl/mgmmcu/generate_kelainan_lab/";
const pre_url_nonlab =
  config.base_url + "/etl/mgmmcu/generate_kelainan_nonlab/";
const pre_url_fisik = config.base_url + "/etl/fisik/generate/";

let isRunning: boolean = false;
let isRefreshRunning: boolean = false;

logging.info(NAME_SPACE, "Starting " + VERSION);

const on_init = async () => {
  logging.info(NAME_SPACE, "On Init Start");
  await one_result_job();
  logging.info(NAME_SPACE, "On Init End");
};

on_init();

async function one_result_job() {
  if (isRunning) {
    logging.info(NAME_SPACE, "Still Running");
    return;
  }
  isRunning = true;
  try {
    logging.info(NAME_SPACE, "Starting Job " + VERSION);
    //Get Order
    logging.info(NAME_SPACE, "Get Etl Mcu");
    var req = await get_order(config.row_per_batch);
    if (req["status"] != "OK") {
      logging.error(NAME_SPACE, "\t Error", req);
    } else {
      logging.info(NAME_SPACE, "\t Process Etl start");
      for (var i = 0; i < req["data"].length; i++) {
        var nolab = req["data"][i]["no_reg"];
        logging.info(NAME_SPACE, "\t Process Etl : " + nolab);
        var headerID = req["data"][i]["T_OrderHeaderID"];
        var mgmMcuID = req["data"][i]["Mgm_McuID"];
        var etl_resp = "";
        //Etl Lab
        var url_lab = pre_url_lab + "/" + mgmMcuID + "/" + headerID;
        logging.info(NAME_SPACE, "\t\t Lab :" + url_lab);
        etl_resp = await axios.get(url_lab);
        logging.info(NAME_SPACE, "\t\t\t", etl_resp);
        //Etl NonLab
        var url_nonlab = pre_url_nonlab + "/" + mgmMcuID + "/" + headerID;
        logging.info(NAME_SPACE, "\t\t NonLab:" + url_nonlab);
        etl_resp = await axios.get(url_nonlab);
        var j_resp = JSON.parse(etl_resp);
        logging.info(NAME_SPACE, "\t\t\t", j_resp["status"]);
        //Etl Fisik
        var url_fisik = pre_url_fisik + "/" + mgmMcuID + "/" + headerID;
        logging.info(NAME_SPACE, "\t\t Fisik :" + url_fisik);
        etl_resp = await axios.get(url_fisik);
        j_resp = JSON.parse(etl_resp);
        logging.info(NAME_SPACE, "\t\t\t", j_resp["status"]);
        //Sleep
        logging.info(NAME_SPACE, "\t wait 3s");
        await delay(3000);
      }
    }
    logging.info(NAME_SPACE, "End Job");
    isRunning = false;
  } catch (e) {
    isRunning = false;
    if (e instanceof Error) {
      logging.error(NAME_SPACE, e.message);
    } else {
      logging.error(NAME_SPACE, "Unknown");
    }
  }
}
// console.log(config);
for (const sched of config.schedule) {
  Xcron.schedule(
    sched,
    async () => {
      await one_result_job();
    },
    {
      timezone: "Asia/Jakarta",
    }
  );
}

// ------------- function
async function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
