import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:jiffy/jiffy.dart';
import 'package:tp5/lido/model/Lidoapi_ofp.dart';
import 'package:tp5/widgets/my_row.dart';

class WLidoOfp extends StatelessWidget {
  const WLidoOfp({super.key, required this.lidoapiofp, this.onTap});
  final LidoapiOfp lidoapiofp;

  final dynamic onTap;

  Map<String, dynamic> get categories =>
      lidoapiofp.categoriesNames?.toJson() ?? {};

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Column(children: [
        ...lidoapiofp.briefingPackages.map((brief) => Card(
              color: Colors.grey[900],
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(6.0),
              ),
              elevation: 8.0,
              margin:
                  const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
              child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(children: [
                    MyRow(children: [
                      ...brief.leg.map((leg) => Column(children: [
                            MyRow(children: [
                              const Text("Schedule:"),
                              _titleInfo(
                                  "Schedule",
                                  "${Jiffy.parseFromMillisecondsSinceEpoch(leg.scheduledDepartureTime ?? 0).format(pattern: "Hm")}-${Jiffy.parseFromMillisecondsSinceEpoch(leg.scheduledTimeOfArrival ?? 0).format(pattern: "Hm")}",
                                  Jiffy.parseFromMillisecondsSinceEpoch(
                                          leg.scheduledDepartureTime ?? 0)
                                      .format(pattern: "dd MMMyy")),
                              const Gap(15),
                              _titleInfo(
                                  "Estimate",
                                  "${Jiffy.parseFromMillisecondsSinceEpoch(leg.estimatedDepartureTime ?? 0).format(pattern: "Hm")}-${Jiffy.parseFromMillisecondsSinceEpoch(leg.estimatedTimeOfArrival ?? 0).format(pattern: "Hm")}",
                                  Jiffy.parseFromMillisecondsSinceEpoch(
                                          leg.estimatedDepartureTime ?? 0)
                                      .format(pattern: "dd MMMyy")),
                              const Gap(15),
                              _titleInfo(
                                  "CTOT",
                                  Jiffy.parseFromMillisecondsSinceEpoch(
                                          leg.ctot ?? 0)
                                      .format(pattern: "Hm"),
                                  Jiffy.parseFromMillisecondsSinceEpoch(
                                          leg.ctot ?? 0)
                                      .format(pattern: "dd MMMyy")),
                            ]),
                          ]))
                    ]),
                  ])),
            ))
      ]),
    );
  }

  _titleInfo(String? title, String? info, String? secondary,
          {double sizetitle = 10,
          double sizeinfo = 16,
          Color color = Colors.blueGrey,
          bool bold = false}) =>
      Column(
        children: [
          Text(
            title ?? "---",
            style: TextStyle(color: Colors.grey, fontSize: sizetitle),
          ),
          Text(info ?? "---",
              style: TextStyle(
                  color: color,
                  fontSize: sizeinfo,
                  fontWeight: bold ? FontWeight.bold : null)),
          if (secondary != null)
            Text(secondary == "" ? "---" : secondary,
                style: TextStyle(
                    color: color,
                    fontSize: sizeinfo - 4,
                    fontWeight: bold ? FontWeight.w700 : null)),
        ],
      );

  kilo2ton(int? k, {bool ceil = true}) {
    if (k == null) return null;
    const double conversionFactor = 0.001;
    double tons = k * conversionFactor;
    if (ceil) {
      return "${double.parse(((tons * 10).ceilToDouble() / 10).toStringAsFixed(1))}T";
    } else {
      return "${double.parse(((tons * 10).floorToDouble() / 10).toStringAsFixed(1))}T";
    }
  }
}
