<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<template>
  <div class="charts">
    <div>
      <span
        class="charts-item mr-5"
        v-for="(i, index) in traceStore.serviceList"
        :key="index"
        :style="`color:${getServiceColor(i)}`"
      >
        <Icon iconName="issue-open-m" class="mr-5" size="sm" />
        <span>{{ i }}</span>
      </span>
      <el-button class="btn" size="small" @click="downloadTrace">
        <Icon iconName="download" size="sm" />
      </el-button>
    </div>
    <div class="list">
      <Graph
        :data="data"
        :traceId="traceId"
        :type="TraceGraphType.LIST"
        :selectedMaxTimestamp="selectedMaxTimestamp"
        :selectedMinTimestamp="selectedMinTimestamp"
        :minTimestamp="minTimestamp"
        :maxTimestamp="maxTimestamp"
      />
    </div>
  </div>
</template>
<script lang="ts" setup>
  import * as d3 from "d3";
  import { useAppStoreWithOut } from "@/store/modules/app";
  import { useTraceStore } from "@/store/modules/trace";
  import type { Span } from "@/types/trace";
  import Graph from "../D3Graph/Index.vue";
  import { Themes } from "@/constants/data";
  import { TraceGraphType } from "./constant";
  import { getServiceColor } from "@/utils/color";

  /* global defineProps, Indexable*/
  type Props = {
    data: Span[];
    traceId: string;
    selectedMaxTimestamp?: number;
    selectedMinTimestamp?: number;
    minTimestamp: number;
    maxTimestamp: number;
  };
  defineProps<Props>();
  const appStore = useAppStoreWithOut();
  const traceStore = useTraceStore();

  function downloadTrace() {
    const serializer = new XMLSerializer();
    const svgNode: any = d3.select(".trace-list").node();
    const source = `<?xml version="1.0" standalone="no"?>\r\n${serializer.serializeToString(svgNode)}`;
    const canvas = document.createElement("canvas");
    const context: any = canvas.getContext("2d");
    canvas.width = (d3.select(".trace-list") as Indexable)._groups[0][0].clientWidth;
    canvas.height = (d3.select(".trace-list") as Indexable)._groups[0][0].clientHeight;
    context.fillStyle = appStore.theme === Themes.Dark ? "#212224" : `#fff`;
    context.fillRect(0, 0, canvas.width, canvas.height);
    const image = new Image();
    image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(source)}`;
    image.onload = () => {
      context.drawImage(image, 0, 0);
      const tagA = document.createElement("a");
      tagA.download = "trace-list.png";
      tagA.href = canvas.toDataURL("image/png");
      tagA.click();
    };
  }
</script>
<style lang="scss" scoped>
  .charts {
    overflow: auto;
    padding: 10px;
    height: 100%;
    width: 100%;
  }

  .charts-item {
    display: inline-block;
    padding: 2px 8px;
    border: 1px solid;
    font-size: 11px;
    border-radius: 4px;
    margin: 3px;
  }

  .btn {
    float: right;
  }

  .list {
    height: calc(100% - 150px);
    position: relative;
  }

  .event-tag {
    color: red;
  }
</style>
