<!--
/**
 * @fileoverview Grid component
 * @license MIT
 * @author Rafal Pospiech <neuronet.io@gmail.com>
 * @package GanttElastic
 */
-->
<template>
  <svg
    ref="chart"
    :style="{ ...root.style['grid-lines-wrapper'] }"
    :width="root.state.options.width"
    :height="root.state.options.allVisibleTasksHeight"
    class="gantt-elastic__grid-lines-wrapper"
    x="0"
    y="0"
    xmlns="http://www.w3.org/2000/svg"
  >
    <g :style="{ ...root.style['grid-lines'] }" class="gantt-elastic__grid-lines">
      <line
        v-for="line in horizontalLines"
        :style="{ ...root.style['grid-line-horizontal'] }"
        :key="line.key"
        :x1="line.x1"
        :y1="line.y1"
        :x2="line.x2"
        :y2="line.y2"
        class="gantt-elastic__grid-line-horizontal"
      />
      <line
        v-for="line in verticalLines"
        :style="{ ...root.style['grid-line-vertical'] }"
        :key="line.key"
        :x1="line.x1"
        :y1="line.y1"
        :x2="line.x2"
        :y2="line.y2"
        class="gantt-elastic__grid-line-vertical"
      />
      <line
        :style="{ ...root.style['grid-line-time'] }"
        :x1="timeLinePosition.x"
        :y1="timeLinePosition.y1"
        :x2="timeLinePosition.x"
        :y2="timeLinePosition.y2"
        class="gantt-elastic__grid-line-time"
      />
    </g>
  </svg>
</template>

<script>
export default {
  name: 'Grid',
  inject: ['root'],
  data() {
    return {}
  },

  computed: {
    /**
     * Generate vertical lines of the grid
     *
     * @returns {array}
     */
    verticalLines() {
      const lines = []
      const state = this.root.state
      state.options.times.steps.forEach(step => {
        if (this.root.isInsideViewPort(step.offset.px, 1)) {
          lines.push({
            key: step.time,
            x1: step.offset.px,
            y1: 0,
            x2: step.offset.px,
            y2:
              state.tasks.length * (state.options.row.height + state.options.chart.grid.horizontal.gap * 2) +
              this.root.style['grid-line-vertical']['stroke-width']
          })
        }
      })
      return lines
    },

    /**
     * Generate horizontal lines of the grid
     *
     * @returns {array}
     */
    horizontalLines() {
      const lines = []
      const state = this.root.state.options
      const tasks = this.root.visibleTasks
      for (let index = 0, len = tasks.length; index <= len; index++) {
        const y =
          index * (state.row.height + state.chart.grid.horizontal.gap * 2) +
          this.root.style['grid-line-vertical']['stroke-width'] / 2
        lines.push({
          key: 'hl' + index,
          x1: 0,
          y1: y,
          x2: '100%',
          y2: y
        })
      }
      return lines
    },

    /**
     * Check if specified line is inside viewport (visible)
     *
     * @returns {function}
     */
    inViewPort() {
      return line => {
        const state = this.root.state.options
        return line.x1 >= state.scroll.chart.left && line.x1 <= state.scroll.chart.right
      }
    },

    /**
     * Get current time line position
     *
     * @returns {object}
     */
    timeLinePosition() {
      const d = new Date()
      const current = d.getTime()
      const currentOffset = this.root.timeToPixelOffsetX(current)
      const timeLine = {
        x: 0,
        y1: 0,
        y2: '100%',
        dateTime: '',
        time: current
      }
      timeLine.x = currentOffset
      timeLine.dateTime = d.toLocaleDateString()
      return timeLine
    }
  },
  /**
   * Created
   */
  created() {
    this.root.$on('recenterPosition', this.recenterPosition)
  },

  /**
   * Mounted
   */
  mounted() {
    this.$nextTick(() => {
      this.$nextTick(() => {
        // because of stupid slider :/
        this.root.scrollToTime(this.timeLinePosition.time)
      })
    })
  },

  methods: {
    /**
     * Recenter position - go to current time line
     */
    recenterPosition() {
      this.root.scrollToTime(this.timeLinePosition.time)
    }
  }
}
</script>
