import 'package:test_sa/extensions/enum_extensions.dart';
import 'package:test_sa/models/enums/recurrent_task_inspection_data_type.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/timer_model.dart';
import 'package:test_sa/modules/cleaner_module/widgets/cleaner_checklist_widget.dart';

class RecurrentWo {
  RecurrentWoData? recurrentWoData;
  String? message;
  String? title;
  String? innerMessage;
  int? responseCode;
  bool? isSuccess;

  RecurrentWo({this.recurrentWoData, this.message, this.title, this.innerMessage, this.responseCode, this.isSuccess});

  RecurrentWo.fromJson(Map<String, dynamic> json) {
    recurrentWoData = json['data'] != null ? RecurrentWoData.fromJson(json['data']) : null;
    message = json['message'];
    title = json['title'];
    innerMessage = json['innerMessage'];
    responseCode = json['responseCode'];
    isSuccess = json['isSuccess'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (recurrentWoData != null) {
      data['data'] = recurrentWoData!.toJson();
    }
    data['message'] = message;
    data['title'] = title;
    data['innerMessage'] = innerMessage;
    data['responseCode'] = responseCode;
    data['isSuccess'] = isSuccess;
    return data;
  }
}

class RecurrentWoData {
  int? id;
  String? title;
  String? taskNo;
  Engineer? engineer;
  String? scheduleDate;
  Status? status;
  Site? site;
  PlanRecurrent? planRecurrent;
  Lookup? building;
  Lookup? floor;
  Lookup? department;
  Lookup? room;
  List<PlanRecurrentMedicalTaskRooms>? planRecurrentMedicalTaskRooms;
  List<PlanRecurrentTaskTimers>? planRecurrentTaskTimers;
  List<ChecklistItem>? planRecurrentTaskCheckLists;
  TimerModel? recurrentWoTimerModel = TimerModel();
  double? totalWorkingHours = 0.0;
  List<TimerModel>? timerModelList = [];
  String? comment;
  TimerModel? recurrentWoTimePicker;
  String? imageRoom;

  RecurrentWoData(
      {this.id,
      this.title,
      this.taskNo,
      this.engineer,
      this.scheduleDate,
      this.status,
      this.site,
      this.planRecurrent,
      this.building,
      this.recurrentWoTimerModel,
      this.floor,
      this.department,
      this.room,
      this.planRecurrentMedicalTaskRooms,
      this.planRecurrentTaskTimers,
      this.planRecurrentTaskCheckLists,
      this.timerModelList,
      this.recurrentWoTimePicker,
      this.comment,
      this.totalWorkingHours,
      this.imageRoom});

  RecurrentWoData.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    title = json['title'];
    taskNo = json['taskNo'];
    engineer = json['engineer'] != null ? new Engineer.fromJson(json['engineer']) : null;
    scheduleDate = json['scheduleDate'];
    status = json['status'] != null ? Status.fromJson(json['status']) : null;
    planRecurrent = json['status'] != null ? PlanRecurrent.fromJson(json['planRecurrent']) : null;
    site = json['site'] != null ? Site.fromJson(json['site']) : null;
    building = json["building"] == null ? null : Lookup.fromJson(json["building"]);
    floor = json["floor"] == null ? null : Lookup.fromJson(json["floor"]);
    department = json["department"] == null ? null : Lookup.fromJson(json["department"]);
    room = json["room"] == null ? null : Lookup.fromJson(json["room"]);
    comment = json["comment"];
    imageRoom = json["imageRoom"];
    if (json['planRecurrentMedicalTaskRooms'] != null) {
      planRecurrentMedicalTaskRooms = <PlanRecurrentMedicalTaskRooms>[];
      json['planRecurrentMedicalTaskRooms'].forEach((v) {
        planRecurrentMedicalTaskRooms!.add(PlanRecurrentMedicalTaskRooms.fromJson(v));
      });
    }

