import { AttachmentTypes } from '../../AppConstants';
import ValidationSuccess from './ValidationSuccess';
import ValidationFail from './ValidationFail';

function validateDraft(draft) {
  let validationResult = new ValidationSuccess();

  const hasVideoAttached =
        draft.enabledAttachmentType === AttachmentTypes.MEDIA && draft.video !== null;

  if (hasVideoAttached) {
    validationResult = validateVideoForService(draft.video, draft.service);
  }

  return validationResult;
}

function validateVideoForService(video, service) {
  if (service.videoMaxSize && video.size > service.videoMaxSize) {
    const maxFileSizeInMb = service.videoMaxSize / 1024 / 1024;
    return new ValidationFail(`Please provide a smaller video, maximum size is ${maxFileSizeInMb} MB`);
  }

  if (service.videoMinDuration && video.duration < service.videoMinDuration) {
    return new ValidationFail(`Please provide a longer video, videos of less than ${service.videoMinDuration} seconds in duration are not valid`);
  }

  if (service.videoMaxDuration
      && video.duration > service.videoMaxDuration) {
    return new ValidationFail(`Please provide a shorter video, videos longer than ${service.videoMaxDuration} seconds in duration are not valid`);
  }

  return new ValidationSuccess();
}

export default validateDraft;
