import 'dart:convert';
import 'dart:io';

import '../lookup.dart';
import '../timer_model.dart';
import 'calibration_tools.dart';
import 'pm_kit.dart';
import 'ppm_check_list.dart';

class Pentry {
  Lookup? ppmVisitStatus;
  Lookup? status;
  TimerModel? timer;
  DateTime? actualVisitDate;
  String? travelingHours;
  String? image;
  File? imageFile;
  List<PPMCheckList>? ppmCheckLists;
  List<CalibrationTool>? calibrationTools;
  List<PMKit>? pmKits;

  Pentry({
    this.travelingHours,
    this.timer,
    this.status,
    this.ppmVisitStatus,
    this.actualVisitDate,
    this.image,
    this.imageFile,
    // this.contacts,
    this.ppmCheckLists,
    this.calibrationTools,
    this.pmKits,
  });

  bool validate() {
    if (actualVisitDate == null) return false;
    if (timer == null && timer?.endAt != null) return false;
    if (ppmVisitStatus == null) return false;
    //if(status == null) return false;

    return true;
  }

  Map<String, String> toMap() {
    Map<String, String> map = {};
    if (ppmVisitStatus != null) map["visit_status"] = ppmVisitStatus!.id.toString();
    if (status != null) map["pentry_status"] = status!.id.toString();
    if (travelingHours != null) map["traveling_hours"] = travelingHours!;
    if (imageFile != null) map["file_attachement"] = base64Encode(imageFile!.readAsBytesSync());
    map["actual_date"] = ((actualVisitDate?.millisecondsSinceEpoch ?? 0) / 1000).toStringAsFixed(0);
    if (timer != null) {
      map["start_date"] = ((timer!.startAt?.millisecondsSinceEpoch ?? 0) / 1000).toStringAsFixed(0);
      map["end_date"] = (((timer!.endAt)?.millisecondsSinceEpoch ?? 0) / 1000).toStringAsFixed(0);
      map["working_hours"] = ((timer!.durationInSecond ?? 0) / 60 / 60).toStringAsFixed(5);
    }
    // if(contacts?.isNotEmpty == true) {
    //   for(int i = 0;i<contacts.length;i++){
    //     contacts[i].toMap().forEach((key, value) {
    //       body["contacts[$i].$key"] = value;
    //     });
    //   }
    // }
    map["ppmCheckLists"] = jsonEncode(ppmCheckLists?.map((e) => e.toMap()).toList());
    map["calibrationTools"] = jsonEncode(calibrationTools?.map((e) => e.toMap()).toList());
    map["pmKits"] = jsonEncode(pmKits?.map((e) => e.toMap()).toList());
    return map;
  }

  factory Pentry.fromMap(Map<String, dynamic> map) {
    // List<Contact> contacts = [];
    // if(map['contacts'] != null){
    //   contacts =(map['contacts'] as List<dynamic>)
    //       .map((e) => Contact.fromMap(e as Map<String, dynamic>))
    //       .toList();
    // }

    List<PMKit> pmKits = [];
    if (map['pmKits'] != null) {
      pmKits = (map['pmKits'] as List<dynamic>).map((e) => PMKit.fromMap(e as Map<String, dynamic>)).toList();
    }

    List<PPMCheckList> ppmCheckLists = [];
    if (map['ppmCheckLists'] != null) {
      ppmCheckLists = (map['ppmCheckLists'] as List<dynamic>).map((e) => PPMCheckList.fromMap(e as Map<String, dynamic>)).toList();
    }

    List<CalibrationTool> calibrationTools = [];
    if (map['calibrationTools'] != null) {
      calibrationTools = (map['calibrationTools'] as List<dynamic>).map((e) => CalibrationTool.fromMap(e as Map<String, dynamic>)).toList();
    }
    return Pentry(
      status: Lookup.fromJson(map["pentry_status"]),
      ppmVisitStatus: Lookup.fromJson(map["visit_status"]),
      actualVisitDate: getDate(map["actual_date"]),
      travelingHours: map["traveling_hours"],
      timer: TimerModel(startAt: getDate(map["start_date"]), endAt: getDate(map["end_date"]), durationInSecond: (int.tryParse(map["working_hours"] ?? "") ?? 0) * 60 * 60),
      // contacts: contacts,
      ppmCheckLists: ppmCheckLists,
      calibrationTools: calibrationTools,
      pmKits: pmKits,
    );
  }

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