import { assoc } from 'ramda';

import getJwplayerSetup from './getJwplayersetup';

describe('getJwplayerSetup', () => {
  const VIDEO_FIXTURE = {
    id: 23,
    endpoints: {
      hls: 'http://examplehls.com',
      preview_thumbnails: 'http://examplepreview.com',
      offline: 'file://offline.com',
    },
    playback: {},
  };

  const defaultExpectSetup = {
    'aspectratio': '16:9',
    'playlist': [{
      'mediaid': 23,
      'sources': [{ 'file': 'file://offline.com', 'type': 'mp4' }],
      'tracks': [{
        'file': 'http://examplepreview.com', 'kind': 'thumbnails',
      }],
    }],
  };

  it('returns setup needed for jwplayer', () => {
    expect(getJwplayerSetup({}, VIDEO_FIXTURE, '16:9'))
      .toEqual(defaultExpectSetup);
  });

  it('includes additional setup passed in by jwPlayerConfig', () => {
    const newExpectedSetup = assoc('controls', false, defaultExpectSetup);

    expect(getJwplayerSetup({ controls: false }, VIDEO_FIXTURE, '16:9'))
      .toEqual(newExpectedSetup);
  });

  it('uses video aspect ratio if provided', () => {
    const updateVideoFixture = assoc(
      'playback',
      { aspect_ratio: '4:3' },
      VIDEO_FIXTURE
    );
    const newExpectedSetup = assoc(
      'aspectratio',
      '4:3',
      defaultExpectSetup)
    ;

    expect(getJwplayerSetup({}, updateVideoFixture, '16:9'))
      .toEqual(newExpectedSetup);
  });

  it('uses offline video if provided', () => {
    expect(getJwplayerSetup({}, VIDEO_FIXTURE, '16:9').playlist[0].sources)
      .toEqual([{ 'file': 'file://offline.com', 'type': 'mp4' }]);
  });

  it('uses hls endpoint if offline does not start with file', () => {
    const newFixture = {
      id: 23,
      endpoints: {
        hls: 'http://examplehls.com',
        preview_thumbnails: 'http://examplepreview.com',
        offline: 'testNotStartWithFile',
      },
      playback: {},
    };

    expect(getJwplayerSetup({}, newFixture, '16:9').playlist[0].sources)
      .toEqual([{ 'file': 'http://examplehls.com', type: 'hls' }]);
  });

  it('adds file with captions kind if video has subtitle', () => {
    const updateVideoFixture = {
      id: 23,
      endpoints: {
        hls: 'http://examplehls.com',
        preview_thumbnails: 'http://examplepreview.com',
        offline: 'file://offline.com',
        subtitles: { en: 'caption-test' },
      },
      playback: {},
    };

    const newExpectedSetup = {
      'aspectratio': '16:9',
      'playlist': [{
        'mediaid': 23,
        'sources': [{ 'file': 'file://offline.com', 'type': 'mp4' }],
        'tracks': [
          { 'file': 'http://examplepreview.com', 'kind': 'thumbnails' },
          {
            'file': 'caption-test',
            'kind': 'captions',
            'label': 'Default subtitles',
          },
        ],
      }],
    };

    expect(getJwplayerSetup({}, updateVideoFixture, '16:9'))
      .toEqual(newExpectedSetup);
  });

  it('adds file with chappters kind if video has chapter_timestamps', () => {
    const updateVideoFixture = {
      id: 23,
      endpoints: {
        hls: 'http://examplehls.com',
        preview_thumbnails: 'http://examplepreview.com',
        offline: 'file://offline.com',
        chapter_timestamps: 'chapter-timestamp-test',
      },
      playback: {},
    };

    const newExpectedSetup = {
      'aspectratio': '16:9',
      'playlist': [{
        'mediaid': 23,
        'sources': [{ 'file': 'file://offline.com', 'type': 'mp4' }],
        'tracks': [
          { 'file': 'http://examplepreview.com', 'kind': 'thumbnails' },
          {
            'file': 'chapter-timestamp-test',
            'kind': 'chapters',
          },
        ],
      }],
    };

    expect(getJwplayerSetup({}, updateVideoFixture, '16:9'))
      .toEqual(newExpectedSetup);
  });
});