    if (json['planRecurrentTaskTimers'] != null) {
      planRecurrentTaskTimers = <PlanRecurrentTaskTimers>[];
      json['planRecurrentTaskTimers'].forEach((v) {
        planRecurrentTaskTimers?.add(PlanRecurrentTaskTimers.fromJson(v));
      });
      //TODO need to test this @waseem..
      totalWorkingHours = json['planRecurrentTaskTimers'].fold(0.0, (sum, item) => (sum ?? 0) + DateTime.parse(item['endDateTime']).difference(DateTime.parse(item['startDateTime'])).inSeconds) ?? 0;
    }

    if (json['planRecurrentTaskCheckLists'] != null) {
      planRecurrentTaskCheckLists = <ChecklistItem>[];
      json['planRecurrentTaskCheckLists'].forEach((v) {
        planRecurrentTaskCheckLists!.add(ChecklistItem(
          id: v['id'] ?? 0,
          title: v['instructionText'] ?? v['title'] ?? '',
          isChecked: v['isDone'] ?? v['isChecked'] ?? false,
          comment: v['comment'] ?? '',
          isMandatory: v['isRequired'] ?? v['isMandatory'] ?? false,
        ));
      });
    }
  }

  Map<String, dynamic> toJson({int? status = 0}) {
    final Map<String, dynamic> data = <String, dynamic>{};
    List taskRoomTabAttributesJson = [];
    if (planRecurrentMedicalTaskRooms != null) {
      taskRoomTabAttributesJson = planRecurrentMedicalTaskRooms
              ?.expand((room) => room.planRecurrentMedicalTaskRoomTabs ?? [])
              .expand((tab) => tab.planRecurrentMedicalTaskRoomTabAttributes ?? [])
              .map((attribute) => attribute.toJson())
              .toList() ??
          [];
    }
    data['id'] = id;
    data['statusValue'] = status;
    data['comment'] = comment;

    if (planRecurrentTaskTimers != null) {
      data['planRecurrentTaskTimers'] = planRecurrentTaskTimers!.map((v) => v.toJson()).toList();
    }

    if (planRecurrentTaskCheckLists != null) {
      data['planRecurrentTaskCheckLists'] = planRecurrentTaskCheckLists!
          .map((v) => {
                'id': v.id,
                'comment': v.commentController.text,
                'isDone': v.isChecked,
              })
          .toList();
    }

    if (imageRoom != null && imageRoom!.isNotEmpty) {
      data['imageRoom'] = imageRoom;
    }

    if (taskRoomTabAttributesJson.isNotEmpty) {
      data['attributes'] = taskRoomTabAttributesJson;
    } else {
      data['attributes'] = [];
    }

    return data;
  }
}

class Engineer {
  String? userId;
  String? userName;
  String? email;
  String? employeeId;
  int? languageId;

  Engineer({this.userId, this.userName, this.email, this.employeeId, this.languageId});

  Engineer.fromJson(Map<String, dynamic> json) {
    userId = json['userId'];
    userName = json['userName'];
    email = json['email'];
    employeeId = json['employeeId'];
    languageId = json['languageId'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['userId'] = userId;
    data['userName'] = userName;
    data['email'] = email;
    data['employeeId'] = employeeId;
    data['languageId'] = languageId;
    return data;
  }
}

class Status {
  int? id;
  String? name;
  int? value;

  Status({this.id, this.name, this.value});

  Status.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['name'] = name;
    data['value'] = value;
    return data;
  }
}

class Site {
  int? id;
  String? siteName;

  Site({this.id, this.siteName});

  Site.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    siteName = json['siteName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['id'] = id;
    data['siteName'] = siteName;
    return data;
  }
}

class PlanRecurrentMedicalTaskRooms {
  int? id;
  Room? room;
  List<PlanRecurrentMedicalTaskRoomTabs>? planRecurrentMedicalTaskRoomTabs;

  PlanRecurrentMedicalTaskRooms({this.id, this.room, this.planRecurrentMedicalTaskRoomTabs});

