import 'dart:typed_data';

import 'package:flutter/cupertino.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/extensions/string_extensions.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/service_request/spare_parts.dart';
import 'package:test_sa/models/service_request/supp_engineer_work_orders.dart';
import 'package:test_sa/models/service_request/supplier_details.dart';
import 'package:test_sa/models/service_request/wo_call_request.dart';
import 'package:test_sa/models/service_request/wo_parent.dart';
import 'package:test_sa/models/timer_model.dart';

import '../../attachment.dart';
import '../device/asset.dart';
import '../fault_description.dart';
import '../new_models/assigned_employee.dart';
import '../new_models/assistant_employee.dart';
import 'contact_person_work_order.dart';

class ServiceReport {
  ServiceReport({
    this.id,
    this.parentWOId,
    this.workOrderNo,
    this.workOrderYear,
    this.workOrderSequennce,
    this.callRequest,
    this.assetType,
    this.assignedEmployee,
    this.visitDate,
    this.assistantEmployees,
    this.supplier,
    this.vendorTicketNumber,
    this.mrNumber,
    this.contactPersonWorkOrders,
    this.calllastSituation,
    this.currentSituation,
    this.repairLocation,
    this.reason,
    this.serviceType,
    this.startofWorkTime,
    this.endofWorkTime,
    this.workingHours,
    this.travelingHours,
    this.travelingExpenses,
    this.faultDescription,
    this.sparePartsWorkOrders,
    this.reviewComment,
    this.comment,
    this.attachmentsWorkOrder,
    this.equipmentStatus,
    this.suppEngineerWorkOrders,
    this.engSignature,
    this.nurseSignature,
    this.woParentDto,
    this.loanAvailablity,
    this.assetLoan,
    this.timer,
  });

  ServiceReport.fromJson(dynamic json) {
    id = json['id'];
    parentWOId = json['parentWOId'];
    workOrderNo = json['workOrderNo'];
    workOrderYear = json['workOrderYear'];
    workOrderSequennce = json['workOrderSequennce'];
    callRequest = json['callRequest'] != null ? CallRequest.fromJson(json['callRequest']) : null;
    assetType = json['assetType'] != null ? Lookup.fromJson(json['assetType']) : null;
    assignedEmployee = json['assignedEmployee'] != null ? AssignedEmployee.fromJson(json['assignedEmployee']) : null;
    visitDate = json['visitDate'];
    if (json['assistantEmployees'] != null) {
      assistantEmployees = [];
      json['assistantEmployees'].forEach((v) {
        assistantEmployees!.add(AssistantEmployees.fromJson(v));
      });
    }
    supplier = json['supplier'] != null ? SupplierDetails.fromJson(json['supplier']) : null;
    vendorTicketNumber = json['vendorTicketNumber'];
    mrNumber = json['mrNumber'];
    if (json['contactPersonWorkOrders'] != null) {
      contactPersonWorkOrders = [];
      json['contactPersonWorkOrders'].forEach((v) {
        contactPersonWorkOrders!.add(ContactPersonWorkOrder.fromJson(v));
      });
    }
    calllastSituation = json['calllastSituation'] != null ? Lookup.fromJson(json['calllastSituation']) : null;
    currentSituation = json['currentSituation'] != null ? Lookup.fromJson(json['currentSituation']) : null;
    repairLocation = json['repairLocation'] != null ? Lookup.fromJson(json['repairLocation']) : null;
    reason = json['reason'] != null ? Lookup.fromJson(json['reason']) : null;
    serviceType = json['serviceType'] != null ? Lookup.fromJson(json['serviceType']) : null;
    startofWorkTime = json['startofWorkTime'];
    endofWorkTime = json['endofWorkTime'];
    workingHours = json['workingHours'];
    try {
      timer = TimerModel(startAt: DateTime.tryParse(startofWorkTime ?? ""), endAt: DateTime.tryParse(endofWorkTime ?? ""));
      if (timer!.endAt != null && timer!.startAt != null) {
        timer!.durationInSecond = (timer!.endAt!.difference(timer!.startAt!)).inSeconds;
        workingHours = (((timer!.durationInSecond ?? 0) / 60) / 60);
      }
    } catch (e) {
      print(e);
    }
    travelingHours = json['travelingHours'];
    travelingExpenses = json['travelingExpenses'];
    faultDescription = json['faultDescription'] != null ? FaultDescription.fromJson(json['faultDescription']) : null;
    if (json['sparePartsWorkOrders'] != null) {
      sparePartsWorkOrders = [];
      json['sparePartsWorkOrders'].forEach((v) {
        sparePartsWorkOrders!.add(SparePartsWorkOrders.fromJson(v));
      });
    }
    reviewComment = json['reviewComment'];
    comment = json['comment'];
    if (json['attachmentsWorkOrder'] != null) {
      attachmentsWorkOrder = [];
      json['attachmentsWorkOrder'].forEach((v) {
        v["name"] = URLs.getFileUrl(v["name"]);
        attachmentsWorkOrder!.add(Attachment.fromJson(v));
      });
    }
    equipmentStatus = json['equipmentStatus'] != null ? Lookup.fromJson(json['equipmentStatus']) : null;
    if (json['suppEngineerWorkOrders'] != null) {
      suppEngineerWorkOrders = [];
      json['suppEngineerWorkOrders'].forEach((v) {
        suppEngineerWorkOrders!.add(SuppEngineerWorkOrders.fromJson(v));
      });
    }
    engSignature = json['engSignature'];
    nurseSignature = json['nurseSignature'];
    woParentDto = json['woParentDto'] != null ? WoParent.fromJson(json['woParentDto']) : null;
    loanAvailablity = json['loanAvailablity'] != null ? Lookup.fromJson(json['loanAvailablity']) : null;
    assetLoan = json['assetLoan'] != null ? AssetInfo.fromJson(json['assetLoan']) : null;
  }

