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()
      : 1;

  double get _minY => alarmInfo.yaxisMin;

  double get _maxY {
    final raw = alarmInfo.yaxisMax > alarmInfo.yaxisMin
        ? alarmInfo.yaxisMax
        : alarmInfo.yaxisMin + 100;
    // 留 10% 余量，防止顶部标签贴边
    return raw * 1.1;
  }

  double get _horizontalInterval => _maxY / 5;

  double get _bottomInterval {
    final len = alarmInfo.temperatureSpots.length;
    if (len <= 1) return 1;
    if (len <= 6) return 1;
    if (len <= 12) return 2;
    return (len - 1) / 4;
  }

  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.symmetric(horizontal: 50.w, vertical: 25.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.r),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                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: 30.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: 24.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: 24.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,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
