import 'package:flutter/src/widgets/framework.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/device/asset.dart';
import 'package:test_sa/models/fault_description.dart';
import 'package:test_sa/models/lookup.dart';

class ServiceRequest {
  String? id;
  String? requestCode;

  CallCreatedBy? callCreatedBy;
  String? deviceSerialNumber;
  int? deviceId;
  String? deviceArName;
  String? deviceEnName;
  List<String>? devicePhotos;
  String? callComments;
  String? hospitalName;
  int? hospitalId;
  String? departmentName;
  String? engineerName;
  String? engineerId;
  String? date;
  String? audio;
  int? statusValue;
  int? statusId;
  String? statusLabel;
  String? reviewComment;
  String? comments;

  bool viewReport;
  String? engineerMobile;
  String? deviceModel;
  FaultDescription? faultDescription;

  //String? workPerformed;
  String? visitDate;
  String? startDate;
  String? endDate;
  String? workingHours;
  DateTime? nextVisitDate;
  String? jobSheetNumber;
  int? reportID;
  String? deviceNumber;
  Lookup? priority;
  Lookup? defectType;
  Lookup? type;
  Lookup? requestedThrough;
  Lookup? firstAction;
  Lookup? loanAvailability;
  Asset? device;

  ServiceRequest({
    this.id,
    this.date,
    this.requestCode,
    this.callCreatedBy,
    this.hospitalName,
    this.deviceArName,
    this.deviceEnName,
    this.devicePhotos,
    this.hospitalId,
    this.deviceSerialNumber,
    this.callComments,
    this.statusLabel,
    this.statusValue,
    this.statusId,
    this.departmentName,
    this.deviceId,
    this.audio,
    this.engineerName,
    this.engineerId,
    this.viewReport = false,
    this.deviceModel,
    this.engineerMobile,
    this.faultDescription,
    this.jobSheetNumber,
    this.visitDate,
    this.startDate,
    this.endDate,
    this.workingHours,
    this.nextVisitDate,
    //this.workPerformed,
    this.reportID,
    this.defectType,
    this.priority,
    this.deviceNumber,
    this.type,
    this.requestedThrough,
    this.device,
    this.reviewComment,
    this.comments,
    this.loanAvailability,
    this.firstAction,
  });

  factory ServiceRequest.fromJson(Map<String, dynamic> parsedJson) {
    List<String> images = [];
    if (parsedJson["attachmentsCallRequest"] != null) if (parsedJson["attachmentsCallRequest"] is List) {
      List list = parsedJson["attachmentsCallRequest"];
      images = list.map((e) => URLs.getFileUrl(e["name"]!) as String).toList();
    }
    // print(parsedJson["requestedDate"]??"");

    return ServiceRequest(
      id: parsedJson["id"].toString(),
      requestCode: parsedJson["callNo"].toString(),
      callCreatedBy: parsedJson['callCreatedBy'] != null ? CallCreatedBy.fromJson(parsedJson['callCreatedBy']) : null,

      hospitalName: parsedJson["asset"]["site"]["custName"],
      deviceNumber: parsedJson["asset"]["assetNumber"].toString(),
      deviceId: parsedJson["asset"]["id"],
      audio: URLs.getFileUrl(parsedJson["voiceNote"] ?? ""),
      deviceArName: parsedJson["asset"]["modelDefinition"]["assetName"] ?? "",
      deviceEnName: parsedJson["asset"]["modelDefinition"]["assetName"] ?? "",
      devicePhotos: images,
      deviceSerialNumber: parsedJson["asset"]["assetSerialNo"],
      date: DateTime.tryParse(parsedJson["requestedDate"] ?? "").toString(),
      // todo. add requeste time param for time
      callComments: parsedJson["callComments"],
      comments: parsedJson["comments"],
      statusLabel: parsedJson["status"] == null ? null : parsedJson["status"]["name"],
      statusValue: parsedJson["status"] == null ? null : parsedJson["status"]["value"],
      statusId: parsedJson["status"] == null ? null : parsedJson["status"]["id"],
      departmentName: parsedJson["asset"]["department"] != null ? parsedJson["asset"]["department"]["name"] : "",
      engineerName: parsedJson["assignedEmployee"] == null ? null : parsedJson["assignedEmployee"]["name"],
      engineerId: parsedJson["assignedEmployee"] == null ? null : parsedJson["assignedEmployee"]["id"],
      hospitalId: parsedJson["asset"]["site"]["id"],
      reportID: parsedJson["workOrder"] != null ? parsedJson["workOrder"]["workOrderId"] : null,
      viewReport: parsedJson["workOrder"] != null,
      deviceModel: parsedJson["asset"]["modelDefinition"]["modelName"],
      engineerMobile: parsedJson["assignedEmployee"] == null ? null : parsedJson["assignedEmployee"]["phone"],
      faultDescription: parsedJson["workOrder"] != null
          ? parsedJson["workOrder"]["faultDescription"] != null
              ? FaultDescription.fromJson(parsedJson["workOrder"]["faultDescription"])
              : null
          : null,

      jobSheetNumber: parsedJson["workOrder"] != null ? parsedJson["workOrder"]["jobSheetNumber"] : null,
      visitDate: DateTime.tryParse(parsedJson["visitDate"] ?? "").toString().split(" ").first,
      startDate: parsedJson["startDate"] == null ? null : DateTime.tryParse(parsedJson["startDate"] ?? "").toString().split(" ").first,
      endDate: parsedJson["endDate"] == null ? null : DateTime.tryParse(parsedJson["endDate"] ?? "").toString().split(" ").first,

      workingHours: parsedJson["workingHours"],

      nextVisitDate: DateTime.tryParse(parsedJson["nextVisitDate"] ?? ""),
      //workPerformed: parsedJson["workOrder"] != null ? parsedJson["workOrder"]["workPerformed"] : null,
      device: Asset.fromJson(parsedJson["asset"]),
      reviewComment: parsedJson["reviewComment"],
      type: Lookup.fromJson(parsedJson['typeofRequest']),
      defectType: Lookup.fromJson(parsedJson['defectType']),
      loanAvailability: Lookup.fromJson(parsedJson['loanAvailablity']),
      firstAction: Lookup.fromJson(parsedJson['firstAction']),
      requestedThrough: Lookup.fromJson(parsedJson['requestedThrough']),
      priority: Lookup.fromJson(parsedJson['priority']),
    );
  }

  Future<bool> validateNewRequest(BuildContext context) async {
    if (device == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.device}");
      return false;
    } else if (priority == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.priority}");
      return false;
    } else if (defectType == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.equipmentStatus}");
      return false;
    } else if (requestedThrough == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.status}");
      return false;
    } else if (type == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.requestType}");
      return false;
    } else if ((callComments ?? "").isEmpty) {
      await Fluttertoast.showToast(msg: "Please add comments");
      return false;
    }
    return true;
  }

}

class CallCreatedBy {
  String? id;
  String? name;

  CallCreatedBy({this.id, this.name});

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

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