import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/text.dart';

class CustomeLineChart extends StatefulWidget {
  final List<FlSpot> points;
  final List<String> dates;
  final List<int> titles;
  final List<int> values;

  const CustomeLineChart(
      {super.key,
      required this.points,
      required this.dates,
      required this.titles,
      required this.values});

  @override
  State<CustomeLineChart> createState() => _LineChartSample2State();
}

class _LineChartSample2State extends State<CustomeLineChart> {
  List<Color> gradientColors = [
    AppColors.secondryColor.defaultShade,
    AppColors.primaryColor.defaultShade,
  ];

  bool showAvg = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 1,
      child: IgnorePointer(
        ignoring: widget.dates.isEmpty &&
            widget.titles.isEmpty &&
            widget.points.isEmpty,
        child: Stack(
          children: [
            LineChart(
              mainData(),
            ),
            if (widget.dates.isEmpty &&
                widget.titles.isEmpty &&
                widget.points.isEmpty)
              Positioned.fill(
                  child: Container(
                color: Colors.white.withValues(alpha: 0.4),
                child: Center(
                  child: Text(
                    'گزارشی موجود نیست',
                    style: AppTextStyles.body4,
                  ),
                ),
              ))
          ],
        ),
      ),
    );
  }

  Widget leftTitleWidgets(String text) {
    return SideTitleWidget(
      axisSide: AxisSide.bottom,
      child: Text(text == '0' ? '' : '$text  ',
          textDirection: TextDirection.rtl,
          style: AppTextStyles.body4.copyWith(fontWeight: FontWeight.bold),
          textAlign: TextAlign.left),
    );
  }

  LineChartData mainData() {
    return LineChartData(
      gridData: FlGridData(
        show: true,
        drawVerticalLine: true,
        horizontalInterval: 1,
        verticalInterval: 1,
        getDrawingHorizontalLine: (value) {
          return FlLine(
            color: AppColors.gray.defaultShade,
            strokeWidth: 1,
          );
        },
        getDrawingVerticalLine: (value) {
          return FlLine(
            color: AppColors.gray.defaultShade,
            strokeWidth: 1,
          );
        },
      ),
      titlesData: FlTitlesData(
        show: true,
        rightTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            reservedSize: 30,
            interval: 1,
            getTitlesWidget: (value, meta) => leftTitleWidgets(
                widget.dates.isEmpty &&
                        widget.titles.isEmpty &&
                        widget.points.isEmpty
                    ? value.round().toString()
                    : widget.dates[value.toInt()].toString()),
          ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            interval: 1,
            getTitlesWidget: (value, meta) => leftTitleWidgets(
                widget.dates.isEmpty &&
                        widget.titles.isEmpty &&
                        widget.points.isEmpty
                    ? value.round().toString()
                    : widget.titles[value.toInt()].toString()),
            reservedSize: 30,
          ),
        ),
      ),
      borderData: FlBorderData(
        show: true,
        border: Border.all(color: const Color(0xff37434d)),
      ),
      minX: 0,
      maxX: widget.dates.isEmpty ? 10 : (widget.dates.length - 1).toDouble(),
      minY: 0,
      maxY: widget.titles.isEmpty ? 10 : (widget.titles.length - 1).toDouble(),
      lineTouchData: LineTouchData(
          touchTooltipData: LineTouchTooltipData(
        tooltipRoundedRadius: 10,
        getTooltipItems: (touchedSpots) => List.generate(
          touchedSpots.length,
          (index) => LineTooltipItem(
              widget.values[touchedSpots[index].spotIndex].toString(),
              AppTextStyles.body5),
        ),
        getTooltipColor: (touchedSpot) => Colors.white,
      )),
      lineBarsData: [
        LineChartBarData(
          spots: widget.points,
          isCurved: false,
          gradient: LinearGradient(
            colors: gradientColors,
          ),
          barWidth: 2,
          isStrokeCapRound: true,
          dotData: FlDotData(
            show: true,
            getDotPainter: (p0, p1, p2, p3) => FlDotCirclePainter(
                color: gradientColors.first,
                strokeColor: Colors.white,
                strokeWidth: 2,
                radius: 4),
          ),
          belowBarData: BarAreaData(
            show: true,
            gradient: LinearGradient(
              colors:
                  gradientColors.map((color) => color.withAlpha(30)).toList(),
            ),
          ),
        ),
      ],
    );
  }
}
