<template>
  <div class="page-container md-layout-column" id="dashboard-container">

    <!-- toolbar -->
    <toolbar @showDrawerClicked="showDrawer = true" :processing="true" :model-changed="true"/>

    <!-- drawer -->
    <md-drawer :md-active.sync="showDrawer" md-swipeable>
      <workspace-drawer-content/>
    </md-drawer>

    <!-- workspace -->
    <md-content id="diagram-container">
      <simple-flowchart :scene.sync="diagram"></simple-flowchart>
      <element-adder @addElement="newElement"/>
    </md-content>

  </div>
</template>

<script>

import SimpleFlowchart from '@/simple-flowchart';
import ElementAdder from "@/components/ElementAdder";
import WorkspaceDrawerContent from "@/components/WorkspaceDrawerContent";
import Toolbar from "@/components/Toolbar";
import {maxBy} from 'lodash';

export default {
  name: 'Dashboard',
  components: {
    SimpleFlowchart,
    ElementAdder,
    WorkspaceDrawerContent,
    Toolbar
  },
  data: () => ({
    showDrawer: false,
    diagram: {
      centerX: 0,
      centerY: 0,
      scale: 1,
      nodes: [],
      links: []
    }
  }),
  methods: {
    newElement(type) {
      this.showNewElementChooser = false;

      let newNode = {
        x: 10,
        y: 10,
        type
      }

      switch (type) {
        case "ingest":
          newNode.data = {
            "url": "https://videon.test.kmlabz.com/live"
          }
          break;
        case "encoder":
          newNode.data = {
            "bitrate": 8000,
            "width": 600,
            "height": 800
          }
          break;
        case "restreamer":
          newNode.data = {
            "url": "https://youtube.com/live",
            "streamkey": "testkey"
          }
          break;
      }

      if (this.diagram.nodes.length > 0) {
        const max_id = maxBy(this.diagram.nodes, 'id').id

        newNode.id = max_id + 1;
      } else {
        newNode.id = 1;
      }

      this.diagram.nodes.push(newNode)

    }
  }

}
</script>

<style scoped>
#dashboard-container {
  height: 100%;
}

#diagram-container {
  height: calc(100% - 64px);
}
</style>