<div class="status-toolbar {{isStatusInOwnThread ? 'status-in-own-thread' : ''}}" ref:node>
  <IconButton
    className="status-toolbar-reply-button"
    label="{{replyLabel}}"
    pressable="true"
    pressed="{{replyShown}}"
    href="#fa-reply"
    delegateKey="{{replyKey}}"
    focusKey="{{replyKey}}"
    />
  <IconButton
    label="{{reblogLabel}}"
    pressable="{{!reblogDisabled}}"
    pressed="{{reblogged}}"
    disabled="{{reblogDisabled}}"
    href="{{reblogIcon}}"
    delegateKey="{{reblogKey}}"
    animation="{{animateReblog && reblogAnimation}}"
  />
  <IconButton
    label="Favorite"
    pressable="true"
    pressed="{{favorited}}"
    href="#fa-star"
    delegateKey="{{favoriteKey}}"
    animation="{{animateFavorite && favoriteAnimation}}"
  />
  <IconButton
    label="Show more options"
    href="#fa-ellipsis-h"
    delegateKey="{{optionsKey}}"
  />
</div>
<style>
  .status-toolbar {
    grid-area: toolbar;
    display: flex;
    justify-content: space-between;
  }
  .status-toolbar.status-in-own-thread {
    margin-left: 58px;
  }
</style>
<script>
  import IconButton from '../IconButton.html'
  import { store } from '../../_store/store'
  import { registerClickDelegates } from '../../_utils/delegate'
  import { setFavorited } from '../../_actions/favorite'
  import { setReblogged } from '../../_actions/reblog'
  import { importDialogs } from '../../_utils/asyncModules'
  import { updateProfileAndRelationship } from '../../_actions/accounts'
  import { FAVORITE_ANIMATION, REBLOG_ANIMATION } from '../../_static/animations'
  import { on } from '../../_utils/eventBus'

  export default {
    oncreate() {
      registerClickDelegates(this, {
        [this.get('favoriteKey')]: (e) => this.onFavoriteClick(e),
        [this.get('reblogKey')]: (e) => this.onReblogClick(e),
        [this.get('replyKey')]: (e) => this.onReplyClick(e),
        [this.get('optionsKey')]: (e) => this.onOptionsClick(e)
      })
      on('postedStatus', this, this.onPostedStatus)
    },
    components: {
      IconButton
    },
    store: () => store,
    methods: {
      onFavoriteClick(e) {
        e.preventDefault()
        e.stopPropagation()
        let originalStatusId = this.get('originalStatusId')
        let favorited = this.get('favorited')
        /* no await */ setFavorited(originalStatusId, !favorited)
        this.set({animateFavorite: !favorited})
      },
      onReblogClick(e) {
        e.preventDefault()
        e.stopPropagation()
        let originalStatusId = this.get('originalStatusId')
        let reblogged = this.get('reblogged')
        /* no await */ setReblogged(originalStatusId, !reblogged)
        this.set({animateReblog: !reblogged})
      },
      onReplyClick(e) {
        e.preventDefault()
        e.stopPropagation()
        requestAnimationFrame(() => {
          let uuid = this.get('uuid')
          let $repliesShown = this.store.get('repliesShown')
          $repliesShown[uuid] = !$repliesShown[uuid]
          this.store.set({'repliesShown': $repliesShown})
          this.fire('recalculateHeight')
        })
      },
      async onOptionsClick(e) {
        e.preventDefault()
        e.stopPropagation()
        let originalStatusId = this.get('originalStatusId')
        let originalAccountId = this.get('originalAccountId')
        let updateRelationshipPromise = updateProfileAndRelationship(originalAccountId)
        let dialogs = await importDialogs()
        await updateRelationshipPromise
        dialogs.showStatusOptionsDialog(originalStatusId)
      },
      onPostedStatus(realm, inReplyToUuid) {
        if (realm !== this.get('originalStatusId') ||
            inReplyToUuid !== this.get('uuid')) {
          return
        }
        try {
          // return status to the reply button after posting a reply
          this.refs.node.querySelector('.status-toolbar-reply-button').focus()
        } catch (e) { /* ignore */ }
      }
    },
    data: () => ({
      favoriteAnimation: FAVORITE_ANIMATION,
      reblogAnimation: REBLOG_ANIMATION
    }),
    computed: {
      replyLabel: (replyShown) => replyShown ? 'Close reply' : 'Reply',
      reblogLabel: (visibility) => {
        switch (visibility) {
          case 'private':
            return 'Cannot be boosted because this is followers-only'
          case 'direct':
            return 'Cannot be boosted because this is a direct message'
          default:
            return 'Boost'
        }
      },
      reblogIcon: (visibility) => {
        switch (visibility) {
          case 'private':
            return '#fa-lock'
          case 'direct':
            return '#fa-envelope'
          default:
            return '#fa-retweet'
        }
      },
      reblogDisabled: (visibility) => {
        return visibility === 'private' || visibility === 'direct'
      },
      reblogged: (originalStatusId, $currentStatusModifications, originalStatus) => {
        if ($currentStatusModifications && originalStatusId in $currentStatusModifications.reblogs) {
          return $currentStatusModifications.reblogs[originalStatusId]
        }
        return originalStatus.reblogged
      },
      favorited: (originalStatusId, $currentStatusModifications, originalStatus) => {
        if ($currentStatusModifications && originalStatusId in $currentStatusModifications.favorites) {
          return $currentStatusModifications.favorites[originalStatusId]
        }
        return originalStatus.favourited
      },
      favoriteKey: (uuid) => `fav-${uuid}`,
      reblogKey: (uuid) => `reblog-${uuid}`,
      replyKey: (uuid) => `reply-${uuid}`,
      optionsKey: (uuid) => `options-${uuid}`,
    }
  }
</script>