import 'package:hmg_patient_app/widgets/charts/app_time_series_chart.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

import '../../../../Constants.dart';

class MonthLineChartCurved extends StatelessWidget {
  final String? title;
  final List<TimeSeriesSales3>? timeSeries;
  final int? indexes;
  final double? horizontalInterval;

  MonthLineChartCurved({this.title, this.timeSeries, this.indexes, this.horizontalInterval = 15.0});

  List<int> xAxixs = [];
  List<double> yAxixs = [];

  @override
  Widget build(BuildContext context) {
    getXaxix();
    getYaxix();
    return AspectRatio(
      aspectRatio: 1.1,
      child: Container(
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(18)),
          // color: Colors.white,
        ),
        child: Stack(
          children: <Widget>[
            Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                const SizedBox(
                  height: 4,
                ),
                Text(
                  title!,
                  style: TextStyle(color: Colors.black, fontSize: 16, letterSpacing: -0.64, fontWeight: FontWeight.w600),
                  textAlign: TextAlign.center,
                ),
                SizedBox(
                  height: 10,
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(right: 18.0, left: 16.0, top: 15, bottom: 15),
                    child: LineChart(
                      sampleData1(context),
                      duration: const Duration(milliseconds: 250),
                    ),
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  getXaxix() {
    for (int index = 0; index < timeSeries!.length; index++) {
      int mIndex = indexes! * index;
      if (mIndex < timeSeries!.length) {
        xAxixs.add(mIndex);
      }
    }
  }

  getYaxix() {
    int indexess = (timeSeries!.length * 0.30).toInt();
    for (int index = 0; index < timeSeries!.length; index++) {
      int mIndex = indexess * index;
      if (mIndex < timeSeries!.length) {
        yAxixs.add(timeSeries![mIndex].sales);
      }
    }
  }

  LineChartData sampleData1(context) {
    return LineChartData(
      lineTouchData: LineTouchData(
        touchTooltipData: LineTouchTooltipData(
          // tooltipBgColor: Colors.white,
        ),
        touchCallback: (touchEvent, LineTouchResponse? touchResponse) {},
        handleBuiltInTouches: true,
      ),
      gridData: FlGridData(
        horizontalInterval: 12,
        show: true,
        drawVerticalLine: true,
        drawHorizontalLine: true,
        verticalInterval: 12,
      ),
      titlesData: FlTitlesData(
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
              showTitles: true,
              getTitlesWidget: (value, title) {
                TextStyle styl = const TextStyle(
                  color: Colors.black,
                  fontSize: 10,
                );
                return Text(
                  '${value.toInt()}',
                  style: styl,
                );
              }
              //  margin: 22,
              ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            getTitlesWidget: (value, title) {
              TextStyle styl = const TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold,
                fontSize: 10,
              );
              return Text(
                '',
                style: styl,
              );
            },
            interval: getMaxY() - getMinY() <= 500
                ? 50
                : getMaxY() - getMinY() <= 1000
                    ? 100
                    : 200,

            // getTitles: (value) {
            //   if (value.toInt() == 0)
            //     return '${value.toInt()}';
            //   else if (value.toInt() % horizontalInterval == 0)
            //     return '${value.toInt()}';
            //   else
            //     return '';
            // },
            //   margin: 12,
          ),
        ),
      ),
      borderData: FlBorderData(
        show: true,
        border: const Border(
          bottom: BorderSide(
            color: Colors.black,
            width: 0.5,
          ),
          left: BorderSide(
            color: Colors.black,
          ),
          right: BorderSide(
            color: Colors.black,
          ),
          top: BorderSide(
            color: Colors.transparent,
          ),
        ),
      ),
      minX: 0,
      maxX: (timeSeries!.length - 1).toDouble(),
      maxY: getMaxY() + 0.3,
      minY: getMinY(),
      lineBarsData: getData(context),
    );
  }

  double getMaxY() {
    double max = 0;
    timeSeries!.forEach((element) {
      double resultValueDouble = element.sales;
      if (resultValueDouble > max) max = resultValueDouble;
    });

    return max.roundToDouble();
  }

  double getMinY() {
    double min = timeSeries![0].sales;
    timeSeries!.forEach((element) {
      double resultValueDouble = element.sales;
      if (resultValueDouble < min) min = resultValueDouble;
    });
    int value = min.toInt();

    return value.toDouble();
  }

  List<LineChartBarData> getData(context) {
    List<FlSpot> spots = [];
    if (timeSeries!.length == 0) {
      spots.add(FlSpot(0, 0));
    }
    for (int index = 0; index < timeSeries!.length; index++) {
      spots.add(FlSpot(index.toDouble(), timeSeries![index].sales));
    }

    final LineChartBarData lineChartBarData1 = LineChartBarData(
      spots: spots,
      isCurved: true,
      color: secondaryColor,
      barWidth: 1.5,
      isStrokeCapRound: true,
      curveSmoothness: 0.0,
      dotData: FlDotData(
        show: false,
      ),
      belowBarData: BarAreaData(
        show: false,
      ),
    );

    return [
      lineChartBarData1,
    ];
  }
}
