import 'package:hmg_qline/utilities/date_utils.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/utilities/extensions.dart';

class TicketDetailsModel {
  QTypeEnum? qTypeEnum;
  ScreenTypeEnum? screenTypeEnum;
  String? connectionID;
  TicketData? ticketModel;

  TicketDetailsModel({this.qTypeEnum, this.screenTypeEnum, this.connectionID, this.ticketModel});

  TicketDetailsModel.fromJson(Map<String, dynamic> json) {
    qTypeEnum = json['qType'] != null ? (json['qType'] as int).toQTypeEnum() : QTypeEnum.appointment;
    screenTypeEnum = json['screenType'] != null ? (json['screenType'] as int).toScreenTypeEnum() : ScreenTypeEnum.waitingAreaScreen;
    connectionID = json['connectionID'] ?? '';
    ticketModel = json['data'] != null
        ? TicketData.fromJson(
            json['data'],
            qTypeEnum: qTypeEnum,
          )
        : TicketData(); // Use default empty TicketData if null
  }
}

class TicketData {
  int id;
  int patientID;
  int laBQGroupID;
  String queueNo;
  int counterBatchNo;
  int calledBy;
  String calledOn;
  String servedOn;
  String patientName;
  String mobileNo;
  String patientEmail;
  int preferredLang;
  LanguageEnum voiceLanguageEnum;
  String ticketNoText;
  String postVoiceText;
  int patientGender;
  String roomNo;
  bool isActive;
  int createdBy;
  int editedBy;
  DateTime? editedOn;
  DateTime? createdOn;

  // New fields
  String doctorNameN;
  CallTypeEnum callTypeEnum;
  String queueNoM;
  String callNoStr;
  bool isQueue;
  bool isToneReq;
  bool isVoiceReq;
  int orientationType;
  bool isTurnOn;
  int concurrentCallDelaySec;
  String crTypeAckIP;
  int voiceLanguage;
  String voiceLanguageText;
  String vitalSignText;
  String doctorText;
  String procedureText;
  String vaccinationText;
  String nebulizationText;
  String callForVitalSignText;
  String callForDoctorText;
  String callForProcedureText;
  String callForVaccinationText;
  String callForNebulizationText;
  String roomText;
  String queueNoText;
  String callForText;

  TicketData({
    this.id = 0,
    this.patientID = 0,
    this.laBQGroupID = 0,
    this.queueNo = "",
    this.counterBatchNo = 0,
    this.calledBy = 0,
    this.calledOn = "",
    this.servedOn = "",
    this.patientName = "",
    this.mobileNo = "",
    this.patientEmail = "",
    this.preferredLang = 1,
    this.voiceLanguageEnum = LanguageEnum.english,
    this.ticketNoText = "Ticket Number",
    this.postVoiceText = "Please Visit Counter",
    this.patientGender = 1,
    this.roomNo = "",
    this.isActive = true,
    this.createdBy = 0,
    this.createdOn, // will fallback in fromJson (see below)
    this.editedBy = 0,
    this.editedOn, // will fallback in fromJson (see below)
    this.doctorNameN = "",
    this.callTypeEnum = CallTypeEnum.none,
    this.queueNoM = "",
    this.callNoStr = "",
    this.isQueue = false,
    this.isToneReq = false,
    this.isVoiceReq = false,
    this.orientationType = 1,
    this.isTurnOn = true,
    this.concurrentCallDelaySec = 1,
    this.crTypeAckIP = "",
    this.voiceLanguage = 1,
    this.voiceLanguageText = "English",
    this.vitalSignText = "Vital Sign",
    this.doctorText = "Doctor",
    this.procedureText = "Procedure",
    this.vaccinationText = "Vaccination",
    this.nebulizationText = "Nebulization",
    this.callForVitalSignText = "Call for Vital Sign",
    this.callForDoctorText = "Call for Doctor",
    this.callForProcedureText = "Call for Procedure",
    this.callForVaccinationText = "Call for Vaccination",
    this.callForNebulizationText = "Call for Nebulization",
    this.roomText = "Room",
    this.queueNoText = "Counter",
    this.callForText = "Call For",
  });

