import Stack from '@material-hu/mui/Stack';
import useTheme from '@material-hu/mui/styles/useTheme';

import BarChart from '@material-hu/components/design-system/Charts/BarChart';

import ServiceItemStateChartSkeleton from './ServiceItemStateChartSkeleton';

type Props = {
  data: number[];
  labels: string[];
  loading: boolean;
};

const ServiceItemsStateChart = ({ labels, data, loading }: Props) => {
  const theme = useTheme();
  // Main and hover colors for the chart
  const colors = [
    [
      theme.palette.new.action.background.brand.disabled,
      theme.palette.new.text.neutral.disabled,
    ],
    [theme.palette.newBase?.purple[500], theme.palette.newBase?.purple[600]],
    [theme.palette.newBase?.skyBlue[500], theme.palette.newBase?.skyBlue[600]],
    [theme.palette.newBase?.yellow[500], theme.palette.newBase?.yellow[600]],
    [theme.palette.newBase?.green[500], theme.palette.newBase?.green[600]],
    [theme.palette.newBase?.red[500], theme.palette.newBase?.red[600]],
  ];

  if (loading) return <ServiceItemStateChartSkeleton />;

  return (
    <Stack sx={{ height: 220 }}>
      <BarChart
        type="bar"
        data={{
          labels,
          datasets: [
            {
              data: data,
              backgroundColor: colors.map(color => color[0]),
              hoverBackgroundColor: colors.map(color => color[1]),
              borderRadius: 4,
              borderSkipped: 'start',
            },
          ],
        }}
        options={{
          maintainAspectRatio: false,
          indexAxis: 'y',
          plugins: {
            legend: { display: false },
            tooltip: {
              titleFont: {
                family: 'Roboto',
                size: 16,
                weight: 600,
              },
              bodyFont: {
                family: 'Roboto',
                size: 14,
              },
              callbacks: {
                label: ctx => {
                  const value = ctx.parsed.x;
                  const total = (ctx.dataset.data as number[]).reduce(
                    (a, b) => a + b,
                    0,
                  );

                  if (!value || !total) return '';

                  const percentage = ((value / total) * 100).toFixed(2);
                  return `${percentage}% (${value.toLocaleString()})`;
                },
              },
            },
          },
          scales: {
            x: {
              beginAtZero: true,
              grid: { display: false },
              border: { display: false },
              ticks: {
                color: theme.palette.new.text.neutral.default,
                font: {
                  family: 'Roboto',
                  size: 14,
                },
                stepSize: Math.max(...data) <= 10 ? 1 : undefined,
              },
            },
            y: {
              grid: { display: true },
              border: { display: true },
              ticks: {
                color: theme.palette.new.text.neutral.default,
                font: {
                  family: 'Roboto',
                  size: 16,
                },
              },
            },
          },
        }}
      />
    </Stack>
  );
};

export default ServiceItemsStateChart;