  num? id;
  num? parentWOId;
  String? workOrderNo;
  num? workOrderYear;
  num? workOrderSequennce;
  CallRequest? callRequest;
  Lookup? assetType;
  AssignedEmployee? assignedEmployee;
  String? visitDate;
  List<AssistantEmployees>? assistantEmployees;
  SupplierDetails? supplier;
  String? vendorTicketNumber;
  String? mrNumber;
  List<ContactPersonWorkOrder>? contactPersonWorkOrders;
  Lookup? calllastSituation;
  Lookup? currentSituation;
  Lookup? repairLocation;
  Lookup? reason;
  Lookup? serviceType;
  String? startofWorkTime;
  String? endofWorkTime;
  num? workingHours;
  num? travelingHours;
  num? travelingExpenses;
  FaultDescription? faultDescription;
  List<SparePartsWorkOrders>? sparePartsWorkOrders;
  String? reviewComment;
  String? comment;
  List<Attachment>? attachmentsWorkOrder;
  Lookup? equipmentStatus;
  List<SuppEngineerWorkOrders>? suppEngineerWorkOrders;
  String? engSignature;
  Uint8List? localEngSignature;
  String? nurseSignature;
  Uint8List? localNurseSignature;
  WoParent? woParentDto;
  Lookup? loanAvailablity;
  AssetInfo? assetLoan;
  TimerModel? timer = TimerModel();

