<template>

  <v-container pt-0 mb-2>
    <v-layout row justify-start align-center ml-1>
      <h2 class="primary--text pull-left mb-2">Jira</h2>
    </v-layout>
    <v-layout class="jira-issues" row justify-start align-center mx-1>

      <v-progress-circular
        v-if="loading"
        :width="3"
        :size="20"
        indeterminate
        color="primary"/>

      <!-- List of Files -->
      <ul v-else>
        <li
          v-for="(issue, index) in issues"
          :key="index">
          <div class="jira-issue">
            <img :src="issue.fields.status.iconUrl">
            <v-tooltip top>
              <img
                slot="activator"
                :src="issue.fields.issuetype.iconUrl"
                class="jira-type"
                @click="goToJiraIssue(issue)">
              <span>{{ issue.key }}</span>
            </v-tooltip>
            <span class="jira-issue-summary">{{issue.fields.summary}}</span>
          </div>
        </li>

        <li class="d-inline-flex pull-left">
          <v-flex class="jira-new">
            <v-tooltip top>
              <v-btn slot="activator" icon>
                <v-icon
                  class="primary--text"
                  size="18"
                  @click="toggleInput">
                  add
                </v-icon>
              </v-btn>
              <span>Add New Ticket</span>
            </v-tooltip>
          </v-flex>
          <transition name="slide-fade">
            <v-form v-show="isInputShown" @submit.prevent="addNewTicket">
              <v-text-field
                ref="jiraInput"
                v-model.lazy="inputText"
                label="Add summary of new ticket"
                class="jira-new-input mt-1"
                light
                hide-details
                single-line
                @keyup.esc="handleClear"
                @blur="hideInput"/>
            </v-form>
          </transition>
        </li>
      </ul>

    </v-layout>
  </v-container>
</template>

<script>
import Vue from 'vue'
import { createJiraTicket, getJiraEpic } from '@/utils/api'

export default {
  name: 'JiraIntegration',
  props: {
    project: String,
    required: true
  },
  data () {
    return {
      loading: true,
      epic: {},
      baseUrl: '',
      issues: [],
      inputText: null,
      isInputShown: false,
      error: false,
      errorMessage: 'Add summary to create ticket'
    }
  },
  watch: {
    project (newValue, oldValue) {
      if (newValue !== oldValue) {
        this.fetchJiraTicket()
      }
    }
  },
  created () {
    this.fetchJiraTicket()
  },
  methods: {
    goToJiraIssue (issue) {
      window.open(`${this.baseUrl}/browse/${issue.key}`)
    },
    fetchJiraTicket () {
      this.loading = true
      getJiraEpic(this.project).then(response => {
        this.epic = response.epic
        this.baseUrl = response.jira_base_url
        this.issues = response.linked_issues.sort((a, b) => {
          return Number(a.id) > Number(b.id)
        })
        this.loading = false
      })
    },
    addNewTicket () {
      if (!this.inputText) {
        this.error = true
      } else {
        createJiraTicket(this.project, {
          summary: this.inputText
        }).then(() => {
          this.inputText = ''
          this.isInputShown = false
          this.fetchJiraTicket()
        })
      }
    },
    toggleInput () {
      this.isInputShown = !this.isInputShown
      this.error = false
      if (this.isInputShown) {
        Vue.nextTick(() => Vue.nextTick(() => this.$refs.jiraInput.focus()))
      } else {
        Vue.nextTick(() => Vue.nextTick(() => this.$refs.jiraInput.blur()))
      }
    },
    hideInput () {
      if (!this.inputText) {
        this.isInputShown = false
      }
    },
    clearText () {
      this.inputText = undefined
    },
    handleClear () {
      if (this.inputText) {
        this.clearText()
      } else {
        this.hideInput()
      }
    }
  }
}
</script>

<style lang="scss">
.jira-issues {
  ul {
    padding: 0px;
  }
  li {
    list-style-type: none;
    text-align: start;
    margin: 2px;
    margin-left: 0px;
  }
  .jira-issue {
    cursor: default;
    img {
      vertical-align: middle;
      margin-right: 10px;
      margin-left: 6px;
    }
    .jira-type {
      cursor: pointer;
    }
  }
}

.jira-new.flex {
  padding-left: 0 !important;
  .v-btn {
    margin-left: 0;
    height: 30px;
    width: 30px;
  }
}

.jira-new-input {
  min-width: 220px;
  label {
    font-size: 14px;
  }
}

</style>
