import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:jiffy/jiffy.dart';
import 'package:tp5/core/utils.dart';
import 'package:tp5/lido/model/Lidoapi_list.dart';
import 'package:tp5/roster/widgets/w_citypair.dart';
import 'package:tp5/roster/widgets/w_fnum.dart';
import 'package:tp5/roster/widgets/w_hour.dart';
import 'package:tp5/widgets/my_row.dart';

class WLidoflt extends StatelessWidget {
  const WLidoflt(
      {super.key, required this.lidoapilist, this.onTap, this.iata = true});
  final LidoapiList lidoapilist;
  final bool iata;

  final dynamic onTap;
  _estDepTime() => Column(
        children: [
          Text(
            Jiffy.parseFromMillisecondsSinceEpoch(
                    lidoapilist.leg?.estimatedDepartureTime ?? 0,
                    isUtc: true)
                .format(pattern: "ddMMMyy"),
            style: const TextStyle(fontSize: 12, color: Colors.grey),
          ),
          WHour(
              jiffy: Jiffy.parseFromMillisecondsSinceEpoch(
                  lidoapilist.leg?.estimatedDepartureTime ?? 0,
                  isUtc: true)),
        ],
      );

  _fnumCitypair() => Row(
        children: [
          WFnum(
              al: iata
                  ? (lidoapilist.leg?.commercialAirlineIata ?? "++")
                  : (lidoapilist.leg?.commercialAirlineIcao ?? "+++"),
              fnum: lidoapilist.leg?.flightNumber ?? "---"),
          const Gap(10),
          WCitypair(
            iata1: iata
                ? (lidoapilist.leg?.departureAirport ?? "---")
                : (lidoapilist.leg?.departureAirportIcao ?? "----"),
            iata2: iata
                ? (lidoapilist.leg?.destinationAirport ?? "---")
                : (lidoapilist.leg?.destinationAirportIcao ?? "----"),
            size: 14,
          )
        ],
      );

  _aircraft() => Row(children: [
        Text(lidoapilist.leg?.aircraftDetails?.aircraftIcaoType ?? "---",
            style: const TextStyle(
                color: Colors.grey, fontWeight: FontWeight.w600)),
        const Gap(10),
        Text(
          lidoapilist.leg?.aircraftDetails?.aircraftRegistration ?? "---",
          style: const TextStyle(fontSize: 14, color: Colors.yellow),
        ),
      ]);

  _schedCtot() => Row(
        children: [
          if (lidoapilist.leg?.estimatedDepartureTime !=
              lidoapilist.leg?.scheduledDepartureTime) ...[
            const Text(
              "Sched",
              style: TextStyle(color: Colors.white38, fontSize: 10),
            ),
            const Gap(5),
            WHour(
              jiffy: Jiffy.parseFromMillisecondsSinceEpoch(
                  lidoapilist.leg?.scheduledDepartureTime ?? 0,
                  isUtc: true),
              size: 12,
              color: Colors.grey[500],
            ),
            const Gap(10)
          ],
          if (lidoapilist.leg?.ctot != null) ...[
            const Text(
              "CTOT ",
              style: TextStyle(color: Colors.white24, fontSize: 10),
            ),
            WHour(
              jiffy: Jiffy.parseFromMillisecondsSinceEpoch(
                  lidoapilist.leg?.ctot ?? 0,
                  isUtc: true),
              size: 12,
              color: Colors.orange,
            )
          ]
        ],
      );

  _fuel() => Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
        Text(
          "ZFW ${kilo2ton(lidoapilist.leg?.weight?.plannedZfw)}",
          style: const TextStyle(color: Colors.grey),
        ),
        Column(
          children: [
            Text(
              "Fuel  ${kilo2ton(lidoapilist.leg?.fuel?.blockFuel)}",
              style: const TextStyle(
                  color: Colors.cyan, fontWeight: FontWeight.w700),
            ),
            Text(
              "Max xtra +${kilo2ton(lidoapilist.leg?.fuel?.possibleExtra, ceil: true)}",
              style: const TextStyle(color: Colors.amber, fontSize: 10),
            ),
          ],
        ),
        Text(
          "Trip ${kilo2ton(lidoapilist.leg?.fuel?.tripFuel)} / ${Duration(minutes: lidoapilist.leg?.fuel?.totalTripTimeMinutes ?? 0).tohhmm}",
          style: const TextStyle(
              color: Colors.green, fontWeight: FontWeight.w600, fontSize: 10),
        ),
        // Text("Trip  ${kilo2ton(lidoapilist.leg?.fuel?.tripFuel)}"),
      ]);
  _ofpVer() => Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
        Text(
          "OFP ${lidoapilist.leg?.ofpNumber}",
          style: const TextStyle(
              color: Colors.white54, fontWeight: FontWeight.w600),
        ),
        Text(
          // ignore: unnecessary_string_interpolations
          "${Jiffy.parseFromMillisecondsSinceEpoch(lidoapilist.lastGenerated ?? 0, isUtc: true).format(pattern: "ddMMMyy")}",
          style: const TextStyle(color: Colors.white, fontSize: 12),
        ),
        Text(
          // ignore: unnecessary_string_interpolations
          "${Jiffy.parseFromMillisecondsSinceEpoch(lidoapilist.lastGenerated ?? 0, isUtc: true).format(pattern: "HH:mm")}",
          style: const TextStyle(color: Colors.white70, fontSize: 14),
        ),
      ]);
  _crew() => Row(children: [
        const Text("Crew: ",
            style: TextStyle(fontSize: 10, color: Colors.white30)),
        if (lidoapilist.leg?.crewMembers.isNotEmpty ?? false)
          Text(
            "${lidoapilist.leg?.crewMembers.map((e) => e.name?.capitalize())}",
            style: const TextStyle(
                color: Colors.white60,
                fontWeight: FontWeight.w400,
                fontSize: 12),
          )
        else
          const Text("-----"),
      ]);
  _fuelOrder() => Row(
        children: [
          const Text("Fuel order: ",
              style: TextStyle(fontSize: 10, color: Colors.white30)),
          Text(
            "${lidoapilist.latestFuelOrder["fuelOrder"]}",
            style: const TextStyle(
                color: Colors.white60,
                fontWeight: FontWeight.w400,
                fontSize: 12),
          ),
          Text(
            " by ${lidoapilist.latestFuelOrder["source"]}",
            style: const TextStyle(
                color: Colors.white54,
                fontWeight: FontWeight.w300,
                fontSize: 12),
          ),
        ],
      );
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: 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: MyRow(crossAxisAlignment: CrossAxisAlignment.start, children: [
            _estDepTime(),
            const Gap(20),
            Column(
              children: [
                Row(children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _fnumCitypair(),
                      _aircraft(),
                      _schedCtot(),
                    ],
                  ),
                  const Gap(20),
                  _fuel(),
                  const Gap(20),
                  _ofpVer(),
                ]),
                const Gap(5),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    _crew(),
                    if (lidoapilist.latestFuelOrder != null) _fuelOrder(),
                  ],
                ),
              ],
            )
          ]),
        ),
      ),
    );
  }

  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";
    }
  }
}