  ServiceReport copyWith({
    num? id,
    num? parentWOId,
    String? workOrderNo,
    num? workOrderYear,
    num? workOrderSequennce,
    CallRequest? callRequest,
    Lookup? assetType,
    AssignedEmployee? assignedEmployee,
    String? visitDate,
    List<AssistantEmployees>? assistantEmployees,
    SupplierDetails? supplier,
    String? vendorTicketNumber,
    String? mrNumber,
    List<ContactPersonWorkOrder>? contactPersonWorkOrders,
    Lookup? calllastSituation,
    Lookup? currentSituation,
    Lookup? repairLocation,
    Lookup? reason,
    Lookup? serviceType,
    String? startofWorkTime,
    String? endofWorkTime,
    num? workingHours,
    num? travelingHours,
    num? travelingExpenses,
    FaultDescription? faultDescription,
    List<SparePartsWorkOrders>? sparePartsWorkOrders,
    String? reviewComment,
    String? comment,
    List<Attachment>? attachmentsWorkOrder,
    Lookup? equipmentStatus,
    List<SuppEngineerWorkOrders>? suppEngineerWorkOrders,
    String? engSignature,
    String? nurseSignature,
    WoParent? woParentDto,
    Lookup? loanAvailablity,
    AssetInfo? assetLoan,
    TimerModel? timer,
  }) =>
      ServiceReport(
        id: id ?? this.id,
        parentWOId: parentWOId ?? this.parentWOId,
        workOrderNo: workOrderNo ?? this.workOrderNo,
        workOrderYear: workOrderYear ?? this.workOrderYear,
        workOrderSequennce: workOrderSequennce ?? this.workOrderSequennce,
        callRequest: callRequest ?? this.callRequest,
        assetType: assetType ?? this.assetType,
        assignedEmployee: assignedEmployee ?? this.assignedEmployee,
        visitDate: visitDate ?? this.visitDate,
        assistantEmployees: assistantEmployees ?? this.assistantEmployees,
        supplier: supplier ?? this.supplier,
        vendorTicketNumber: vendorTicketNumber ?? this.vendorTicketNumber,
        mrNumber: mrNumber ?? this.mrNumber,
        contactPersonWorkOrders: contactPersonWorkOrders ?? this.contactPersonWorkOrders,
        calllastSituation: calllastSituation ?? this.calllastSituation,
        currentSituation: currentSituation ?? this.currentSituation,
        repairLocation: repairLocation ?? this.repairLocation,
        reason: reason ?? this.reason,
        serviceType: serviceType ?? this.serviceType,
        startofWorkTime: startofWorkTime ?? this.startofWorkTime,
        endofWorkTime: endofWorkTime ?? this.endofWorkTime,
        workingHours: workingHours ?? this.workingHours,
        travelingHours: travelingHours ?? this.travelingHours,
        travelingExpenses: travelingExpenses ?? this.travelingExpenses,
        faultDescription: faultDescription ?? this.faultDescription,
        sparePartsWorkOrders: sparePartsWorkOrders ?? this.sparePartsWorkOrders,
        reviewComment: reviewComment ?? this.reviewComment,
        comment: comment ?? this.comment,
        attachmentsWorkOrder: attachmentsWorkOrder ?? this.attachmentsWorkOrder,
        equipmentStatus: equipmentStatus ?? this.equipmentStatus,
        suppEngineerWorkOrders: suppEngineerWorkOrders ?? this.suppEngineerWorkOrders,
        engSignature: engSignature ?? this.engSignature,
        nurseSignature: nurseSignature ?? this.nurseSignature,
        woParentDto: woParentDto ?? this.woParentDto,
        loanAvailablity: loanAvailablity ?? this.loanAvailablity,
        assetLoan: assetLoan ?? this.assetLoan,
        timer: timer ?? this.timer,
      );

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id ?? 0;
    map['parentWOId'] = parentWOId;
    map['workOrderNo'] = workOrderNo;
    map['workOrderYear'] = workOrderYear;
    map['workOrderSequennce'] = workOrderSequennce;
    if (callRequest != null) {
      map['callRequest'] = callRequest!.toJson();
    }
    if (assetType != null) {
      map['assetType'] = assetType!.toJson();
    }
    if (assignedEmployee != null) {
      map['assignedEmployee'] = assignedEmployee!.toJson();
    }
    map['visitDate'] = visitDate;
    if (assistantEmployees != null) {
      map['assistantEmployees'] = assistantEmployees!.map((v) => v.toJson()).toList();
    }
    if (supplier != null) {
      map['supplier'] = supplier!.toJson();
    }
    map['vendorTicketNumber'] = vendorTicketNumber;
    map['mrNumber'] = mrNumber;
    if (contactPersonWorkOrders != null) {
      map['contactPersonWorkOrders'] = contactPersonWorkOrders!.map((v) => v.toJson()).toList();
    }
    if (calllastSituation != null) {
      map['calllastSituation'] = calllastSituation!.toJson();
    }
    if (currentSituation != null) {
      map['currentSituation'] = currentSituation!.toJson();
    }
    if (repairLocation != null) {
      map['repairLocation'] = repairLocation!.toJson();
    }
    if (reason != null) {
      map['reason'] = reason!.toJson();
    }
    if (serviceType != null) {
      map['serviceType'] = serviceType!.toJson();
    }
    map['startofWorkTime'] = timer?.startAt?.toString();
    map['endofWorkTime'] = timer?.endAt?.toString();
    map['workingHours'] = workingHours;
    map['travelingHours'] = travelingHours;
    map['travelingExpenses'] = travelingExpenses;
    if (faultDescription != null) {
      map['faultDescription'] = faultDescription!.toJson();
    }
    if (sparePartsWorkOrders != null) {
      map['sparePartsWorkOrders'] = sparePartsWorkOrders!.map((v) => v.toJson()).toList();
    }
    map['reviewComment'] = reviewComment;
    map['comment'] = comment;
    if (attachmentsWorkOrder != null) {
      map['attachmentsWorkOrder'] = attachmentsWorkOrder!.map((v) => {"name": v.name!.getFileName}).toList();
    }
    if (equipmentStatus != null) {
      map['equipmentStatus'] = equipmentStatus!.toJson();
    }
    if (suppEngineerWorkOrders != null) {
      map['suppEngineerWorkOrders'] = suppEngineerWorkOrders!.map((v) => v.toJson()).toList();
    }
    map['engSignature'] = engSignature;
    map['nurseSignature'] = nurseSignature;
    if (woParentDto != null) {
      map['woParentDto'] = woParentDto!.toJson();
    }
    if (loanAvailablity != null) {
      map['loanAvailablity'] = loanAvailablity!.toJson();
    }
    if (assetLoan != null) {
      map['assetLoan'] = assetLoan!.toJson();
    }
    return map;
  }

  Future<bool> validate(BuildContext context) async {
    if (timer?.startAt == null) {
      await Fluttertoast.showToast(msg: "Working Hours Required");
      return false;
    }
    if (timer?.endAt == null) {
      await Fluttertoast.showToast(msg: "Please Stop The Timer");
      return false;
    }
    if (calllastSituation == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.callLastSituation}");
      return false;
    }
    if (reason == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.reason}");
      return false;
    }
    // if (sparePartsWorkOrders?.isEmpty ?? true) {
    //   await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.partNo}");
    //   return false;
    // }
    if (reason == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.reason}");
      return false;
    }
    return true;
  }
}
