class Colonia {
  const Colonia({
    required this.id,
    required this.nombre,
    this.routeId,
    this.horarioEstimado,
    this.turno,
  });

  final String id;
  final String nombre;
  final String? routeId;
  final String? horarioEstimado;
  final String? turno;

  factory Colonia.fromJson(Map<String, dynamic> json) {
    final rawNombre = _pickString(<dynamic>[
      json['nombre'],
      json['colonia'],
      json['name'],
    ]);
    final rawRouteId = _pickString(<dynamic>[
      json['routeId'],
      json['route_id'],
    ]);
    final rawId = _pickString(<dynamic>[json['id'], rawRouteId, rawNombre]);

    return Colonia(
      id: rawId ?? '',
      nombre: rawNombre ?? rawId ?? '',
      routeId: rawRouteId,
      horarioEstimado: _pickString(<dynamic>[
        json['horario_estimado'],
        json['horarioEstimado'],
        json['schedule'],
      ]),
      turno: _pickString(<dynamic>[json['turno'], json['shift']]),
    );
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'id': id,
      'nombre': nombre,
      'routeId': routeId,
      'horario_estimado': horarioEstimado,
      'turno': turno,
    };
  }

  static String? _pickString(Iterable<dynamic> candidates) {
    for (final candidate in candidates) {
      if (candidate is String && candidate.isNotEmpty) {
        return candidate;
      }
      if (candidate != null && candidate.toString().isNotEmpty) {
        return candidate.toString();
      }
    }

    return null;
  }
}
