import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import { Event } from '../../components/Logbook/List/EventList';
import {
  ANNOTATION,
  EVENT_CATEGORY_COMMANDLINE,
  NOTIFICATION,
} from '../../constants/eventTypes';

const resources = require('./resources/eventList.resource.js');

require('it-each')({ testPerIteration: true });

beforeEach(() => {
  Enzyme.configure({ adapter: new Adapter() });
});

describe('EventListUnitTests', () => {});

describe('EventUnitTests', () => {
  describe('Rendering', () => {
    it.each(
      resources.renderingEvent,
      '%s',
      ['description'],
      (element, next) => {
        const wrapper = getWrapper({ event: element.event });

        function lazyLoadTest(event, index) {
          // Test that the latest version is lazyloaded when necessary
          const htmlContent = event.content.find(
            (item) => item.format === 'html'
          );
          if (htmlContent) {
            if (htmlContent.text.indexOf('<img') !== -1) {
              expect(
                wrapper
                  .find('EventTextBox')
                  .dive()
                  .find('LazyLoadedText')
                  .at(index)
                  .dive()
                  .find('LazyLoad')
                  .exists()
              ).toBe(true);
            } else {
              expect(
                wrapper
                  .find('EventTextBox')
                  .dive()
                  .find('LazyLoadedText')
                  .at(index)
                  .dive()
                  .find('LazyLoad')
                  .exists()
              ).toBe(false);
            }
          }
        }

        if (element.event.type === NOTIFICATION) {
          if (element.event.previousVersionEvent) {
            // Tests notifications which have been commented

            // it renders 2 lazyLoadedText : the first for the original event and the second for the latest comment
            expect(
              wrapper.find('EventTextBox').dive().find('LazyLoadedText').length
            ).toEqual(2);

            // Check the content
            expect(
              wrapper
                .find('EventTextBox')
                .dive()
                .find('LazyLoadedText')
                .at(0)
                .prop('text')
            ).toEqual(element.expected.texts[0]);
            expect(
              wrapper
                .find('EventTextBox')
                .dive()
                .find('LazyLoadedText')
                .at(1)
                .prop('text')
            ).toEqual(element.expected.texts[1]);

            // Test the <pre> which wraps notification original version only.
            expect(
              wrapper
                .find('EventTextBox')
                .dive()
                .find('LazyLoadedText')
                .at(0)
                .parent()
                .is('pre')
            ).toBe(true);

            // The pre must have the proper className
            if (
              element.event.category.toLowerCase() ===
              EVENT_CATEGORY_COMMANDLINE
            ) {
              expect(
                wrapper
                  .find('EventTextBox')
                  .dive()
                  .find('pre')
                  .prop('className')
              ).toBeUndefined();
            } else {
              expect(
                wrapper
                  .find('EventTextBox')
                  .dive()
                  .find('pre')
                  .prop('className')
              ).toBe('whitePre');
            }

            expect(
              wrapper
                .find('EventTextBox')
                .dive()
                .find('LazyLoadedText')
                .at(1)
                .parent()
                .is('pre')
            ).toBe(false);

            // Test that the latest version is lazyloaded when necessary
            lazyLoadTest(element.event, 1);
          } else {
            // Test notifications which have not been commented yet
            expect(
              wrapper.find('EventTextBox').dive().find('LazyLoadedText').length
            ).toEqual(1);

            // Check the content
            expect(
              wrapper
                .find('EventTextBox')
                .dive()
                .find('LazyLoadedText')
                .prop('text')
            ).toEqual(element.expected.texts[0]);

            // Test the <pre> which must wrap the text.
            expect(
              wrapper.find('EventTextBox').dive().find('pre').exists()
            ).toBe(true);

            //The pre must have the proper className
            if (
              element.event.category.toLowerCase() ===
              EVENT_CATEGORY_COMMANDLINE
            ) {
              expect(
                wrapper
                  .find('EventTextBox')
                  .dive()
                  .find('pre')
                  .prop('className')
              ).toBeUndefined();
            } else {
              expect(
                wrapper
                  .find('EventTextBox')
                  .dive()
                  .find('pre')
                  .prop('className')
              ).toBe('whitePre');
            }

            // Test that the latest version is lazyloaded when necessary
            lazyLoadTest(element.event, 0);
          }
        }

        if (element.event.type === ANNOTATION) {
          // Test events which have not been commented yet
          expect(
            wrapper.find('EventTextBox').dive().find('LazyLoadedText').length
          ).toEqual(1);

          // Test the <pre>
          expect(wrapper.find('EventTextBox').dive().find('pre').exists()).toBe(
            false
          );

          // Check the content
          expect(
            wrapper
              .find('EventTextBox')
              .dive()
              .find('LazyLoadedText')
              .prop('text')
          ).toEqual(element.expected.texts[0]);

          // Test that the latest version is lazyloaded when necessary
          lazyLoadTest(element.event, 0);
        }

        next();
      }
    );

    it.each(
      resources.renderinglinkToEditEvent,
      '[renderinglinkToEditEvent: %s ]',
      ['description'],
      (element, next) => {
        expect(
          getWrapper({
            event: element.event,
            logbookContext: element.logbookContext,
          })
            .find('Glyphicon')
            .prop('glyph')
        ).toEqual(element.expected);
        next();
      }
    );
  });
});

function getWrapper(params) {
  if (params) {
    params.logbookContext = params.logbookContext
      ? params.logbookContext
      : { isReleased: false };
    params.onEventClicked = params.onEventClicked
      ? params.onEventClicked
      : () => null;

    return Enzyme.shallow(
      <Event
        event={params.event}
        logbookContext={params.logbookContext}
        onEventClicked={params.onEventClicked}
      />
    );
  }
}
