import { Selector } from "testcafe";
import {
  getSiteRoot, getPageUrl, deleteForm, doLogin, generateToken, getPostRequestLogger,
} from "../utils";

const LOGGER = getPostRequestLogger("signupForm/");

fixture`Admin signup form`.page(`${getSiteRoot()}/admin/signups`)
  .requestHooks(LOGGER)
  .after(async (ctx) => {
    const token = await generateToken();
    await deleteForm(ctx.formId, token);
  });

test("Logged in user can create signup", async (t) => {
  const loginForm = Selector("form.admin-login-form");
  await t.expect(loginForm.exists).ok();
  await doLogin(t);
  await t.wait(3000);
  await t.expect(await getPageUrl()).contains("/admin/signups");

  const newButton = Selector("[data-e2e=\"create-signup\"]");
  await t.click(newButton);
  await t.wait(3000);
  await t.expect(await getPageUrl()).contains("/admin/signups/create");

  const titleFi = Selector("#rjsf_title_fi");
  const titleEn = Selector("#rjsf_title_en");
  const visible = Selector("#rjsf_visible");

  await t
    .typeText(titleFi, "Testi Ilmo")
    .typeText(titleEn, "Test Signup")
    .click(visible);

  const newQuestionButton = Selector("[data-e2e=\"admin-signup-new-question\"]");
  const lastQuestion = () => Selector("[data-e2e=\"admin-signup-question\"]").child("div").child("div").nth(-1);

  await t.click(newQuestionButton);
  let question = lastQuestion();
  let questionName = question.child("input");
  let questionTypeSelect = question.child("select");
  let requiredBox = question.child("label");

  await t
    .selectText(questionName)
    .pressKey("delete")
    .typeText(questionName, "Nimi")
    .click(questionTypeSelect)
    .click(questionTypeSelect.find("option").withExactText("name"))
    .click(requiredBox);

  await t.click(newQuestionButton);
  question = lastQuestion();
  questionName = question.child("input");
  questionTypeSelect = question.child("select");
  requiredBox = question.child("label");

  await t
    .selectText(questionName)
    .pressKey("delete")
    .typeText(questionName, "S-Posti")
    .click(questionTypeSelect)
    .click(questionTypeSelect.find("option").withExactText("email"))
    .click(requiredBox);

  await t.click(newQuestionButton);
  question = lastQuestion();
  questionName = question.child("input");
  questionTypeSelect = question.child("select");
  const radioOptions = question.child("input").nth(-1);

  await t
    .selectText(questionName)
    .pressKey("delete")
    .typeText(questionName, "Olen")
    .click(questionTypeSelect)
    .click(questionTypeSelect.find("option").withExactText("radiobutton"))
    .typeText(radioOptions, "Nuori,Vanha,Testaaja");

  const submit = Selector("button[type=\"submit\"]");

  await t.click(submit);

  const parsed = JSON.parse(LOGGER.requests[0].response.body as string);
  // eslint-disable-next-line no-param-reassign
  t.fixtureCtx.formId = parsed.id;

  const statusMessage = Selector(".Toastify__toast-body");
  await t
    .hover(statusMessage)
    .expect(
      statusMessage.innerText,
    ).eql("Sign-up created successfully 😎");
});
