import { useContext } from "react";
import { Initiator } from "qualiteer/web-clients";
import JobContext from "../ctx/JobContext.jsx";

const cmd = `node other.js`;
export default function Test() {
  const {
    state: jobState,
    dispatch: jobDispatch,
    jobUpdate,
    jobCreate,
  } = useContext(JobContext);

  function onLog(d) {
    const job = jobState.jobs[0];
    job.log.push(d);
    jobUpdate(job.id, job);
    console.log(d);
    console.log(jobState);
  }

  async function startJob() {
    console.log("Wanting to start");
    const url = "https://Qualiteer.elijahparker3.repl.co";
    // Create an initiator and make a job request
    const primary = new Initiator(url);
    const jobRequest = { command: cmd };

    const job = await primary.newJob(jobRequest, onLog, () =>
      console.log("Primary Job Concluded")
    );
    jobCreate(job);

    console.log("Started");
  }

  return (
    <div className="test">
      <h1>vv Info vv </h1>
      <button onClick={startJob}>Start</button>
      {jobState.jobs.map((j) =>
        j.log.map((l, i) => (
          <div className="line" key={i}>
            {l}
            <br />
          </div>
        ))
      )}
    </div>
  );

  /*
      }*/
}
