import 'package:flutter/material.dart';

import '../model/reading.dart';
import '../services/date_utility.dart';

class FloodStationTable extends StatelessWidget {
  const FloodStationTable({super.key, required List<Reading> readings})
      : _readings = readings;

  final List<Reading> _readings;

  List<TableRow> get _children {
    return _readings.map<TableRow>((e) => _getTableRow(e)).toList();
  }

  TableRow _getTableRow(Reading reading) {
    String date = DateUtility.formatDateToYmd(reading.dateTime);
    String time = DateUtility.formatDateToHm(reading.dateTime);
    return TableRow(
      decoration: BoxDecoration(),
      children: [
        Row(
          children: [
            Text(
              date,
              style: TextStyle(
                fontSize: 16,
              ),
            ),
            Padding(padding: EdgeInsets.only(right: 12)),
            Text(
              time,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
          ],
        ),
        Text(
          reading.value.toString(),
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 16,
            backgroundColor: _getColorFromSeverity(reading.value),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Table(
      children: _children,
      columnWidths: {
        0: FixedColumnWidth(160),
        1: FlexColumnWidth(),
      },
    );
  }

  Color _getColorFromSeverity(double value) {
    if (value < 1.0) return Color.fromARGB(0, 0, 0, 0);
    if (value < 2.0) return Colors.yellow;
    if (value < 3.0) return Colors.orange;
    return Colors.red;
  }
}