  TicketData.fromJson(Map<String, dynamic> json, {QTypeEnum? qTypeEnum})
      : id = json['id'] ?? 0,
        patientID = json['patientID'] ?? 0,
        laBQGroupID = json['laB_QGroupID'] ?? 0,
        queueNo = json['queueNoM'] ?? "",
        counterBatchNo = json['counterBatchNo'] ?? 0,
        calledBy = json['calledBy'] ?? 0,
        calledOn = json['calledOn'] ?? "",
        servedOn = json['servedOn'] ?? "",
        patientName = json['patientName'] ?? "",
        mobileNo = json['mobileNo'] ?? "",
        patientEmail = json['patientEmail'] ?? "",
        preferredLang = (json['preferredLang'] != null && json['preferredLang'].toString().trim() != "") ? int.parse(json['preferredLang'].toString()) : 1,
        voiceLanguageEnum = (json['preferredLang'] != null && json['preferredLang'].toString().trim() != "") ? (int.parse(json['preferredLang'].toString())).toLanguageEnum() : LanguageEnum.english,
        ticketNoText = json['ticketNoText'] ?? "Ticket Number",
        postVoiceText = json['pleaseVisitCounterText'] ?? "Please Visit Counter",
        patientGender = json['patientGender'] ?? 1,
        roomNo = (() {
          if (qTypeEnum != null && qTypeEnum == QTypeEnum.general) {
            return json['counterNo']?.toString() ?? "";
          }
          return json['roomNo']?.toString() ?? "";
        })(),
        isActive = json['isActive'] ?? true,
        createdBy = json['createdBy'] ?? 0,
        editedBy = json['editedBy'] ?? 0,
        createdOn = json['createdOn'] != null ? (json['createdOn'] as String).toDateTime() : DateTime.now(),
        editedOn = json['editedOn'] != null ? (json['editedOn'] as String).toDateTime() : DateTime.now(),
        doctorNameN = json['doctorNameN'] ?? "",
        callTypeEnum = ((json['callType'] ?? 0) as int).toCallTypeEnum(),
        queueNoM = json['queueNoM'] ?? "",
        callNoStr = json['callNoStr'] ?? "",
        isQueue = json['isQueue'] ?? false,
        isToneReq = json['isToneReq'] ?? false,
        isVoiceReq = json['isVoiceReq'] ?? false,
        orientationType = json['orientationType'] ?? 1,
        isTurnOn = json['isTurnOn'] ?? true,
        concurrentCallDelaySec = json['concurrentCallDelaySec'] ?? 1,
        crTypeAckIP = json['crTypeAckIP'] ?? "",
        voiceLanguage = json['voiceLanguage'] ?? 1,
        voiceLanguageText = json['voiceLanguageText'] ?? "English",
        vitalSignText = json['vitalSignText'] ?? "Vital Sign",
        doctorText = json['doctorText'] ?? "Doctor",
        procedureText = json['procedureText'] ?? "Procedure",
        vaccinationText = json['vaccinationText'] ?? "Vaccination",
        nebulizationText = json['nebulizationText'] ?? "Nebulization",
        callForVitalSignText = json['callForVitalSignText'] ?? "Call for Vital Sign",
        callForDoctorText = json['callForDoctorText'] ?? "Call for Doctor",
        callForProcedureText = json['callForProcedureText'] ?? "Call for Procedure",
        callForVaccinationText = json['callForVaccinationText'] ?? "Call for Vaccination",
        callForNebulizationText = json['callForNebulizationText'] ?? "Call for Nebulization",
        roomText = json['roomText'] ?? "Room",
        queueNoText = json['queueNoText'] ?? "Counter",
        callForText = json['callForText'] ?? "Call For";

  @override
  String toString() => 'TicketData{id: $id, patientID: $patientID, laBQGroupID: $laBQGroupID, queueNo: $queueNo, ...}';
}
