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

class DeviceTransferInfo{
  String userId;
  String comment;
  Hospital client;
  Department department;
  String workingHours;
  String travelingHours;
  String userName;
  String signature;
  Lookup status;

  DeviceTransferInfo({
    this.userId,
    this.comment,
    this.department,
    this.client,
    this.userName,
    this.travelingHours,
    this.workingHours,
    this.signature,
    this.status,
  });

  Map<String,String>toJson(bool isSender){
    Map<String,String> body = {};
    final baseKey = isSender ? "sender_" : "receiver_";

    if(comment != null && comment.isNotEmpty) body["${baseKey}comment"] = comment;
    if(workingHours != null && workingHours.isNotEmpty) body["${baseKey}working_hours"] = workingHours;
    if(travelingHours != null && travelingHours.isNotEmpty) body["${baseKey}travel_hours"] = comment;
    if(status != null) body["${baseKey}status"] = status.id.toString();
    if(signature != null && signature.isNotEmpty) body["${baseKey}image"] = signature;
    return body;
  }

  bool validate(){
    if(client == null) return false;
    if(department == null) return false;
    return true;
  }

  fromDetails(DeviceTransferInfo old,{bool withSignature = true}){
    userId = old.userId;
    userName = old.userName;
    client = Hospital.fromHospital(old.client);
    department = Department.fromDepartment(old.department);
    workingHours = old.workingHours;
    travelingHours = old.travelingHours;
    comment = old.comment;
    if(withSignature) signature = old.signature;
    status = old.status;
  }

  factory DeviceTransferInfo.fromJson(Map<String,dynamic> parsedJson,String key){
    return DeviceTransferInfo(
      workingHours: parsedJson["${key}working_hours"],
      travelingHours: parsedJson["${key}travel_hours"],
      userName: parsedJson["${key}name"],
      signature: parsedJson["${key}image"],
      userId: parsedJson["${key}id"],
      comment: parsedJson["${key}comment"],
      client: Hospital(
        id: parsedJson["${key}SiteId"],
        name: parsedJson["${key}SiteName"]
      ),
      department: Department(
        id: parsedJson["${key}DepartmentId"],
        name: parsedJson["${key}DepartmentName"],
      ),
      status: Lookup(
          id: parsedJson["${key}status"],
          name: parsedJson["${key}status"],
      ),
    );
  }
}

