import minimalModule from '../../../fixtures/module.minimal';

const module = {
  ...minimalModule,
  chapters: [
    {
      contentBlocks: [
        {
          type: 'task',
          contents: [
            {
              type: 'text_block',
              value: {
                text: 'hallo velo',
              },
              id: 'bla-123',
            },
            {
              type: 'assignment',
              value: {
                title: 'assignment-title',
                assignment: 'assignment-assignment',
                id: 'some-assignment',
              },
              id: '123-456',
            },
          ],
        },
      ],
    },
  ],
};

const myText = 'submission text';

const getOperations = ({final, readOnly, classReadOnly = false}) => ({
  MeQuery: {
    me: {
      readOnly,
      selectedClass: {
        id: 'selectedClassId',
        readOnly: classReadOnly
      }
    },
  },
  ModuleDetailsQuery: {
    module,
  },
  AssignmentQuery: {
    assignment: {
      submission: {
        text: myText,
        final,
        document: '',
        submissionFeedback: null
      },
    },
  },
});

describe('Assignments read-only - Student', () => {
  beforeEach(() => {
    cy.setup();
  });

  it('can edit and turn in', () => {
    cy.mockGraphqlOps({
      operations: getOperations({final: false, readOnly: false})
    });
    cy.visit('module/module-with-assignment');

    cy.get('.submission-form__textarea').as('textarea');

    cy.get('@textarea').invoke('val').should('contain', myText);
    cy.get('@textarea').clear().type('Hello');
    cy.get('@textarea').should('not.have.attr', 'readonly');

    cy.getByDataCy('submission-form-submit').should('exist');
  });

  it('can not edit or turn in', () => {
    cy.mockGraphqlOps({
      operations: getOperations({final: false, readOnly: true})
    });
    cy.visit('module/module-with-assignment');

    cy.isSubmissionReadOnly(myText);
    cy.getByDataCy('final-submission-reopen').should('not.exist');
  });

  it('can revoke turn in', () => {
    cy.mockGraphqlOps({
      operations: getOperations({final: true, readOnly: false})
    });

    cy.visit('module/module-with-assignment');
    cy.getByDataCy('final-submission').should('exist');
    cy.getByDataCy('final-submission-reopen').should('exist');
  });

  it('can not revoke turn in', () => {
    cy.mockGraphqlOps({
      operations: getOperations({final: true, readOnly: true})
    });

    cy.visit('module/module-with-assignment');
    cy.getByDataCy('final-submission').should('exist');
    cy.getByDataCy('final-submission-reopen').should('not.exist');
  });

  it('can not edit or turn in in inactive class', () => {
    cy.mockGraphqlOps({
      operations: getOperations({final: false, readOnly: false, classReadOnly: true})
    });
    cy.visit('module/module-with-assignment');

    cy.isSubmissionReadOnly(myText);
    cy.getByDataCy('final-submission-reopen').should('not.exist');
  });
});
