import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:smart_hotel_app/views/home/abnormal/cubit/abnormal_state.dart';

class TemperatureCharBlock extends StatelessWidget {
  final AlarmInfo alarmInfo;

  const TemperatureCharBlock({super.key, required this.alarmInfo});

  double get _minX => 0;

  double get _maxX => alarmInfo.temperatureSpots.length > 1
      ? (alarmInfo.temperatureSpots.length - 1).toDouble()
      : 24;

  double get _minY => alarmInfo.yaxisMin;

  double get _maxY => alarmInfo.yaxisMax > alarmInfo.yaxisMin
      ? alarmInfo.yaxisMax
      : alarmInfo.yaxisMin + 100;

  double get _horizontalInterval {
    final interval = (_maxY - _minY) / 5;
    return interval > 0 ? interval : 20;
  }

  double get _bottomInterval => alarmInfo.temperatureSpots.length > 1
      ? (alarmInfo.temperatureSpots.length - 1) / 4
      : 6;

  String _formatBottomTitle(double value) {
    final index = value.toInt();
    if (index < 0 || index >= alarmInfo.temperatureTimes.length) {
      return '';
    }
    final time = alarmInfo.temperatureTimes[index];
    if (time.length >= 5) {
      return time.substring(0, 5);
    }
    return time;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 10.h),
      padding: EdgeInsets.all(25.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.r),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                alarmInfo.chartTitle.isEmpty
                    ? '24小时温度趋势'
                    : alarmInfo.chartTitle,
                style: TextStyle(
                  fontSize: 24.sp,
                  color: const Color.fromRGBO(100, 116, 139, 1),
                ),
              ),
              Text(
                alarmInfo.date,
                style: TextStyle(
                  fontSize: 24.sp,
                  color: const Color.fromRGBO(100, 116, 139, 1),
                ),
              ),
            ],
          ),
          SizedBox(height: 20.h),
          SizedBox(
            height: 250.h,
            child: LineChart(
              LineChartData(
                gridData: FlGridData(
                  show: true,
                  drawHorizontalLine: true,
                  drawVerticalLine: false,
                  horizontalInterval: _horizontalInterval,
                  getDrawingHorizontalLine: (value) {
                    return const FlLine(
                      color: Color.fromRGBO(209, 213, 219, 1),
                      strokeWidth: 2,
                      dashArray: [5, 5],
                    );
                  },
                ),
                titlesData: FlTitlesData(
                  rightTitles: const AxisTitles(
                    sideTitles: SideTitles(showTitles: false),
                  ),
                  topTitles: const AxisTitles(
                    sideTitles: SideTitles(showTitles: false),
                  ),
                  bottomTitles: AxisTitles(
                    sideTitles: SideTitles(
                      showTitles: true,
                      interval: _bottomInterval,
                      getTitlesWidget: (value, meta) {
                        return Text(
                          _formatBottomTitle(value),
                          style: TextStyle(
                            color: const Color.fromRGBO(156, 163, 175, 1),
                            fontSize: 14.sp,
                          ),
                        );
                      },
                      reservedSize: 40,
                    ),
                  ),
                  leftTitles: AxisTitles(
                    sideTitles: SideTitles(
                      showTitles: true,
                      interval: _horizontalInterval,
                      getTitlesWidget: (value, meta) {
                        return Text(
                          '${value.toInt()}',
                          style: TextStyle(
                            color: const Color.fromRGBO(156, 163, 175, 1),
                            fontSize: 14.sp,
                          ),
                        );
                      },
                      reservedSize: 40,
                    ),
                  ),
                ),
                borderData: FlBorderData(show: false),
                minX: _minX,
                maxX: _maxX,
                minY: _minY,
                maxY: _maxY,
                lineBarsData: [
                  LineChartBarData(
                    spots: alarmInfo.temperatureSpots,
                    isCurved: true,
                    color: const Color.fromRGBO(239, 68, 68, 1),
                    barWidth: 4,
                    isStrokeCapRound: true,
                    dotData: const FlDotData(show: false),
                    belowBarData: BarAreaData(
                      show: true,
                      gradient: const LinearGradient(
                        colors: [
                          Color.fromRGBO(254, 202, 202, 1),
                          Color.fromRGBO(254, 226, 226, 0.1),
                        ],
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
