import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:tp5/roster/models/duty.dart';
import 'package:tp5/roster/widgets/w_airport.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';

class WDuty extends StatelessWidget {
  const WDuty({super.key, required this.duty, this.date, this.onTap});
  final Duty duty;
  final String? date;
  final dynamic onTap;

  Widget _checkDutyType() {
    final String widgetdate = date ?? duty.date;
    if (duty.type == "flight") {
      return Card(
        color: Colors.grey[800],
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(6.0),
        ),
        elevation: 8.0,
        margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
        child: SizedBox(
          child: ListTile(
            contentPadding:
                const EdgeInsets.symmetric(horizontal: 20.0, vertical: 0.0),
            leading: Icon(duty.icon),
            title: Row(
              children: [
                WCitypair(iata1: duty.data["dep"], iata2: duty.data["des"]),
                if (duty.data["req"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  const Text("Requested",
                      style: TextStyle(color: Colors.white38, fontSize: 12))
                ],
              ],
            ),
            subtitle: Row(
              children: [
                WFnum(al: duty.data["al"], fnum: duty.data["fnum"]),
                const Gap(20),
                if (duty.data["actype"] != null)
                  Text("${duty.data["actype"] ?? ""}",
                      style: const TextStyle(
                          color: Colors.yellow, fontWeight: FontWeight.w700)),
                if (duty.data["remark"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  _text(duty.data["remark"])
                ],
              ],
            ),
            trailing: Row(mainAxisSize: MainAxisSize.min, children: [
              WHour(jiffy: duty.start!, hide: duty.date != widgetdate),
              const Gap(10),
              WHour(
                  jiffy: duty.end!,
                  color: duty.end!.format(pattern: "yyyy-MM-dd") == widgetdate
                      ? null
                      : Colors.black)
            ]),
          ),
        ),
      );
    } else if (duty.type == "dhflight") {
      return Card(
        color: Colors.blueGrey[800],
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(6.0),
        ),
        elevation: 8.0,
        margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
        child: SizedBox(
          child: ListTile(
            contentPadding:
                const EdgeInsets.symmetric(horizontal: 20.0, vertical: 0.0),
            leading: Icon(duty.icon),
            title: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(2),
                  color: Colors.blueGrey[900],
                  child: const Text("DH",
                      style:
                          TextStyle(fontWeight: FontWeight.w400, fontSize: 12)),
                ),
                const Gap(10),
                WCitypair(iata1: duty.data["dep"], iata2: duty.data["des"]),
                if (duty.data["req"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  const Text("Requested",
                      style: TextStyle(color: Colors.white38, fontSize: 12))
                ],
              ],
            ),
            subtitle: Row(
              children: [
                WFnum(al: duty.data["al"], fnum: duty.data["fnum"]),
                if (duty.data["remark"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  _text(duty.data["remark"])
                ],
              ],
            ),
            trailing: Row(mainAxisSize: MainAxisSize.min, children: [
              WHour(jiffy: duty.start!, hide: duty.date != widgetdate),
              const Gap(10),
              WHour(
                  jiffy: duty.end!,
                  color: duty.end!.format(pattern: "yyyy-MM-dd") == widgetdate
                      ? null
                      : Colors.black)
            ]),
          ),
        ),
      );
    } else if (duty.type == "dhlimo") {
      return Card(
        color: Colors.blueGrey[800],
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(6.0),
        ),
        elevation: 8.0,
        margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
        child: SizedBox(
          child: ListTile(
            contentPadding:
                const EdgeInsets.symmetric(horizontal: 20.0, vertical: 0.0),
            leading: Icon(duty.icon),
            title: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(2),
                  color: Colors.blueGrey[900],
                  child: const Text("DH",
                      style:
                          TextStyle(fontWeight: FontWeight.w400, fontSize: 12)),
                ),
                const Gap(10),
                WCitypair(iata1: duty.data["dep"], iata2: duty.data["des"]),
                if (duty.data["req"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  const Text("Requested",
                      style: TextStyle(color: Colors.white38, fontSize: 12))
                ],
              ],
            ),
            subtitle: Row(
              children: [
                const WFnum(al: "", fnum: "LIMO"),
                if (duty.data["remark"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  _text(duty.data["remark"])
                ],
              ],
            ),
            trailing: Row(mainAxisSize: MainAxisSize.min, children: [
              WHour(jiffy: duty.start!, hide: duty.date != widgetdate),
              const Gap(10),
              WHour(
                  jiffy: duty.end!,
                  color: duty.end!.format(pattern: "yyyy-MM-dd") == widgetdate
                      ? null
                      : Colors.black)
            ]),
          ),
        ),
      );
    } else if (duty.type == "wholeday" ||
        (duty.data["hdep"] == "0100" && duty.data["hdes"] == "2300") ||
        (duty.data["hdep"] == "0001" && duty.data["hdes"] == "2300")) {
      return Card(
        color: Colors.white10,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(6.0),
        ),
        elevation: 2.0,
        margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
        child: SizedBox(
          child: ListTile(
            contentPadding:
                const EdgeInsets.symmetric(horizontal: 20.0, vertical: 0.0),
            title: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: [
                Text(
                  duty.data["label"],
                  style: TextStyle(
                      fontSize: (duty.data["label"]).toString().length < 16
                          ? 24
                          : 12),
                ),
                const Gap(10),
                WAirport(
                  iata: duty.data["dep"],
                  size: 12,
                ),
                const Gap(5),
                if (duty.data["req"] != null) ...[
                  const Gap(10),
                  const Text("Requested",
                      style: TextStyle(color: Colors.white38, fontSize: 12))
                ],
              ],
            ),
            subtitle: Row(
              children: [
                if (duty.data["remark"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  _text(duty.data["remark"])
                ],
              ],
            ),
          ),
        ),
      );
    } else if (duty.type == "ground") {
      return Card(
        color: Colors.blueGrey[700],
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(6.0),
        ),
        elevation: 8.0,
        margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
        child: SizedBox(
          child: ListTile(
            contentPadding:
                const EdgeInsets.symmetric(horizontal: 20.0, vertical: 0.0),
            leading: Icon(duty.icon),
            title: Row(
              children: [
                Text(
                  duty.data["label"],
                  style: TextStyle(
                      fontSize: (duty.data["label"]).toString().length < 16
                          ? 24
                          : 12),
                ),
                const Gap(10),
                WAirport(
                  iata: duty.data["dep"],
                  size: 12,
                ),
                const Gap(5),
                if (duty.data["req"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  const Text("Requested",
                      style: TextStyle(color: Colors.white38, fontSize: 12))
                ],
              ],
            ),
            subtitle: Row(
              children: [
                if (duty.data["actype"] != null) ...[
                  const Text("SIMU"),
                  const Gap(10),
                  Text("${duty.data["actype"] ?? ""}",
                      style: const TextStyle(
                          color: Colors.yellow, fontWeight: FontWeight.w700))
                ],
                if (duty.data["remark"] != null) ...[
                  const Gap(10),
                  Expanded(child: Container()),
                  _text(duty.data["remark"])
                ],
              ],
            ),
            trailing: Row(mainAxisSize: MainAxisSize.min, children: [
              WHour(jiffy: duty.start!, hide: duty.date != widgetdate),
              const Gap(10),
              WHour(
                  jiffy: duty.end!,
                  color: duty.end!.format(pattern: "yyyy-MM-dd") == widgetdate
                      ? null
                      : Colors.black)
            ]),
          ),
        ),
      );
    } else if (duty.type == "checkin") {
      return Row(mainAxisAlignment: MainAxisAlignment.start, children: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 5),
          color: Colors.white10,
          child: Text(
            ">>> C/I   ${duty.data["station"]}   ${duty.data["time"]}",
            style: const TextStyle(letterSpacing: 2, color: Colors.white54),
          ),
        )
      ]);
    } else if (duty.type == "checkout") {
      return Row(mainAxisAlignment: MainAxisAlignment.end, children: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 5),
          color: Colors.white10,
          child: Text(
            "${duty.data["time"]}   ${duty.data["station"]}   C/O <<<",
            style: const TextStyle(letterSpacing: 2, color: Colors.white54),
          ),
        )
      ]);
    } else if (duty.type == "credit") {
      return Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
        // const Divider(),
        Text(
          "credit: [${duty.data["credit"]}]",
          style: TextStyle(
              fontSize: 12, letterSpacing: 2, color: Colors.blue[400]),
        )
      ]);
    } else if (duty.type == "changed") {
      return Card(
        color: Colors.orange.shade800,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5.0),
        ),
        elevation: 2.0,
        margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
        child: const SizedBox(
          child: ListTile(
            contentPadding:
                EdgeInsets.symmetric(horizontal: 20.0, vertical: 0.0),
            title: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.max,
                children: [
                  Text(
                    "CHANGED",
                    style: TextStyle(fontSize: 24),
                  ),
                ]),
          ),
        ),
      );
    } else if (duty.type == "unknown") {
      return Tooltip(
          message: "${duty.data["unknown"]}",
          child: Text(
              overflow: TextOverflow.fade,
              "${duty.data["unknown"]}",
              style: const TextStyle(fontSize: 10)));
    } else {
      return Tooltip(
          message: "${duty.data}",
          child: Text(
              overflow: TextOverflow.fade,
              "${duty.data}",
              style: const TextStyle(fontSize: 10)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: _checkDutyType(),
    );
  }

  Widget _text(String? msg) => Flexible(
      child: Tooltip(
          message: msg ?? "",
          child: Text(msg ?? "",
              overflow: TextOverflow.ellipsis,
              style: const TextStyle(color: Colors.white38, fontSize: 12))));
}
