import validateDraft from '../../../lib/validation/ValidateDraft';
import ValidationSuccess from '../../../lib/validation/ValidationSuccess';
import ValidationFail from '../../../lib/validation/ValidationFail';

import { Services, AttachmentTypes } from '../../../AppConstants';

function getInstagramService() {
  return Services.find((service) => service.name === 'instagram');
}

describe('When validateDraft is called with draft with video', () => {
  it('returns ValidationFail if video too big', () => {
    const instagramService = getInstagramService();

    const draft = {
      enabledAttachmentType: AttachmentTypes.MEDIA,
      video: {
        size: instagramService.videoMaxSize * 2, // Twice what's allowed
      },
      service: instagramService,
    };

    const maxFileSizeInMb = instagramService.videoMaxSize / 1024 / 1024;
    const expectedMessage = `Please provide a smaller video, maximum size is ${maxFileSizeInMb} MB`;
    expect(validateDraft(draft)).toEqual(new ValidationFail(expectedMessage));
  });

  it('returns ValidationFail if video is too short', () => {
    const instagramService = getInstagramService();

    const draft = {
      enabledAttachmentType: AttachmentTypes.MEDIA,
      video: {
        size: instagramService.videoMaxSize / 2,
        duration: instagramService.videoMinDuration - 1,
      },
      service: instagramService,
    };

    const expectedMessage = `Please provide a longer video, videos of less than ${instagramService.videoMinDuration} seconds in duration are not valid`;
    expect(validateDraft(draft)).toEqual(new ValidationFail(expectedMessage));
  });

  it('returns ValidationFail if video is too long', () => {
    const instagramService = getInstagramService();

    const draft = {
      enabledAttachmentType: AttachmentTypes.MEDIA,
      video: {
        size: instagramService.videoMaxSize / 2,
        duration: instagramService.videoMaxDuration * 2,
      },
      service: instagramService,
    };

    const expectedMessage = `Please provide a shorter video, videos longer than ${instagramService.videoMaxDuration} seconds in duration are not valid`;
    expect(validateDraft(draft)).toEqual(new ValidationFail(expectedMessage));
  });

  it('returns ValidationSuccess if video is within limits', () => {
    const instagramService = getInstagramService();

    const draft = {
      enabledAttachmentType: AttachmentTypes.MEDIA,
      video: {
        size: instagramService.videoMaxSize / 2,
        duration: instagramService.videoMaxDuration / 2,
      },
      service: instagramService,
    };

    expect(validateDraft(draft)).toEqual(new ValidationSuccess());
  });
});


describe('When validateDraft is called with draft without video', () => {
  it('returns ValidationSuccess', () => {
    const draft = {
      video: null,
    };

    expect(validateDraft(draft)).toEqual(new ValidationSuccess());
  });
});
