<!--
/**
 * @fileoverview Task component
 * @license MIT
 * @author Rafal Pospiech <neuronet.io@gmail.com>
 * @package GanttElastic
 */
-->
<template>
  <g
    :style="{
      ...root.style['chart-row-bar-wrapper'],
      ...root.style['chart-row-task-wrapper'],
      ...task.style['chart-row-bar-wrapper']
    }"
    class="gantt-elastic__chart-row-bar-wrapper gantt-elastic__chart-row-task-wrapper"
  >
    <foreignObject
      v-if="displayExpander"
      :style="{
        ...root.style['chart-expander'],
        ...root.style['chart-expander--task'],
        ...task.style['chart-expander']
      }"
      :x="task.x - root.state.options.chart.expander.offset - root.state.options.chart.expander.size"
      :y="task.y + (root.state.options.row.height - root.state.options.chart.expander.size) / 2"
      :width="root.state.options.chart.expander.size"
      :height="root.state.options.chart.expander.size"
      class="gantt-elastic__chart-expander gantt-elastic__chart-expander--task"
    >
      <expander :tasks="[task]" :options="root.state.options.chart.expander" type="chart"/>
    </foreignObject>
    <svg
      :style="{ ...root.style['chart-row-bar'], ...root.style['chart-row-task'], ...task.style['chart-row-bar'] }"
      :x="task.x"
      :y="task.y"
      :width="task.width"
      :height="task.height"
      :viewBox="`0 0 ${task.width} ${task.height}`"
      class="gantt-elastic__chart-row-bar gantt-elastic__chart-row-task"
      xmlns="http://www.w3.org/2000/svg"
      @click="emitEvent('click', $event)"
      @mouseenter="emitEvent('mouseenter', $event)"
      @mouseover="emitEvent('mouseover', $event)"
      @mouseout="emitEvent('mouseout', $event)"
      @mousemove="emitEvent('mousemove', $event)"
      @mousedown="emitEvent('mousedown', $event)"
      @mouseup="emitEvent('mouseup', $event)"
      @mousewheel="emitEvent('mousewheel', $event)"
      @touchstart="emitEvent('touchstart', $event)"
      @touchmove="emitEvent('touchmove', $event)"
      @touchend="emitEvent('touchend', $event)"
    >
      <defs>
        <clipPath :id="clipPathId">
          <polygon :points="getPoints"/>
        </clipPath>
      </defs>
      <polygon
        :style="{
          ...root.style['chart-row-bar-polygon'],
          ...root.style['chart-row-task-polygon'],
          ...task.style['base'],
          ...task.style['chart-row-bar-polygon']
        }"
        :points="getPoints"
        class="gantt-elastic__chart-row-bar-polygon gantt-elastic__chart-row-task-polygon"
      />
      <progress-bar :task="task" :clip-path="'url(#' + clipPathId + ')'"/>
    </svg>
    <chart-text v-if="root.state.options.chart.text.display" :task="task"/>
  </g>
</template>

<script>
import ChartText from '../Text.vue'
import ProgressBar from '../ProgressBar.vue'
import Expander from '../../Expander.vue'
import taskMixin from './Task.mixin.js'
export default {
  name: 'Task',
  components: {
    ChartText,
    ProgressBar,
    Expander
  },
  inject: ['root'],
  mixins: [taskMixin],
  // eslint-disable-next-line vue/require-prop-types
  props: ['task'],
  data() {
    return {}
  },
  computed: {
    /**
     * Get clip path id
     *
     * @returns {string}
     */
    clipPathId() {
      return 'gantt-elastic__task-clip-path-' + this.task.id
    },

    /**
     * Get points
     *
     * @returns {string}
     */
    getPoints() {
      const task = this.task
      return `0,0 ${task.width},0 ${task.width},${task.height} 0,${task.height}`
    }
  }
}
</script>
