<template>
  <div class="video root-result">

    <div class="thumbnail">
      <div class="img">
        <img v-bind:src="getVideoThumbnailUrl()" alt="">

        <span class="duration">{{ formattedDuration }}</span>
      </div>
    </div>

    <div class="information">
      <h5 class="title">{{ video.name }}</h5>

      <div class="description">{{ video.description }}</div>

      <div class="metadatas">
        <div class="by-account">
          <label>Created by</label>
          <actor-miniature type="account" v-bind:actor="video.account"></actor-miniature>
        </div>

        <div class="by-channel">
          <label>In</label>

          <actor-miniature type="channel" v-bind:actor="video.channel"></actor-miniature>
        </div>

        <div class="language">
          <label>Language</label>

          <div class="value">{{ video.language.label }}</div>
        </div>

        <div class="tags" v-if="video.tags && video.tags.length !== 0">
          <label>Tags</label>

          <div class="value">{{ video.tags.join(', ') }}</div>
        </div>
      </div>

      <div class="button">
        <a class="button-link" target="_blank" v-bind:href="video.url">Watch the video on {{host}}</a>
      </div>
    </div>

  </div>
</template>

<style lang="scss">
  @import '../scss/_variables';

  .video {
    .thumbnail {
      margin-right: 20px;

      --thumbnail-width: #{$thumbnail-width};
      --thumbnail-height: #{$thumbnail-height};

      // For the duration overlay
      .img {
        position: relative;
      }

      img {
        width: var(--thumbnail-width);
        height: var(--thumbnail-height);
        border-radius: 3px;
      }

      .duration {
        position: absolute;
        padding: 2px 5px;
        right: 5px;
        bottom: 5px;
        display: inline-block;
        background-color: rgba(0, 0, 0, 0.7);
        color: #fff;
        font-size: 11px;
        border-radius: 3px;
      }

      @media screen and (max-width: $small-view) {
        --thumbnail-width: calc(100% + 10px);
        --thumbnail-height: auto;

        img {
          border-radius: 0;
        }
      }
    }
  }
</style>

<script lang="ts">
  import Vue, { PropType } from 'vue'
  import ActorMiniature from './ActorMiniature.vue'
  import { Video } from '../../../PeerTube/shared/models'
  import { durationToString } from '../shared/utils'

  export default Vue.extend({
    components: {
      'actor-miniature': ActorMiniature
    },

    props: {
      video: Object as PropType<Video>
    },

    computed: {
      host () {
        const url = this.video.url

        return new URL(url as string).host
      },

      formattedDuration () {
        return durationToString(this.video.duration)
      },

      windowWidth () {
        return window.innerWidth
      }
    },

    methods: {
      getVideoThumbnailUrl () {
        if (this.windowWidth >= 900) {
          return this.video.thumbnailUrl
        }

        return this.video.previewUrl
      }
    }
  })
</script>
