import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class LineChartCon extends StatefulWidget {
  final bool isShowingMainData;
  final int lineNum;
  final List? line1;
  final List? line2;
  final List? line3;

  LineChartCon(
      {required this.isShowingMainData,
      required this.lineNum,
      this.line1,
      this.line2,
      this.line3});

  @override
  State<LineChartCon> createState() => _LineChartConState();
}

class _LineChartConState extends State<LineChartCon> {
  double maxX = 0.0;

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

  getMaxX(){
    if (widget.lineNum > 2) {
      if (widget.line3!.length > widget.line2!.length &&
          widget.line3!.length > widget.line1!.length) {
        maxX = double.parse(widget.line3!.length.toString());
      } else if (widget.line2!.length > widget.line3!.length &&
          widget.line2!.length > widget.line1!.length) {
        maxX = double.parse(widget.line2!.length.toString());
      } else {
        maxX = double.parse(widget.line1!.length.toString());
      }
    } else if (widget.lineNum > 1) {

    }
    print('maxx=======$maxX');
    return maxX;
  }

  @override
  Widget build(BuildContext context) {

    return LineChart(
      LineChartData(
        lineTouchData: const LineTouchData(enabled: false),
        gridData: gridData,
        titlesData: titlesData1,
        borderData: borderData,
        lineBarsData: lineBarsData1,
        minX: 1,
        maxX:  getMaxX()>0? getMaxX():3,
        maxY: 6,
        minY: 0,
      ),
      duration: const Duration(milliseconds: 250),
    );
  }

  FlTitlesData get titlesData1 => FlTitlesData(
        bottomTitles: AxisTitles(
          sideTitles: bottomTitles,
        ),
        rightTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        leftTitles: AxisTitles(
          sideTitles: leftTitles(),
        ),
      );

  List<LineChartBarData> get lineBarsData1 => [
        lineChartBarData1_1,
        lineChartBarData1_2,
        lineChartBarData1_3,
      ];

  Widget leftTitleWidgets(double value, TitleMeta meta) {
    String text;
    switch (value.toInt()) {
      case 0:
        text = '0';
        break;
      case 1:
        text = '20%';
        break;
      case 2:
        text = '40%';
        break;
      case 3:
        text = '60%';
        break;
      case 4:
        text = '80%';
        break;
      case 5:
        text = '100%';
        break;
      default:
        return Container();
    }

    return Text(text, style: TextStyle(fontSize: 12.sp, color: const Color(0xFF070707)), textAlign: TextAlign.left);

  /*  return SideTitleWidget(
      axisSide: meta.axisSide,
      child: Text(
        '${value}',
        style: TextStyle(fontSize: 12.sp, color: Color(0xFF070707)),
      ),
    );*/
  }

  SideTitles leftTitles() => SideTitles(
        getTitlesWidget: leftTitleWidgets,
        showTitles: true,
        interval: 1,
        reservedSize: 50,
      );

  Widget bottomTitleWidgets(double value, TitleMeta meta) {
    return SideTitleWidget(
      axisSide: meta.axisSide,
      space: 10,
      child: Text(
        value.toString() == '0.0' ? '' : value.toStringAsFixed(0),
        style: TextStyle(
          fontWeight: FontWeight.w400,
          fontSize: 12.sp,
          color: const Color.fromRGBO(39, 39, 39, 0.5),
        ),
      ),
    );
  }

  SideTitles get bottomTitles => SideTitles(
        showTitles: true,
        reservedSize: 32,
        interval: 1,
        getTitlesWidget: bottomTitleWidgets,
      );

  FlGridData get gridData => const FlGridData(
      show: true, drawVerticalLine: false, horizontalInterval: 1.0);

  FlBorderData get borderData => FlBorderData(
        show: true,
        border: const Border(
          bottom: BorderSide(color: Colors.transparent, width: 1),
          left: BorderSide(color: Colors.transparent),
          right: BorderSide(color: Colors.transparent),
          top: BorderSide(color: Colors.transparent),
        ),
      );

  LineChartBarData get lineChartBarData1_1 => LineChartBarData(
        isCurved: true,
        curveSmoothness: 0,
        color: const Color(0xFF4DE6BF),
        barWidth: 2,
        isStrokeCapRound: true,
        dotData: const FlDotData(show: false),
        belowBarData: BarAreaData(show: false),
        spots: List.generate(widget.line1!.length, (index){
          var element = widget.line1![index];
          return FlSpot(double.parse(element['id'].toString()), double.parse(element['rate'].toString()));
        })
       /* const [
          FlSpot(1, 0),
          FlSpot(3, 1.5),
          FlSpot(5, 1.4),
          FlSpot(7, 3.4),
          FlSpot(10, 2),
          FlSpot(12, 2.2),
          FlSpot(13, 1.8),
        ],*/
      );

  LineChartBarData get lineChartBarData1_2 => LineChartBarData(
        isCurved: true,
        curveSmoothness: 0,
        color: Theme.of(context).primaryColor,
        barWidth: 2,
        isStrokeCapRound: true,
        dotData: const FlDotData(show: false),
        belowBarData: BarAreaData(
          show: false,
          color: Colors.yellow,
        ),
        spots:  List.generate(widget.line2!.length, (index){
          var element = widget.line2![index];
          return FlSpot(double.parse(element['id'].toString()), double.parse(element['rate'].toString()));
        })
      /*  const [
          FlSpot(1, 1),
          FlSpot(3, 2.8),
          FlSpot(7, 1.2),
          FlSpot(10, 2.8),
          FlSpot(12, 2.6),
          FlSpot(13, 3.9),
        ],*/
      );

  LineChartBarData get lineChartBarData1_3 => LineChartBarData(
        isCurved: true,
        curveSmoothness: 0,
        color: const Color(0xFF1890FF),
        barWidth: 2,
        isStrokeCapRound: true,
        dotData: const FlDotData(show: false),
        belowBarData: BarAreaData(show: false),
        spots:  List.generate(widget.line3!.length, (index){
          var element = widget.line3![index];
          return FlSpot(double.parse(element['id'].toString()), double.parse(element['rate'].toString()));
        })
       /* const [
          FlSpot(1, 2.8),
          FlSpot(3, 1.9),
          FlSpot(6, 3),
          FlSpot(10, 1.3),
          FlSpot(13, 2.5),
        ],*/
      );
}
