import 'package:collection/collection.dart';
import 'package:flutter/services.dart';
//import 'package:csv/csv.dart';

class Airport {
  String code;
  String timezoneid;
  String name;
  String citycode;
  String countryid;
  String location;
  String elevation;
  String url;
  String icao;
  String city;
  String county;
  String state;
  Airport(
      this.code,
      this.timezoneid,
      this.name,
      this.citycode,
      this.countryid,
      this.location,
      this.elevation,
      this.url,
      this.icao,
      this.city,
      this.county,
      this.state);
}

class Airports {
  // static final Airports instance = Airports._();
  // Airports._();

  // List lines = [];
  // List titles = [];
  static Airport? find(String code) =>
      Airports.list.firstWhereOrNull((e) => e.code == code);

  static Future<void> init() async {
    String x = await rootBundle.loadString('assets/csv/airports.csv');
    // lines = x.replaceAll('"', "").split("\n");
    // titles = lines[0].split(",");

    // lines.removeAt(0);

    // lines = lines
    //     .map((e) => Map.fromIterables(
    //         titles,
    //         e.split(",").length == titles.length
    //             ? e.split(",")
    //             : List.generate(titles.length, (w) => "")))
    //     .toList();

    // List<List<dynamic>> listData = const CsvToListConverter().convert(x);
    // for (var ap in listData) {
    //   list.add(Airport(ap[0], ap[1], ap[2], ap[3], ap[4], ap[5],
    //       ap[6].toString(), ap[7], ap[8], ap[9], ap[10], ap[11]));
    // }

    List<List<dynamic>> listData =
        x.split("\n").map((e) => e.split(",").toList()).toList();
    for (var ap in listData) {
      list.add(Airport(ap[0], ap[1], ap[2], ap[3], ap[4], ap[5],
          ap[6].toString(), ap[7], ap[8], ap[9], ap[10], ap[11]));
    }

    print("Airports: loaded ${list.length} Airports");
  }

  static List<Airport> list = [];
}
