<template lang="html">
  <div class="content"></div>
</template>

<script>
import * as am5 from "@amcharts/amcharts5";
import * as am5xy from "@amcharts/amcharts5/xy";
import am5themes_Animated from "@amcharts/amcharts5/themes/Animated";


export default {
  props: {
    chartData: {
      type: Array,
      default: [],
    },
  },
  data: function () {
    return {};
  },
  methods: {
    chartCreate: function (data) {
      let chartWarp = this.$parent.$refs.chartdiv; // 차트 상위 div ref 매칭
      chartWarp.innerHTML = ""; // 차트 상위 div 내용 초기화 (기존 차트 삭제)
      let div = document.createElement("div"); // 차트를 담을 빈 div 생성 (차트 하위 div)
      div.style.width = "100%"; // 차트를 담을 div의 넓이
      div.style.height = "100%"; // 차트를 담을 div의 높이
      let root = am5.Root.new(div); // 차트 하위 div에 차트(root) 담기
      chartWarp.appendChild(div); // 차트 상위 div 안에 차트 하위 div를 추가
      this.charts = root; // 차트 정보 전역에 담기

      root._logo.dispose();

      // Set themes
      // https://www.amcharts.com/docs/v5/concepts/themes/
      root.setThemes([
        am5themes_Animated.new(root)
      ]);

      root.dateFormatter.setAll({
        dateFormat: "yyyy",
        dateFields: ["valueX"]
      });

      


      // Create chart
      // https://www.amcharts.com/docs/v5/charts/xy-chart/
      let chart = root.container.children.push(am5xy.XYChart.new(root, {
        focusable: true,
        panX: true,
        panY: true,
        wheelX: "panX",
        wheelY: "zoomX",
        pinchZoomX: true,
        paddingLeft: 0
      }));

      let easing = am5.ease.linear;


      // Create axes
      // https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
      let xAxis = chart.xAxes.push(am5xy.DateAxis.new(root, {
        maxDeviation: 0.1,
        groupData: false,
        baseInterval: {
          timeUnit: "day",
          count: 1
        },
        renderer: am5xy.AxisRendererX.new(root, {
          minorGridEnabled: true,
          minGridDistance: 70
        }),
        tooltip: am5.Tooltip.new(root, {})
      }));

      let yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
        maxDeviation: 0.2,
        renderer: am5xy.AxisRendererY.new(root, {})
      }));


      // Add series
      // https://www.amcharts.com/docs/v5/charts/xy-chart/series/
      let series = chart.series.push(am5xy.LineSeries.new(root, {
        minBulletDistance: 10,
        connect: false,
        xAxis: xAxis,
        yAxis: yAxis,
        valueYField: "value",
        valueXField: "date",
        tooltip: am5.Tooltip.new(root, {
          pointerOrientation: "horizontal",
          labelText: "{valueY}"
        })
      }));

      series.fills.template.setAll({
        fillOpacity: 0.2,
        visible: true
      });

      series.strokes.template.setAll({
        strokeWidth: 2
      });


      // Set up data processor to parse string dates
      // https://www.amcharts.com/docs/v5/concepts/data/#Pre_processing_data
      series.data.processor = am5.DataProcessor.new(root, {
        dateFormat: "yyyy-MM-dd",
        dateFields: ["date"]
      });

      series.data.setAll(data);

      series.bullets.push(function () {
        let circle = am5.Circle.new(root, {
          radius: 4,
          fill: root.interfaceColors.get("background"),
          stroke: series.get("fill"),
          strokeWidth: 2
        })

        return am5.Bullet.new(root, {
          sprite: circle
        })
      });


      // Add cursor
      // https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
      let cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
        xAxis: xAxis,
        behavior: "none"
      }));
      cursor.lineY.set("visible", false);

      // add scrollbar
      chart.set("scrollbarX", am5.Scrollbar.new(root, {
        orientation: "horizontal"
      }));


      // Make stuff animate on load
      // https://www.amcharts.com/docs/v5/concepts/animations/
      chart.appear(1000, 100);
    },
  },
  computed: {},
  watch: {
    chartData: function (newData) {
      if (this.chartData != null && this.chartData.length > 0) {
        this.chartCreate(newData);
      }
    },
  },
  //beforeCreate: function () {},
  //created: function () {},
  //beforeUpdate: function () {},
  //updated: function () {},
  mounted: function () { },
};
</script>
