import 'package:test_sa/models/base.dart';
import 'package:test_sa/models/new_models/building.dart';

class Site extends Base {
  Site({
    this.id,
    this.custName,
    this.buildings,
  }) : super(identifier: id.toString(), name: custName);

  Site.fromJson(dynamic json) {
    id = json['id'];
    identifier = id.toString();
    custName = json['custName'];
    name = custName;
    if (json['buildings'] != null) {
      buildings = [];
      json['buildings'].forEach((v) {
        buildings.add(Building.fromJson(v));
      });
    }
  }

  num id;
  String custName;
  List<Building> buildings;

  Site copyWith({
    num id,
    String custName,
    List<Building> buildings,
  }) =>
      Site(
        id: id ?? this.id,
        custName: custName ?? this.custName,
        buildings: buildings ?? this.buildings,
      );

  Map<String, dynamic> toJson({bool addBuildings = true}) {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['custName'] = custName;
    if (addBuildings) {
      if (buildings != null) {
        map['buildings'] = buildings.map((v) => v.toJson()).toList();
      }
    } else {
      map['buildings'] = [];
    }

    return map;
  }
}
