import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gap/gap.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:linear_progress_bar/linear_progress_bar.dart';
import 'package:tp5/core/utils.dart';
import 'package:tp5/csv/data.dart';
import 'package:tp5/roster/widgets/w_citypair.dart';
import 'package:tp5/roster/widgets/w_hour.dart';
import 'package:tp5/widgets/my_row.dart';

class WFlt extends ConsumerWidget {
  const WFlt(
      {super.key,
      required this.acleg,
      this.hide = const [],
      this.highlight = false});

  final List<String> hide;
  final bool highlight;
  final Acleg acleg;

  String get fltstate {
    if (acleg.flt_status == "Arrived" || acleg.flt_status == "Landed") {
      return "ARR";
    } else if (acleg.flt_status == "Inflight" ||
        acleg.flt_status == "Taxiout") {
      return "DEP";
    } else if (acleg.flt_status == "Delayed") {
      return "DEL";
    } else if (acleg.flt_status == "Sched") {
      return "SCD";
    } else {
      return "UNK";
    }
  }

  aclegicon() {
    if (fltstate == "DEP") {
      return Icons.flight_takeoff_rounded;
    } else if (fltstate == "ARR") {
      return Icons.flight_land_rounded;
    } else if (fltstate == "DEL") {
      return Icons.timer;
    } else if (fltstate == "SCD") {
      return Icons.check_rounded;
    } else {
      return Icons.device_unknown;
    }
  }

