<template>
  <q-card v-bind="$attrs" class="q-px-md full-height column justify-between">
    <q-card-section class="q-pa-none">
      <div class="row justify-between no-wrap items-start q-py-md q-px-sm">
        <div>
          <div class="text-bold text-description q-mb-sm">
            {{ String(props.title).toLocaleUpperCase() }}
          </div>
          <span>{{ props.subTitle }}</span>
        </div>
        <q-btn
          icon="mdi-tray-arrow-down"
          class="q-ml-md"
          dense
          flat
          @click="downloadImage"
        />
      </div>
      <q-separator dark />
    </q-card-section>
    <div class="graph-container">
      <Doughnut
        ref="chart_ref"
        :options="chartOptions"
        :data="chartData"
        :plugins="[ChartDataLabels, gaugeNeedle]"
      />
    </div>
  </q-card>
</template>

<script setup>
import { ref } from "vue";
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js";
import { Doughnut } from "vue-chartjs";
import ChartDataLabels from "chartjs-plugin-datalabels";
import { base64ToJPEG } from "src/helpers/convertBase64Image";

const props = defineProps({
  title: {
    type: String,
    required: true,
  },
  valorAgulha: {
    type: Number,
    required: true,
  },
  subTitle: {
    type: String,
    default: null,
  },
});

const chart_ref = ref(null);
const titulo = ref(`Valor: ${props.valorAgulha}`);

const gaugeNeedle = {
  id: "gaugeNeedle",
  afterDatasetsDraw(chart) {
    const { ctx, data } = chart;
    ctx.save();

    const needleValue = data.datasets[0].needleValue;
    const xCenter = chart.getDatasetMeta(0).data[0].x;
    const yCenter = chart.getDatasetMeta(0).data[0].y;
    const outerRadius = chart.getDatasetMeta(0).data[0].outerRadius - 40;
    const angle = Math.PI;

    let circumference =
      (chart.getDatasetMeta(0).data[0].circumference /
        Math.PI /
        data.datasets[0].data[0]) *
      needleValue;

    const needleAngleValue = circumference + 1.5;

    ctx.translate(xCenter, yCenter);
    ctx.rotate(angle * needleAngleValue);

    // Draw the needle
    ctx.beginPath();
    ctx.strokeStyle = "grey";
    ctx.fillStyle = "grey";
    ctx.moveTo(0 - 4, 0);
    ctx.lineTo(0, -outerRadius);
    ctx.lineTo(0 + 4, 0);
    ctx.stroke();
    ctx.fill();

    ctx.beginPath();
    ctx.arc(0, 0, 8, 0, 2 * Math.PI);
    ctx.fillStyle = "grey";
    ctx.fill();

    ctx.restore();
  },
};

ChartJS.register(ArcElement, Tooltip, Legend);

const chartData = ref({
  datasets: [
    {
      backgroundColor: [
        "#00a550",
        "#4dbb7e",
        "#9ad2ad",
        "#cce156",
        "#fff100",
        "#ffbe00",
        "#ff8c00",
        "#FC3D23",
        "#D01616",
        "#8A0000",
      ],
      data: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      needleValue: props.valorAgulha,
      borderColor: "transparent",
    },
  ],
});

const chartOptions = ref({
  rotation: 270,
  circumference: 180,
  cutout: "50%",
  plugins: {
    tooltip: {
      enabled: false,
    },
    title: {
      display: true,
      text: titulo.value,
      color: "#ffffff",
      position: "bottom",
    },
    datalabels: {
      color: "black",
      font: {
        size: 14,
        weight: "bold",
      },
      formatter: (value, ctx) => {
        const valor = ctx.dataIndex;
        return valor;
      },
    },
  },
});

addEventListener("resize", () => {
  if (chart_ref.value) {
    chart_ref.value.chart.update();
  }
});

const downloadImage = () => {
  const image = chart_ref.value.chart?.toBase64Image("image/jpeg", 1);
  base64ToJPEG(image, props.title);
};
</script>

<style scoped>
.graph-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-height: 300px;
}

@media screen and (max-width: 1600px) {
  .graph-container {
    max-height: 250px;
  }
}

@media screen and (max-width: 1360px) {
  .graph-container {
    max-height: 200px;
  }
}

@media screen and (max-width: 1130px) {
  .graph-container {
    max-height: 175px;
  }
}

@media screen and (max-width: 888px) {
  .graph-container {
    max-height: 250px;
  }
}

@media screen and (max-width: 650px) {
  .graph-container {
    max-height: 300px;
  }
}
</style>
