import * as fixtures from "../../../fixtures/imports";
import State from "../../../utils/State";
import getConnectorDetails, * as utils from "../../configs/Payment/Utils";

let globalState;

describe("Real Time Payment", () => {
  before("seed global state", () => {
    cy.task("getGlobalState").then((state) => {
      globalState = new State(state);
    });
  });

  afterEach("flush global state", () => {
    cy.task("setGlobalState", globalState.data);
  });

  context("DuitNow automatic capture flow", () => {
    let shouldContinue = true;

    beforeEach(function () {
      if (!shouldContinue) {
        this.skip();
      }
    });

    it("Create Payment Intent", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["PaymentIntent"];

      cy.createPaymentIntentTest(
        fixtures.createPaymentBody,
        data,
        "no_three_ds",
        "automatic",
        globalState
      );

      if (shouldContinue) shouldContinue = utils.should_continue_further(data);
    });

    it("Payment Methods Call Test", () => {
      cy.paymentMethodsCallTest(globalState);
    });

    it("Confirm DuitNow", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["DuitNow"];

      cy.confirmRealTimePaymentCallTest(
        fixtures.confirmBody,
        data,
        true,
        globalState
      );

      if (shouldContinue) shouldContinue = utils.should_continue_further(data);
    });

    it("Handle Redirection", () => {
      const expected_redirection = fixtures.confirmBody["return_url"];
      cy.handleRedirection(globalState, expected_redirection);
    });

    it("Retrieve Payment", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["DuitNowRetrieve"];

      cy.retrievePaymentCallTest({
        globalState,
        data,
        expectedIntentStatus: "succeeded",
      });
    });

    it("Sync Payment", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["DuitNowRetrieve"];

      cy.retrievePaymentCallTest({
        globalState,
        data,
        expectedIntentStatus: "succeeded",
      });
    });
  });

  context("QRIS automatic capture flow", () => {
    let shouldContinue = true;

    beforeEach(function () {
      if (!shouldContinue) {
        this.skip();
      }
    });

    it("Create Payment Intent", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["PaymentIntent"];

      cy.createPaymentIntentTest(
        fixtures.createPaymentBody,
        data,
        "no_three_ds",
        "automatic",
        globalState
      );

      if (shouldContinue) shouldContinue = utils.should_continue_further(data);
    });

    it("Payment Methods Call Test", () => {
      cy.paymentMethodsCallTest(globalState);
    });

    it("Confirm QRIS", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["Qris"];

      cy.confirmRealTimePaymentCallTest(
        fixtures.confirmBody,
        data,
        true,
        globalState
      );

      if (shouldContinue) shouldContinue = utils.should_continue_further(data);
    });

    it("Retrieve Payment", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["Qris"];

      cy.retrievePaymentCallTest({
        globalState,
        data,
      });
    });

    it("Sync Payment", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["Qris"];

      cy.retrievePaymentCallTest({
        globalState,
        data,
      });
    });
  });

  context("QRIS mandate setup flow", () => {
    let shouldContinue = true;

    beforeEach(function () {
      if (!shouldContinue) {
        this.skip();
      }
    });

    it("Create Payment Intent with Mandate Setup", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["PaymentIntent"];

      cy.createPaymentIntentTest(
        fixtures.createPaymentBody,
        data,
        "no_three_ds",
        "automatic",
        globalState
      );

      if (shouldContinue) shouldContinue = utils.should_continue_further(data);
    });

    it("Payment Methods Call Test", () => {
      cy.paymentMethodsCallTest(globalState);
    });

    it("Confirm QRIS with Mandate", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["QrisMandate"];

      cy.confirmRealTimePaymentCallTest(
        fixtures.confirmBody,
        data,
        true,
        globalState
      );

      if (shouldContinue) shouldContinue = utils.should_continue_further(data);
    });

    it("Retrieve Payment", () => {
      const data = getConnectorDetails(globalState.get("connectorId"))[
        "real_time_payment_pm"
      ]["QrisMandate"];

      cy.retrievePaymentCallTest({
        globalState,
        data,
      });
    });
  });
});
