import 'dart:typed_data';

import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/models/device/device.dart';
import 'package:test_sa/models/engineer.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/part.dart';
import 'package:test_sa/models/service_request/service_request.dart';
import 'package:test_sa/models/timer_model.dart';

class ServiceReport {
  int id;
  String operatingHours;
  DateTime visitDate;
  DateTime endDate;
  Lookup assetType;
  Lookup callLastSituation;
  Engineer engineer;
  Lookup status;
  Lookup type;
  Lookup reason;
  int faultDescriptionId;
  String workPreformed;
  //String workHours;
  String travelingHours;
  String invoiceNumber;
  String invoiceCode;
  List<Part> parts;
  String image;
  Device device;
  String quantity;
  String jobSheetNumber;
  TimerModel timer;
  String signatureNurse;
  String signatureEngineer;
  Uint8List localNurseSignature;
  Uint8List localEngineerSignature;

  ServiceReport(
      {this.id,
      this.visitDate,
      this.endDate,
      this.assetType,
      this.status,
      this.type,
      this.faultDescriptionId,
      //this.workHours,
      this.travelingHours,
      this.parts,
      this.engineer,
      this.workPreformed,
      this.reason,
      this.operatingHours,
      this.callLastSituation,
      this.jobSheetNumber,
      this.image,
      this.device,
      this.invoiceCode,
      this.invoiceNumber,
      this.quantity = "1",
      this.timer,
      this.signatureNurse,
      this.signatureEngineer,
      this.localNurseSignature,
      this.localEngineerSignature});

  Map<String, dynamic> toMap(ServiceRequest request) {
    Map<String, dynamic> _map = {};
    if (id != null) _map["id"] = id;
    if (visitDate != null) _map["visitDate"] = visitDate.toIso8601String();
    //if(serviceType != null) _map["service_type"] = serviceType.id.toString();
    if (status != null) _map["status"] = status?.toMap();
    if (type != null) _map["typeOfWO"] = type?.toMap();
    if (assetType != null) _map["TypeOfWO"] = assetType?.toMap();
    //if(faultDescriptionId != null && faultDescriptionId.isNotEmpty) _map["fault_description"] = faultDescriptionId;
    //if(workHours != null && workHours.isNotEmpty) _map["working_hours"] = workHours;
    if (timer != null) {
      _map["startofWorkTime"] = timer.startAt.toIso8601String();
      _map["endofWorkTime"] = (timer.endAt ?? DateTime.now()).toIso8601String();
      _map["workingHours"] = (timer.durationInSecond / 60 / 60).toStringAsFixed(5);
    }
    if (travelingHours != null && travelingHours.isNotEmpty) _map["traveling_hours"] = travelingHours;
    // if(workPreformed != null && workPreformed.isNotEmpty){
    //   _map["faultDescription"] = {
    //     //"id":faultDescriptionId ?? 0,
    //     "workPerformed":workPreformed
    //   };
    // }
    if (jobSheetNumber != null && jobSheetNumber.isNotEmpty) _map["job_sheet_no"] = jobSheetNumber;
    if (parts != null && parts.isNotEmpty) {
      _map["sparePartsWorkOrders"] = parts.map((e) => e.toJson()).toList();
    }
    if (device != null && device.id != null) {
      _map["callRequest"] = {
        "id": request.id,
        "asset": device?.toMap(assetType),
      };
      _map["callRequest"]["asset"]["invoiceNumber"] = invoiceNumber;
    }
    _map["AssignedEmployee"] = engineer?.toMap();
    //if(quantity != null && quantity.isNotEmpty) _map["qty"] = quantity;
    //if(endDate != null) _map["end_date"] = (endDate.millisecondsSinceEpoch ~/ 1000).toString();
    if (reason != null) _map["reason"] = reason.toMap();
    //if(operatingHours != null && operatingHours.isNotEmpty) _map["operation_hours"] = operatingHours;
    if (callLastSituation != null) _map["calllastSituation"] = callLastSituation.toMap();
    //if(image != null) _map["image"] = image;
    //if(invoiceCode != null) _map["invoice_no"] = invoiceCode;
    //if(invoiceNumber != null) _map["invoice_code"] = invoiceNumber;
    if (image != null) {
      _map["attachmentsWorkOrder"] = [
        {"name": image}
      ];
    }
    _map["nurseSignature"] = signatureNurse;
    _map["engSignature"] = signatureEngineer;
    return _map;
  }

  bool validate() {
    if (visitDate == null) return false;
    //if(serviceType == null) return false;
    if (status == null) return false;
    if (type == null && assetType == null) return false;
    if (engineer == null) return false;
    if (callLastSituation == null) return false;
    if (callLastSituation?.value == 12) {
      //  if(invoiceCode != null || invoiceCode?.isEmpty == true) return false;
      if (invoiceNumber != null || invoiceNumber?.isEmpty == true) return false;
    }
    if (parts == null) return false;
    //if(endDate == null) return false;
    //if(reason == null) return false;
    //todo uncoment this line
    //if((device?.id == null || device.id.isEmpty) && type?.id != 1) return false;
    //if(quantity == null || quantity.isEmpty) return false;
    //if(image == null) return false;
    return true;
  }

  factory ServiceReport.fromJson(Map<String, dynamic> parsedJson, int id) {
    List<Part> _parts = [];
    if (parsedJson["sparePartsWorkOrders"] != null) {
      if (parsedJson["sparePartsWorkOrders"][0]["id"] != null) {
        List partsList = parsedJson["sparePartsWorkOrders"];
        _parts = partsList.map((e) => Part.fromJson(e["sparePart"], reportJson: e)).toList();
      }
    }
    return ServiceReport(
      id: id,
      assetType: Lookup.fromJson(parsedJson["assetType"]),
      callLastSituation: Lookup.fromJson(parsedJson["calllastSituation"]),
      reason: Lookup.fromJson(parsedJson["reason"]),
      status: Lookup.fromJson(parsedJson["status"]),
      type: Lookup.fromJson(parsedJson["typeOfWO"]),
      //faultDescriptionId: parsedJson["fault_description"],
      endDate: DateTime.tryParse(parsedJson["endofWorkTime"]),
      //invoiceCode: parsedJson["invoice_code"],
      //invoiceNumber: parsedJson["invoice_no"],
      //jobSheetNumber: parsedJson["job_sheet_no"],
      //operatingHours: parsedJson["workingHours"],
      engineer: Engineer.fromJson(parsedJson["assignedEmployee"]),
      parts: _parts,
      //quantity: parsedJson["nid"],
      //travelingHours: parsedJson["traveling_hours"],
      visitDate: DateTime.tryParse(parsedJson["visitDate"]),
      //workHours: parsedJson["working_hours"],
      timer: TimerModel(
          startAt: DateTime.tryParse(parsedJson["startofWorkTime"]), endAt: DateTime.tryParse(parsedJson["endofWorkTime"]), durationInSecond: ((parsedJson["workingHours"] ?? 0) * 60 * 60).toInt()),
      //workPreformed: parsedJson["work_performed"],
      device: Device.fromJson(parsedJson["callRequest"]["asset"]),
      signatureNurse: URLs.getFileUrl(parsedJson["nurseSignature"]),
      signatureEngineer: URLs.getFileUrl(parsedJson["engSignature"]),
    );
  }

  // static getDate(String date){
  //   return date == null || date.isEmpty
  //       ? null : DateTime.fromMillisecondsSinceEpoch(int.tryParse(date) * 1000);
  // }
}
