/**
 * This file is part of the SplendidBear Websites' projects.
 *
 * Copyright (c) 2026 @ www.splendidbear.org
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import React from 'react';
import { BarChart } from '@mui/x-charts/BarChart';
import { LineChart } from '@mui/x-charts/LineChart';
import { PieChart } from '@mui/x-charts/PieChart';
import { createTheme, ThemeProvider } from '@mui/material/styles';
import { shape, arrayOf, number, string } from 'prop-types';

const darkTheme = createTheme({
  palette: {
    mode: 'dark',
    background: { paper: 'transparent' },
  },
  typography: {
    fontFamily: '\'Rajdhani\', sans-serif',
  },
});

const WIN_COLOR = '#5ee89a';
const LOSS_COLOR = '#f67d52';
const DRAW_COLOR = '#95cff5';
const MINES_COLOR = '#f67d52';
const BONUS_COLOR = '#ffd700';

const axisStyle = {
  tickLabelStyle: { fill: 'rgba(255,255,255,0.4)', fontSize: 11, fontFamily: '\'Rajdhani\', sans-serif' },
  labelStyle: { fill: 'rgba(255,255,255,0.4)', fontSize: 11, fontFamily: '\'Rajdhani\', sans-serif' },
};

export default function ProfileCharts({ chartData }) {
  const { months, wins, losses, draws, pieWins, pieLosses, pieDraws, recentGames } = chartData;
  const total = pieWins + pieLosses + pieDraws;

  const hasBars = wins.some(v => 0 < v) || losses.some(v => 0 < v) || draws.some(v => 0 < v);
  const hasRecent = recentGames
    && (recentGames.mines?.some(v => 0 < v) || recentGames.bonus?.some(v => 0 < v));

  return (
    <ThemeProvider theme={darkTheme}>
      <div className="profile-charts">
        {0 < total && (
          <div className="profile-chart-block">
            <h2 className="profile-section__title">
              <i className="fa fa-pie-chart" /> Result Breakdown
            </h2>
            <div className="profile-chart-inner">
              <PieChart
                series={[{
                  data: [
                    { id: 0, value: pieWins, label: `Wins ${pieWins}`, color: WIN_COLOR },
                    { id: 1, value: pieLosses, label: `Losses ${pieLosses}`, color: LOSS_COLOR },
                    { id: 2, value: pieDraws, label: `Draws ${pieDraws}`, color: DRAW_COLOR },
                  ],
                  innerRadius: 52,
                  paddingAngle: 3,
                  cornerRadius: 4,
                  highlightScope: { fade: 'global', highlight: 'item' },
                }]}
                slotProps={{
                  legend: {
                    labelStyle: {
                      fill: 'rgba(255,255,255,0.55)',
                      fontSize: 13,
                      fontFamily: '\'Rajdhani\', sans-serif',
                    },
                  },
                }}
                width={320}
                height={200}
                margin={{ top: 10, bottom: 10, left: 10, right: 120 }}
              />
            </div>
          </div>
        )}

        {hasBars && (
          <div className="profile-chart-block">
            <h2 className="profile-section__title">
              <i className="fa fa-bar-chart" /> Activity (6 months)
            </h2>
            <div className="profile-chart-inner">
              <BarChart
                xAxis={[{ scaleType: 'band', data: months, ...axisStyle }]}
                yAxis={[{ ...axisStyle }]}
                series={[
                  { data: wins, label: 'Wins', color: WIN_COLOR, stack: 'total' },
                  { data: losses, label: 'Losses', color: LOSS_COLOR, stack: 'total' },
                  { data: draws, label: 'Draws', color: DRAW_COLOR, stack: 'total' },
                ]}
                slotProps={{
                  legend: {
                    labelStyle: {
                      fill: 'rgba(255,255,255,0.55)',
                      fontSize: 13,
                      fontFamily: '\'Rajdhani\', sans-serif',
                    },
                  },
                }}
                borderRadius={3}
                width={460}
                height={200}
                margin={{ top: 10, bottom: 30, left: 30, right: 120 }}
              />
            </div>
          </div>
        )}

        {hasRecent && (
          <div className="profile-chart-block profile-chart-block--wide">
            <h2 className="profile-section__title">
              <i className="fa fa-line-chart" /> Last {recentGames.labels.length} games — mines & bonus
            </h2>
            <div className="profile-chart-inner">
              <LineChart
                xAxis={[{ scaleType: 'band', data: recentGames.labels, ...axisStyle }]}
                yAxis={[{ ...axisStyle }]}
                series={[
                  { data: recentGames.mines, label: 'Mines hit', color: MINES_COLOR },
                  { data: recentGames.bonus, label: 'Bonus points', color: BONUS_COLOR },
                ]}
                slotProps={{
                  legend: {
                    labelStyle: {
                      fill: 'rgba(255,255,255,0.55)',
                      fontSize: 13,
                      fontFamily: '\'Rajdhani\', sans-serif',
                    },
                  },
                }}
                borderRadius={3}
                height={220}
                margin={{ top: 10, bottom: 30, left: 40, right: 140 }}
              />
            </div>
          </div>
        )}
      </div>
    </ThemeProvider>
  );
}

ProfileCharts.propTypes = {
  chartData: shape({
    months:     arrayOf(string).isRequired,
    wins:       arrayOf(number).isRequired,
    losses:     arrayOf(number).isRequired,
    draws:      arrayOf(number).isRequired,
    pieWins:    number.isRequired,
    pieLosses:  number.isRequired,
    pieDraws:   number.isRequired,
    recentGames: shape({
      labels: arrayOf(string).isRequired,
      mines:  arrayOf(number).isRequired,
      bonus:  arrayOf(number).isRequired,
    }).isRequired,
  }).isRequired,
};
