<template>

  <v-container px-3 py-0 mb-0>
    <v-layout row justify-start align-start>
      <h2 class="primary--text jira-title">Jira</h2>

      <v-progress-circular
        v-if="loading"
        :width="3"
        :size="16"
        indeterminate
        class="jira-loader"
        color="primary"/>

      <!-- List of Files -->
      <ul v-else class="jira-issues">
        <li
          v-for="(issue, index) in issues"
          :key="index">
          <div class="jira-issue">
            <v-icon
              v-if="issue.fields.status.name === 'Done'"
              class="jira-icon"
              color="green">
              check
            </v-icon>
            <img
              v-else
              :src="issue.fields.priority.iconUrl"
              class="jira-icon">
            <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" class="ma-0" 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-0 ml-2"
                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-title {
  padding: 10px 5px;
}
.jira-loader {
  margin: 17px;
}
.jira-issues {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 10px;
  li {
    list-style-type: none;
    text-align: start;
    margin: 2px;
    margin-left: 0px;
  }
  .jira-issue {
    cursor: default;
    img {
      vertical-align: middle;
    }
    .jira-icon {
      height: 16px;
      width: 16px;
      margin-right: 8px;
      margin-left: 8px;
    }
    .jira-type {
      cursor: pointer;
    }
    .jira-issue-summary {
      padding-left: 10px;
      text-overflow: wrap;
    }
  }
}
.jira-new.flex {
  padding: 0 !important;
  .v-btn {
    margin-left: 0;
    height: 30px;
    width: 30px;
  }
}
.jira-new-input {
  min-width: 220px;
  &.v-input label {
    font-size: 14px;
    line-height: 16px;
  }
  &.v-input input {
    max-height: 24px;
    font-size: 12px;
  }
}
</style>
