import 'package:collection/collection.dart';
import 'package:jiffy/jiffy.dart';

class CrewlistLeg {
  List data;
  int get order => int.parse(data[0]);
  String get al => data[1].split(" ")[0];
  String get fnum => data[1].split(" ")[1];
  String get datedep => data[2]; //ddMMMyy
  Jiffy get jdate => Jiffy.parse(datedep, pattern: "ddMMMyy", isUtc: true);
  String get date => jdate.format(pattern: "yyyy-MM-dd");

  String get dep => data[3];
  String get hdep => data[4];
  Jiffy get jdep =>
      Jiffy.parse("$datedep ${hdep[0]}${hdep[1]}:${hdep[2]}${hdep[3]}",
          pattern: "ddMMMyy HH:mm", isUtc: true);

  String get hdes => data[5];
  String get des => data[6];
  Jiffy get jdes =>
      Jiffy.parse("$datedep ${hdes[0]}${hdes[1]}:${hdes[2]}${hdep[3]}",
              pattern: "ddMMMyy HH:mm", isUtc: true)
          .add(days: hdep.compareTo(hdes) <= 0 ? 0 : 1);

  String get crew => data[7];
  List<String> get cockpit => (crew.split("|")[0].split(","))
      .map((e) => e.trim())
      .whereNot((e) => e == "")
      .toList();
  List<String> get cabin => (crew.split("|")[1].split(","))
      .map((e) => e.trim())
      .whereNot((e) => e == "")
      .toList();

  List<String> get dh => (crew.split("|")[2].split(","))
      .map((e) => e.trim())
      .whereNot((e) => e == "")
      .toList();

  List<Crew>? get crewlist =>
      details ? (data[8]!).map<Crew>((x) => Crew(x)).toList() : null;
  bool get details => data.length >= 9;

  CrewlistLeg(this.data);
}

class Crew {
  List data;
  Crew(this.data);
  String get tlc => data[0];
  String get position => data[1];
  String get senior => data[2];
  String get base => data[3];
  String get name => data[5];
}
