import 'package:agrilink_vocpro/core/constant/app_theme.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class GarphicWidget extends StatelessWidget {
  const GarphicWidget(
      {super.key,
      required this.gradientColors,
      this.data,
      this.hour,
      this.maxValue = 100});

  final List<Color> gradientColors;
  final List<num>? hour;
  final List<num>? data;
  final double maxValue;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(8.r),
      child: LineChart(
        mainData(),
      ),
    );
  }

  Widget bottomTitleWidgets(double value, TitleMeta meta) {
    TextStyle style = AppTheme.labelSmall;
    Widget text;
    switch (value.toInt()) {
      case 1:
        text = Text('01.00', style: style);
        break;
      case 6:
        text = Text('06.00', style: style);
        break;
      case 12:
        text = Text('12.00', style: style);
        break;
      case 18:
        text = Text('18.00', style: style);
        break;
      case 23:
        text = Text('23.00', style: style);
        break;
      default:
        text = Text('', style: style);
        break;
    }

    return SideTitleWidget(
      axisSide: meta.axisSide,
      child: text,
    );
  }

  LineChartData mainData() {
    return LineChartData(
      gridData: const FlGridData(
        show: false,
      ),
      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: bottomTitleWidgets,
          ),
        ),
        leftTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
      ),
      borderData: FlBorderData(
        show: false,
        border: Border.all(color: const Color(0xff37434d)),
      ),
      minX: 0,
      maxX: 24,
      minY: 0,
      maxY: data == null ? 0 : maxValue,
      lineBarsData: [
        LineChartBarData(
          spots: data == null && hour == null
              ? [FlSpot(0, 0)]
              : List.generate(
                  hour!.length,
                  (index) =>
                      FlSpot(hour![index].toDouble(), data![index].toDouble()),
                ),
          isCurved: true,
          gradient: LinearGradient(
            colors: gradientColors,
          ),
          barWidth: 5,
          isStrokeCapRound: true,
          dotData: const FlDotData(
            show: false,
          ),
          belowBarData: BarAreaData(
            show: true,
            gradient: LinearGradient(
                colors: gradientColors
                    .map((color) => color.withOpacity(0.3))
                    .toList()),
          ),
        ),
      ],
    );
  }
}
