{% comment %}
  Renders a media

  Accepts:
  - media: {Object or String} Media object or video url
  - type: {String} Type of media. Example: 'youtube', 'model', 'external_video', 'image', 'video', 'vimeo' (optional)
  - class: {String} Adds a media class to the media element (optional)
  - loop: {Boolean} Set looping for video. Default: false (optional)
  - controls: {Boolean} Set controls for video. Default: false (optional)
  - muted: {Boolean} Set muted for video. Default: false (optional)
  - autoplay: {Boolean} Set autoplay for video. Default: false (optional)

  Usage:

  {% render 'media',
    media: media,
    type: 'video',
    class: 'media',
    loop: true,
    controls: true,
    muted: true,
    autoplay: false
  %}
{% endcomment %}

{% liquid
  assign loop = loop | default: false
  assign controls = controls | default: true
  assign muted = muted | default: true
  assign autoplay = autoplay | default: false
%}

<div class='xo-media {{ class }}'>
  {% case type %}
    {% when 'video' %}
      <div class='xo-embed-responsive {{ autoplay | json }}'>
        {{ media | video_tag: image_size: '1100x', loop: loop, controls: controls, muted: muted, autoplay: autoplay }}
      </div>
    {% when 'vimeo' %}
      <div class='xo-embed-responsive'>
        <iframe
          src='https://player.vimeo.com/video/{{ media.id }}?autoplay=1{{ loop }}'
          allow='autoplay; encrypted-media'
          allowfullscreen
          title='{{ media.alt | escape }}'
        ></iframe>
      </div>

    {% when 'youtube' %}
      <div class='xo-embed-responsive'>
        <iframe
          src='https://www.youtube.com/embed/{{ media.id }}?autoplay={{autoplay}}&loop={{loop}}&mute=1&controls=1&iv_load_policy=1&disablekb=1&playlist={{ media.id }}&modestbranding=1&playsinline=1'
          allow='autoplay; encrypted-media'
          allowfullscreen
          title='{{ media.alt | escape }}'
        ></iframe>
      </div>

    {% when 'image' %}
      {% render 'image', image: media %}

    {% when 'model' %}
      {{ media | media_tag: image_size: '2048x', toggleable: true }}

    {% when 'external_video' %}
      <div class='xo-embed-responsive'>
        {%- if media.host == 'youtube' -%}
          {{ media | external_video_url: autoplay: autoplay, loop: loop, playlist: media.external_id }}
        {%- else -%}
          {{ media | external_video_url: autoplay: autoplay, loop: loop }}
        {%- endif -%}
      </div>
  {% endcase %}
</div>
