<!--
/**
 * @fileoverview ProgressBar component
 * @license MIT
 * @author Rafal Pospiech <neuronet.io@gmail.com>
 * @package GanttElastic
 */
-->
<template>
  <g
    :style="{ ...root.style['chart-row-progress-bar-wrapper'], ...task.style['chart-row-progress-bar-wrapper'] }"
    class="gantt-elastic__chart-row-progress-bar-wrapper"
  >
    <defs>
      <pattern
        id="diagonalHatch"
        :width="root.state.options.chart.progress.width"
        :height="root.state.options.chart.progress.width"
        pattern-transform="rotate(45 0 0)"
        pattern-units="userSpaceOnUse"
      >
        <line
          :style="{ ...root.style['chart-row-progress-bar-line'], ...task.style['chart-row-progress-bar-line'] }"
          :y2="root.state.options.chart.progress.width"
          class="chart-row-progress-bar-line"
          x1="0"
          y1="0"
          x2="0"
        />
      </pattern>
    </defs>
    <rect
      v-if="root.state.options.chart.progress.bar"
      :style="{ ...root.style['chart-row-progress-bar-solid'], ...task.style['chart-row-progress-bar-solid'] }"
      :width="getProgressWidth"
      class="gantt-elastic__chart-row-progress-bar-solid"
      x="0"
      y="0"
    />
    <g v-if="root.state.options.chart.progress.pattern">
      <rect
        :style="{ ...root.style['chart-row-progress-bar-pattern'], ...task.style['chart-row-progress-bar-pattern'] }"
        :x="getProgressWidth"
        :width="100 - task.progress + '%'"
        class="gantt-elastic__chart-row-progress-bar-pattern"
        y="0"
        height="100%"
      />
      <path
        :style="{
          ...root.style['chart-row-progress-bar-outline'],
          ...task.style['base'],
          ...task.style['chart-row-progress-bar-outline']
        }"
        :d="getLinePoints"
        class="gantt-elastic__chart-row-progress-bar-outline"
      />
    </g>
  </g>
</template>

<script>
export default {
  name: 'ProgressBar',
  inject: ['root'],
  // eslint-disable-next-line vue/require-prop-types
  props: ['task'],
  data() {
    return {}
  },

  computed: {
    /**
     * Get progress width
     *
     * @returns {string}
     */
    getProgressWidth() {
      return this.task.progress + '%'
    },

    /**
     * Get line points
     *
     * @returns {string}
     */
    getLinePoints() {
      const start = (this.task.width / 100) * this.task.progress
      return `M ${start} 0 L ${start} ${this.task.height}`
    },

    /**
     * Get solid style
     *
     * @returns {object}
     */
    getSolidStyle() {
      return Object.assign({}, this.root.state.options.chart.progress.styles.bar.solid, this.task.progressBarStyle.bar)
    },

    /**
     * Get line style
     *
     * @returns {object}
     */
    getLineStyle() {
      return Object.assign(
        {},
        {
          stroke: this.root.state.options.row.styles.bar.stroke + 'a0',
          'stroke-width': this.root.state.options.row.styles.bar['stroke-width'] / 2
        },
        this.task.style
      )
    }
  }
}
</script>