  PlanRecurrentMedicalTaskRooms.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    room = json['room'] != null ? Room.fromJson(json['room']) : null;
    if (json['planRecurrentMedicalTaskRoomTabs'] != null) {
      planRecurrentMedicalTaskRoomTabs = <PlanRecurrentMedicalTaskRoomTabs>[];
      json['planRecurrentMedicalTaskRoomTabs'].forEach((v) {
        planRecurrentMedicalTaskRoomTabs!.add(PlanRecurrentMedicalTaskRoomTabs.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['id'] = id;
    if (room != null) {
      data['room'] = room!.toJson();
    }
    if (planRecurrentMedicalTaskRoomTabs != null) {
      data['planRecurrentMedicalTaskRoomTabs'] = planRecurrentMedicalTaskRoomTabs!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Room {
  int? id;
  String? roomId;

  Room({this.id, this.roomId});

  Room.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    roomId = json['roomId'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['id'] = id;
    data['roomId'] = roomId;
    return data;
  }
}

class PlanRecurrentMedicalTaskRoomTabs {
  int? id;
  String? tabName;
  int? tabMedicalRoomId;
  List<PlanRecurrentMedicalTaskRoomTabAttributes>? planRecurrentMedicalTaskRoomTabAttributes;

  PlanRecurrentMedicalTaskRoomTabs({this.id, this.tabName, this.tabMedicalRoomId, this.planRecurrentMedicalTaskRoomTabAttributes});

  PlanRecurrentMedicalTaskRoomTabs.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    tabName = json['tabName'];
    tabMedicalRoomId = json['tabMedicalRoomId'];
    if (json['planRecurrentMedicalTaskRoomTabAttributes'] != null) {
      planRecurrentMedicalTaskRoomTabAttributes = <PlanRecurrentMedicalTaskRoomTabAttributes>[];
      json['planRecurrentMedicalTaskRoomTabAttributes'].forEach((v) {
        planRecurrentMedicalTaskRoomTabAttributes!.add(PlanRecurrentMedicalTaskRoomTabAttributes.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['id'] = id;
    data['tabName'] = tabName;
    data['tabMedicalRoomId'] = tabMedicalRoomId;
    if (planRecurrentMedicalTaskRoomTabAttributes != null) {
      data['planRecurrentMedicalTaskRoomTabAttributes'] = planRecurrentMedicalTaskRoomTabAttributes!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class PlanRecurrentMedicalTaskRoomTabAttributes {
  int? id;
  Attribute? attribute;
  dynamic attributeValue;

  PlanRecurrentMedicalTaskRoomTabAttributes({this.id, this.attribute, this.attributeValue});

  PlanRecurrentMedicalTaskRoomTabAttributes.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    attribute = json['attribute'] != null ? Attribute.fromJson(json['attribute']) : null;
    attributeValue = json['attributeValue'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attributeValue'] = attributeValue != null ? attributeValue.toString() : attributeValue;
    return data;
  }
}

class Attribute {
  String? name;
  String? type;
  String? key;
  RecurrentTaskInspectionDataTypeEnum? dataTypeEnum;

  Attribute({this.name, this.type, this.key, this.dataTypeEnum});

  Attribute.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    type = json['type'];
    dataTypeEnum = json['type'] == null ? null : (json['type'] as String).toRecurrentTaskInspectionDataTypeEnum();
    key = json['key'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['name'] = name;
    data['type'] = type;
    data['key'] = key;
    return data;
  }
}

class PlanRecurrentTaskTimers {
  int? id;
  String? startTime;
  String? endTime;
  dynamic workingHours;

  PlanRecurrentTaskTimers({this.id, this.startTime, this.endTime, this.workingHours});

  PlanRecurrentTaskTimers.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    startTime = json['startDateTime'];
    endTime = json['endDateTime'];
    workingHours = json['workingHour'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['startDateTime'] = startTime;
    data['endDateTime'] = endTime;
    data['workingHour'] = workingHours;
    return data;
  }
}

class PlanRecurrent {
  int? taskTypeId;
  Lookup? taskType;
  String? planNo;
  String? planName;
  int? planSequence;
  int? frequencyId;
  String? frequency;
  bool? autoConverstion;
  String? comment;
  String? scheduleDateFrom1;
  String? scheduleDateFrom2;
  Site? site;
  Lookup? building;
  Lookup? floor;
  Lookup? department;
  Lookup? room;

  // List<Null>? planRecurrentMedicalRooms;
  bool? friday;
  bool? saturday;
  bool? sunday;
  bool? monday;
  bool? tuesday;
  bool? wednesday;
  bool? thursday;

  PlanRecurrent(
      {this.taskTypeId,
      this.taskType,
      this.planNo,
      this.planName,
      this.planSequence,
      this.frequencyId,
      this.frequency,
      this.autoConverstion,
      this.comment,
      this.scheduleDateFrom1,
      this.scheduleDateFrom2,
      this.site,
      this.building,
      this.floor,
      this.department,
      this.room,
      // this.planRecurrentMedicalRooms,
      this.friday,
      this.saturday,
      this.sunday,
      this.monday,
      this.tuesday,
      this.wednesday,
      this.thursday});

  PlanRecurrent.fromJson(Map<String, dynamic> json) {
    taskTypeId = json['taskTypeId'];
    taskType = json['taskType'] != null ? new Lookup.fromJson(json['taskType']) : null;
    planNo = json['planNo'];
    planName = json['planName'];
    planSequence = json['planSequence'];
    frequencyId = json['frequencyId'];
    frequency = json['frequency'];
    autoConverstion = json['autoConverstion'];
    comment = json['comment'];
    scheduleDateFrom1 = json['scheduleDateFrom1'];
    scheduleDateFrom2 = json['scheduleDateFrom2'];
    site = json['site'] != null ? Site.fromJson(json['site']) : null;
    building = json["building"] == null ? null : Lookup.fromJson(json["building"]);
    floor = json["floor"] == null ? null : Lookup.fromJson(json["floor"]);
    department = json["department"] == null ? null : Lookup.fromJson(json["department"]);
    room = json["room"] == null ? null : Lookup.fromJson(json["room"]);
    // if (json['planRecurrentMedicalRooms'] != null) {
    //   planRecurrentMedicalRooms = <Null>[];
    //   json['planRecurrentMedicalRooms'].forEach((v) {
    //     planRecurrentMedicalRooms!.add(new Null.fromJson(v));
    //   });
    // }
    friday = json['friday'];
    saturday = json['saturday'];
    sunday = json['sunday'];
    monday = json['monday'];
    tuesday = json['tuesday'];
    wednesday = json['wednesday'];
    thursday = json['thursday'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['taskTypeId'] = this.taskTypeId;
    if (this.taskType != null) {
      data['lookUp'] = this.taskType!.toJson();
    }
    data['planNo'] = this.planNo;
    data['planName'] = this.planName;
    data['planSequence'] = this.planSequence;
    data['frequencyId'] = this.frequencyId;
    data['frequency'] = this.frequency;
    data['autoConverstion'] = this.autoConverstion;
    data['comment'] = this.comment;
    data['scheduleDateFrom1'] = this.scheduleDateFrom1;
    data['scheduleDateFrom2'] = this.scheduleDateFrom2;
    if (this.site != null) {
      data['site'] = this.site!.toJson();
    }
    data['building'] = this.building;
    data['floor'] = this.floor;
    data['department'] = this.department;
    data['room'] = this.room;
    // if (this.planRecurrentMedicalRooms != null) {
    //   data['planRecurrentMedicalRooms'] = this.planRecurrentMedicalRooms!.map((v) => v.toJson()).toList();
    // }
    data['friday'] = this.friday;
    data['saturday'] = this.saturday;
    data['sunday'] = this.sunday;
    data['monday'] = this.monday;
    data['tuesday'] = this.tuesday;
    data['wednesday'] = this.wednesday;
    data['thursday'] = this.thursday;
    return data;
  }
}
