import 'package:test_sa/models/hospital.dart';
import 'package:test_sa/models/lookup.dart';

import 'model.dart';

class Device {
  int id;
  String serialNumber;
  String number;
  String assetName;
  String destBuildingName;
  int destBuildingId;
  String destDepartmentName;
  int destDepartmentId;
  String destRoom;
  String destFloor;
  int destFloorId;
  String destSiteName;

  // String brand;
  // String model;
  ModelDefinition modelDefinition;
  Hospital hospital;

  Device({
    this.id,
    this.serialNumber,
    this.number,
    this.assetName,
    this.hospital,
    this.destBuildingName,
    this.destDepartmentName,
    this.destRoom,
    this.destFloor,
    this.destSiteName,
    // this.brand,
    // this.model,
    this.modelDefinition,
    this.destFloorId,
    this.destDepartmentId,
    this.destBuildingId,
  });

  factory Device.fromJson(Map<String, dynamic> parsedJson, {bool startKeyWithDest = true}) {
    return Device(
        id: parsedJson["id"],
        serialNumber: parsedJson["assetSerialNo"],
        number: parsedJson["assetNumber"],
        assetName: parsedJson["assetName"],
        modelDefinition: ModelDefinition.fromJson(parsedJson["modelDefinition"]),
        hospital: Hospital.fromJson(parsedJson["site"]),
        destBuildingName: startKeyWithDest ? parsedJson["destBuildingName"] : parsedJson['building']['name'],
        destBuildingId: startKeyWithDest ? parsedJson["destBuildingName"] : parsedJson['building']['id'],
        destDepartmentName: startKeyWithDest ? parsedJson["destDepartmentName"] : parsedJson['department']['departmentName'],
        destDepartmentId: startKeyWithDest ? parsedJson["destDepartmentName"] : parsedJson['department']['id'],
        destRoom: startKeyWithDest ? parsedJson["destRoom"] : parsedJson['room'],
        destFloor: startKeyWithDest ? parsedJson["destFloor"] : parsedJson['floor']['name'],
        destFloorId: startKeyWithDest ? null : parsedJson['floor']['id'],
        destSiteName: parsedJson['destSiteName']
        // parsedJson["modelDefinition"] == null ? "" :
        // parsedJson["modelDefinition"]["manufacturerName"],
        // model: parsedJson["modelDefinition"] == null ? "" :
        // parsedJson["modelDefinition"]["modelName"],
        );
  }

  factory Device.fromDevice(Device device) {
    return Device(
      id: device.id,
      serialNumber: device.serialNumber,
      number: device.number,
      // brand: device.brand,
      // model: device.model,
    );
  }

  Map<String, dynamic> toMap(Lookup assetType) {
    return {
      'id': id,
      'assetSerialNo': serialNumber,
      'assetNumber': number,
      'assetName': assetName,
      'modelDefinition': modelDefinition.toJson(),
      'site': hospital.toMap(),
      'AssetType': assetType.toMap(),
      "supplier": {"id": 0, "suppliername": "string"},
    };
  }
}
