import { TEST_TRAINER1_USER_ID } from "../../consts";
import { login } from "../helpers";

describe("assignmentTrainer.cy.js", () => {
  beforeEach(() => {
    cy.manageCommand("cypress_reset --create-assignment-completion");
    login("test-trainer1@example.com", "test");
  });

  describe("Casework", () => {
    it("can open cockpit assignment page and open user assignment", () => {
      cy.visit("/course/test-lehrgang/cockpit");
      cy.get(
        '[data-cy="show-details-btn-test-lehrgang-lp-circle-fahrzeug-lc-überprüfen-einer-motorfahrzeug-versicherungspolice"]'
      ).click();

      cy.get('[data-cy="Student1"]').should("contain", "Ergebnisse abgegeben");
      cy.get('[data-cy="Student1"]').find('[data-cy="show-results"]').click();

      cy.get('[data-cy="student-submission"]').should("contain", "Ergebnisse");
      cy.get('[data-cy="student-submission"]').should("contain", "Student1");
    });

    it("can start evaluation and store evaluation results", () => {
      cy.visit("/course/test-lehrgang/cockpit");
      cy.get(
        '[data-cy="show-details-btn-test-lehrgang-lp-circle-fahrzeug-lc-überprüfen-einer-motorfahrzeug-versicherungspolice"]'
      ).click();

      cy.get('[data-cy="Student1"]').find('[data-cy="show-results"]').click();

      cy.get('[data-cy="title"]').should("contain", "Bewertung");
      cy.get('[data-cy="evaluation-duedate"]').should("exist");
      cy.get('[data-cy="instruction"]').should(
        "contain",
        "Die Gesamtpunktzahl und die daraus resultierende Note wird auf Grund des hinterlegeten Beurteilungsinstrument berechnet."
      );
      cy.get('[data-cy="start-evaluation"]').click();
      cy.get('[data-cy="evaluation-task"]').should(
        "contain",
        "Beurteilungskriterium 1 / 5"
      );

      // without text input the button should be disabled
      cy.get('[data-cy="next-step"]').should("be.disabled");

      // with text you can continue
      cy.get('[data-cy="subtask-4"]').click();
      cy.get('[data-cy="reason-text"]').type("Gut gemacht!");
      // wait for debounce
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      cy.get('[data-cy="evaluation-task"]').should(
        "contain",
        "Beurteilungskriterium 2 / 5"
      );
      cy.get('[data-cy="subtask-2"]').click();
      cy.get('[data-cy="reason-text"]').type("Nicht so gut");
      cy.wait(500);

      // revisit step 1 will show stored data
      cy.get('[data-cy="previous-step"]').click();
      cy.get('[data-cy="reason-text"]')
        .find("textarea")
        .should("have.value", "Gut gemacht!");

      // even after reload
      cy.reload();
      cy.get('[data-cy="reason-text"]')
        .find("textarea")
        .should("have.value", "Gut gemacht!");

      // it can access step directly via url
      cy.url().then((url) => {
        const step2Url = url.replace("step=1", "step=2");
        console.log(step2Url);
        cy.visit(step2Url);
      });

      cy.get('[data-cy="reason-text"]')
        .find("textarea")
        .should("have.value", "Nicht so gut");

      cy.wait(500);
      // load AssignmentCompletion from DB and check
      cy.loadAssignmentCompletion(
        "evaluation_user_id",
        TEST_TRAINER1_USER_ID
      ).then((ac) => {
        expect(ac.completion_status).to.equal("EVALUATION_IN_PROGRESS");
        expect(JSON.stringify(ac.completion_data)).to.include("Nicht so gut");
        expect(Cypress._.values(ac.completion_data)).to.deep.include({
          expert_data: { points: 2, text: "Nicht so gut" },
        });
        expect(Cypress._.values(ac.completion_data)).to.deep.include({
          expert_data: { points: 4, text: "Gut gemacht!" },
        });
      });
    });

    it("can make complete evaluation", () => {
      cy.visit("/course/test-lehrgang/cockpit");
      cy.get(
        '[data-cy="show-details-btn-test-lehrgang-lp-circle-fahrzeug-lc-überprüfen-einer-motorfahrzeug-versicherungspolice"]'
      ).click();

      cy.get('[data-cy="Student1"]').find('[data-cy="show-results"]').click();

      cy.get('[data-cy="start-evaluation"]').click();

      // step 1
      cy.get('[data-cy="evaluation-task"]').should(
        "contain",
        "Beurteilungskriterium 1 / 5"
      );
      cy.get('[data-cy="subtask-6"]').click();
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 1");
      // wait for debounce
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 2
      cy.get('[data-cy="evaluation-task"]').should(
        "contain",
        "Beurteilungskriterium 2 / 5"
      );
      cy.get('[data-cy="subtask-4"]').click();
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 2");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 3
      cy.get('[data-cy="evaluation-task"]').should(
        "contain",
        "Beurteilungskriterium 3 / 5"
      );
      cy.get('[data-cy="subtask-2"]').click();
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 3");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 4
      cy.get('[data-cy="evaluation-task"]').should(
        "contain",
        "Beurteilungskriterium 4 / 5"
      );
      cy.get('[data-cy="subtask-3"]').click();
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 4");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 5
      cy.get('[data-cy="evaluation-task"]').should(
        "contain",
        "Beurteilungskriterium 5 / 5"
      );
      cy.get('[data-cy="subtask-2"]').click();
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 5");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // freigabe
      cy.get('[data-cy="sub-title"]').should("contain", "Bewertung Freigabe");
      cy.get('[data-cy="user-points"]').should("contain", "17");
      cy.get('[data-cy="total-points"]').should("contain", "24");
      cy.get('[data-cy="submit-evaluation"]').click();

      cy.get('[data-cy="result-section"]').should(
        "contain",
        "Deine Bewertung für Test Student1 wurde freigegeben"
      );

      // going back to cockpit should show points for student
      cy.visit("/course/test-lehrgang/cockpit");
      cy.reload();
      cy.get(
        '[data-cy="show-details-btn-test-lehrgang-lp-circle-fahrzeug-lc-überprüfen-einer-motorfahrzeug-versicherungspolice"]'
      ).click();

      cy.get('[data-cy="Student1"]').should("contain", "Bewertung freigegeben");
      cy.get('[data-cy="Student1"]').should("contain", "17 von 24 Punkte");

      // clicking on results page will go to last step
      cy.get('[data-cy="Student1"]').find('[data-cy="show-results"]').click();
      cy.url().should("include", "step=6");

      // load AssignmentCompletion from DB and check
      cy.loadAssignmentCompletion(
        "evaluation_user_id",
        TEST_TRAINER1_USER_ID
      ).then((ac) => {
        expect(ac.completion_status).to.equal("EVALUATION_SUBMITTED");
        expect(ac.evaluation_points).to.equal(17);
        expect(ac.evaluation_max_points).to.equal(24);
        expect(ac.evaluation_passed).to.equal(true);
        const completionString = JSON.stringify(ac.completion_data);
        expect(completionString).to.include("Begründung Schritt 1");
        expect(completionString).to.include("Begründung Schritt 2");
        expect(completionString).to.include("Begründung Schritt 3");
        expect(completionString).to.include("Begründung Schritt 4");
        expect(completionString).to.include("Begründung Schritt 5");
      });
    });
  });

  //Todo: Move tests to Lernbegleitung once it is implemented
  describe("Praxis Assignment", () => {
    it("can start evaluation and store evaluation results", () => {
      cy.visit("/course/test-lehrgang/cockpit");
      cy.get('[data-cy="dropdown-select"]').click();
      cy.get('[data-cy="dropdown-select-option-Reisen"]').click();
      cy.get(
        '[data-cy="show-details-btn-test-lehrgang-lp-circle-reisen-lc-mein-kundenstamm"]'
      ).click();

      cy.get('[data-cy="Student1"]').find('[data-cy="show-results"]').click();

      cy.get('[data-cy="title"]').should("contain", "Feedback");
      cy.get('[data-cy="evaluation-duedate]"').should("not.exist");
      cy.get('[data-cy="instruction"]').should("contain", "Intro für Feedback");
      cy.get('[data-cy="start-evaluation"]').click();
      cy.get('[data-cy="evaluation-task"]').should("contain", "Feedback 1 / 5");

      // without text input the button should be disabled
      cy.get('[data-cy="next-step"]').should("be.disabled");

      // with text you can continue
      cy.get('[data-cy="reason-text"]').type("Gut gemacht!");
      // wait for debounce
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      cy.get('[data-cy="evaluation-task"]').should("contain", "Feedback 2 / 5");
      cy.get('[data-cy="reason-text"]').type("Nicht so gut");
      cy.wait(1000);

      // load AssignmentCompletion from DB and check
      cy.loadAssignmentCompletion(
        "evaluation_user_id",
        TEST_TRAINER1_USER_ID
      ).then((ac) => {
        console.log(ac.completion_status);
        expect(ac.completion_status).to.equal("EVALUATION_IN_PROGRESS");
        expect(JSON.stringify(ac.completion_data)).to.include("Nicht so gut");
        expect(Cypress._.values(ac.completion_data)).to.deep.include({
          expert_data: { points: 0, text: "Nicht so gut" },
        });
        expect(Cypress._.values(ac.completion_data)).to.deep.include({
          expert_data: { points: 0, text: "Gut gemacht!" },
        });
      });
    });

    it("can make complete evaluation", () => {
      cy.visit("/course/test-lehrgang/cockpit");
      cy.get('[data-cy="dropdown-select"]').click();
      cy.get('[data-cy="dropdown-select-option-Reisen"]').click();
      cy.get(
        '[data-cy="show-details-btn-test-lehrgang-lp-circle-reisen-lc-mein-kundenstamm"]'
      ).click();

      cy.get('[data-cy="Student1"]').find('[data-cy="show-results"]').click();

      cy.get('[data-cy="start-evaluation"]').click();

      // step 1
      cy.get('[data-cy="evaluation-task"]').should("contain", "Feedback 1 / 5");
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 1");
      // wait for debounce
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 2
      cy.get('[data-cy="evaluation-task"]').should("contain", "Feedback 2 / 5");
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 2");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 3
      cy.get('[data-cy="evaluation-task"]').should("contain", "Feedback 3 / 5");
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 3");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 4
      cy.get('[data-cy="evaluation-task"]').should("contain", "Feedback 4 / 5");
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 4");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // step 5
      cy.get('[data-cy="evaluation-task"]').should("contain", "Feedback 5 / 5");
      cy.get('[data-cy="reason-text"]').type("Begründung Schritt 5");
      cy.wait(500);
      cy.get('[data-cy="next-step"]').click();

      // freigabe
      cy.get('[data-cy="sub-title"]').should("contain", "Feedback Freigabe");
      cy.get('[data-cy="total-points"]').should("not.exist");
      cy.get('[data-cy="submit-evaluation"]').click();

      cy.get('[data-cy="result-section"]').should(
        "contain",
        "Dein Feedback für Test Student1 wurde freigegeben"
      );

      // going back to cockpit should show points for student
      cy.visit("/course/test-lehrgang/cockpit");
      cy.reload();
      cy.get('[data-cy="dropdown-select"]').click();
      cy.get('[data-cy="dropdown-select-option-Reisen"]').click();
      cy.get(
        '[data-cy="show-details-btn-test-lehrgang-lp-circle-reisen-lc-mein-kundenstamm"]'
      ).click();

      cy.get('[data-cy="Student1"]').should("contain", "Feedback freigegeben");
      cy.get('[data-cy="Student1"]').should("not.contain", "Punkte");

      // clicking on results page will go to last step
      cy.get('[data-cy="Student1"]').find('[data-cy="show-results"]').click();
      cy.url().should("include", "step=6");

      // load AssignmentCompletion from DB and check
      cy.loadAssignmentCompletion(
        "evaluation_user_id",
        TEST_TRAINER1_USER_ID
      ).then((ac) => {
        expect(ac.completion_status).to.equal("EVALUATION_SUBMITTED");
        expect(ac.evaluation_max_points).to.equal(0);
        const completionString = JSON.stringify(ac.completion_data);
        expect(completionString).to.include("Begründung Schritt 1");
        expect(completionString).to.include("Begründung Schritt 2");
        expect(completionString).to.include("Begründung Schritt 3");
        expect(completionString).to.include("Begründung Schritt 4");
        expect(completionString).to.include("Begründung Schritt 5");
      });
    });
  });
});