  Color aclegcolor() {
    if (fltstate == "DEP") {
      return Colors.black;
    } else if (fltstate == "ARR") {
      return Colors.black;
    } else if (fltstate == "DEL") {
      return Colors.grey[900]!;
    } else if (fltstate == "SCD") {
      return Colors.grey[900]!;
    } else {
      return Colors.yellow[900]!;
    }
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final now = ref.watch(clockProvider);
    return Card(
      color: aclegcolor(),
      // surfaceTintColor: (fltstate == "DEP") ? Colors.white : null,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(5.0), // Rounded corners
        side: BorderSide(
          color: highlight ? Colors.yellow : Colors.transparent, // Border color
          width: 2.0, // Border width
        ),
      ),
      elevation: 8.0,
      //margin: const EdgeInsets.symmetric(horizontal: 5.0, vertical: 4.0),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 2, vertical: 0),
            child: MyRow(children: [
              if (!hide.contains("leading")) _leading(),
              if (!hide.contains("timing")) _timing(),
              if (!hide.contains("flight")) _flight(),
              if (!hide.contains("actype")) _actype(),
              if (!hide.contains("booking")) _booking(),
              if (!hide.contains("delay")) _delay(),
            ]),
          ),
          if (fltstate == "DEP" && now.isBefore(acleg.jarr!))
            Row(children: [
              const Gap(60),
              Text(
                "departed\n${now.dateTime.difference(acleg.jdep!.dateTime).tohhmm} ago",
                style: const TextStyle(
                    fontSize: 9, letterSpacing: 1, color: Colors.grey),
              ),
              const Gap(5),
              Expanded(
                child: LinearProgressBar(
                  maxSteps: acleg.jarr!.dateTime
                      .difference(acleg.jdep!.dateTime)
                      .inMinutes,
                  progressType: LinearProgressBar
                      .progressTypeLinear, // Use Linear progress
                  currentStep:
                      now.dateTime.difference(acleg.jdep!.dateTime).inMinutes,
                  progressColor: Colors.cyan,
                  backgroundColor: Colors.grey[700],
                  borderRadius: BorderRadius.circular(10), //  NEW
                ),
              ),
              const Gap(5),
              Text(
                "arrives\nin ${acleg.jarr!.dateTime.difference(now.dateTime).tohhmm}",
                style: const TextStyle(
                    fontSize: 9, letterSpacing: 1, color: Colors.grey),
              ),
              const Gap(5)
            ]),
          const Gap(2),
        ],
      ),
    );
  }

  _leading() => SizedBox(
        width: 50,
        child: Padding(
          padding: const EdgeInsets.all(4.0),
          child: Column(children: [
            Icon(aclegicon()),
            Text(
              acleg.flt_status,
              style: const TextStyle(fontSize: 9),
            )
          ]),
        ),
      );

  _flight() => SizedBox(
        width: 100,
        child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
          WCitypair(
            iata1: acleg.DEP_AP_ACTUAL ?? "---",
            iata2: acleg.ARR_AP_SCHED ?? "---",
            size: 14,
            color:
                (acleg.ARR_AP_ACTUAL == acleg.ARR_AP_SCHED) ? null : Colors.red,
          ),
          MyRow(
            children: [
              Text(
                  "${acleg.FN_CARRIER}${acleg.FN_NUMBER ?? "---"}${acleg.FN_SUFFIX ?? ""}",
                  style: const TextStyle(color: Colors.white70, fontSize: 12)),
              const Gap(10),
              if (acleg.ARR_AP_ACTUAL != acleg.ARR_AP_SCHED &&
                  acleg.DEP_AP_ACTUAL != acleg.ARR_AP_ACTUAL &&
                  acleg.blocks[1] != null)
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Text("Diverted to",
                        style: TextStyle(color: Colors.red, fontSize: 9)),
                    Text(acleg.ARR_AP_ACTUAL ?? "---",
                        style: const TextStyle(
                            color: Colors.yellow,
                            fontSize: 12,
                            fontWeight: FontWeight.w600)),
                  ],
                ),
              if (acleg.ARR_AP_ACTUAL != acleg.ARR_AP_SCHED &&
                  acleg.DEP_AP_ACTUAL == acleg.ARR_AP_ACTUAL)
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                        (acleg.blocks[1] != null)
                            ? "Flight back to"
                            : "Return to",
                        style: const TextStyle(color: Colors.red, fontSize: 9)),
                    Text(acleg.ARR_AP_ACTUAL ?? "---",
                        style: const TextStyle(
                            color: Colors.yellow,
                            fontSize: 12,
                            fontWeight: FontWeight.w600)),
                  ],
                ),
            ],
          ),
        ]),
      );

  _actype() => SizedBox(
        width: 60,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(acleg.AC_REGISTRATION ?? "-----",
                style: TextStyle(
                    color: Colors.yellow[800]!, fontWeight: FontWeight.w700)),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(acleg.AC_SUBTYPE ?? "---",
                    style: const TextStyle(
                        fontSize: 12,
                        color: Colors.grey,
                        fontWeight: FontWeight.w500)),
                Text(acleg.AC_OWNER ?? "---",
                    style: TextStyle(
                        fontSize: 12,
                        color: Colors.grey[400]!,
                        fontWeight: FontWeight.w500)),
              ],
            )
          ],
        ),
      );
  _booking() => SizedBox(
        width: 60,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              acleg.pax_booked ?? "---",
              style: GoogleFonts.robotoMono(
                  fontSize: 11, fontWeight: FontWeight.w300),
            ),
            Text(
              acleg.AC_VERSION ?? "---",
              style: GoogleFonts.robotoMono(
                  color: Colors.grey, fontSize: 9, fontWeight: FontWeight.w300),
            ),
          ],
        ),
      );
  _timing() => SizedBox(
        width: 80,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              acleg.jdep!.format(pattern: "dd MMM 'yy"),
              style: const TextStyle(fontSize: 11, color: Colors.grey),
            ),
            Row(
              children: [
                WHour(jiffy: acleg.jdep!, size: 13),
                const Gap(10),
                WHour(jiffy: acleg.jarr!, size: 13)
              ],
            ),
            if (acleg.jdep!.isAfter(acleg.jdepsched!.add(minutes: 15)))
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    "Delay ",
                    style: GoogleFonts.robotoMono(
                        fontSize: 10,
                        fontWeight: FontWeight.w400,
                        color: Colors.red[600]),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(right: 8),
                    child: Text(
                      acleg.jdep!.dateTime
                          .difference(acleg.jdepsched!.dateTime)
                          .tohhmm,
                      style: GoogleFonts.robotoMono(
                          fontSize: 10,
                          fontWeight: FontWeight.w400,
                          color: Colors.red[200]),
                    ),
                  )
                ],
              ),
            if (acleg.slot != null && acleg.slot!.year > 2000)
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    "CTOT ",
                    style: GoogleFonts.robotoMono(
                        fontSize: 10,
                        fontWeight: FontWeight.w600,
                        color: Colors.amber),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(right: 8),
                    child: Text(
                      acleg.slot!.Hm,
                      style: GoogleFonts.robotoMono(
                          fontSize: 10,
                          fontWeight: FontWeight.w400,
                          color: Colors.amber),
                    ),
                  )
                ],
              ),
          ],
        ),
      );

  _delay() => SizedBox(
      width: 60,
      child: Column(
        children: [
          if (acleg.dla.isNotEmpty) ...[
            const Text("DLA",
                style: TextStyle(fontSize: 10, color: Colors.grey)),
            ...acleg.dla.map((e) => Text(
                  "${e[0]} = ${e[1] is Duration ? ((e[1] as Duration).tohhmm) : ""}",
                  style: const TextStyle(fontSize: 10),
                ))
          ]
        ],
      ));
}
