import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/models/global_config_model.dart';
import 'package:hmg_qline/models/ticket_model.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:intl/intl.dart';

extension ScreenOrientationEnumExt on int {
  ScreenOrientationEnum toScreenOrientationEnum() {
    if (this == 1) {
      return ScreenOrientationEnum.portraitUp;
    } else if (this == 2) {
      return ScreenOrientationEnum.portraitDown;
    } else if (this == 3) {
      return ScreenOrientationEnum.landscapeRight;
    } else if (this == 4) {
      return ScreenOrientationEnum.landscapeLeft;
    } else {
      return ScreenOrientationEnum.portraitUp;
    }
  }
}

extension DateFormatExt on int {
  String toFormattedDateTimeFromInt() {
    return DateFormat('hh:mm a').format(DateTime.fromMillisecondsSinceEpoch(this));
  }

  DateTime toDateTimeFromInt() {
    return DateTime.fromMillisecondsSinceEpoch(this);
  }
}

extension NavigationExt on BuildContext {
  navigateReplaceTo(String route) {
    Navigator.pushReplacementNamed(this, route);
  }

  navigateTo(String route) {
    Navigator.pushNamed(this, route);
  }

  popScreen() => Navigator.of(this).pop();
}

extension ScreenOrientationExt on ScreenOrientationEnum {
  int getTurnsByOrientation() {
    switch (this) {
      case ScreenOrientationEnum.portraitUp:
        return 0;
      case ScreenOrientationEnum.portraitDown:
        return 2;
      case ScreenOrientationEnum.landscapeRight:
        return 1;
      case ScreenOrientationEnum.landscapeLeft:
        return 3;
      default:
        return 0;
    }
  }
}

extension QTypeEnumExtension on int {
  QTypeEnum toQTypeEnum() {
    // return QTypeEnum.lab;
    switch (this) {
      case 1:
        return QTypeEnum.appointment;
      case 2:
        return QTypeEnum.lab;
      case 3:
        return QTypeEnum.rad;
      case 4:
        return QTypeEnum.general;
      default:
        return QTypeEnum.appointment;
    }
  }
}

extension QTypeExtension on QTypeEnum {
  // Convert the QType enum to its corresponding integer ID
  int getQTypeIDFromEnum() {
    return index + 1; // enum index starts from 0, so we add 1 to match your values.
  }
}

extension ScreenTypeExtension on int {
  // Convert int to ScreenType enum
  ScreenTypeEnum toScreenTypeEnum() {
    switch (this) {
      case 1:
        return ScreenTypeEnum.waitingAreaScreen;
      case 2:
        return ScreenTypeEnum.roomLevelScreen;
      case 3:
        return ScreenTypeEnum.receptionScreen;
      case 4:
        return ScreenTypeEnum.dashboardScreen;
      case 5:
        return ScreenTypeEnum.kioskScreen;
      default:
        return ScreenTypeEnum.waitingAreaScreen;
    }
  }
}

extension ScreenTypeIDExtension on ScreenTypeEnum {
  // Convert the ScreenType enum to its corresponding integer ID
  int getScreenTypeIDFromEnum() {
    return index + 1; // enum index starts from 0, so we add 1 to match your values.
  }
}

extension IntToLanguageEnum on int {
  LanguageEnum toLanguageEnum() {
    switch (this) {
      case 1:
        return LanguageEnum.english;
      case 2:
        return LanguageEnum.arabic;
      default:
        return LanguageEnum.english;
    }
  }
}

extension LanguageEnumToInt on LanguageEnum {
  int enumToInt() {
    switch (this) {
      case LanguageEnum.english:
        return 1;
      case LanguageEnum.arabic:
        return 2;
      default:
        return 1;
    }
  }
}

extension LanguageEnumToString on LanguageEnum {
  String enumToString() {
    switch (this) {
      case LanguageEnum.english:
        return "en";
      case LanguageEnum.arabic:
        return "ar";
      default:
        return "en";
    }
  }
}

enum CallTypeEnum { vitalSign, doctor, procedure, vaccination, nebulization, none }

extension XCallType on CallTypeEnum {
  Color getColorByCallType() {
    if (this == CallTypeEnum.vitalSign) {
      return AppColors.vitalSignColor;
    } else if (this == CallTypeEnum.doctor) {
      return AppColors.doctorColor;
    } else if (this == CallTypeEnum.procedure) {
      return AppColors.procedureColor;
    } else if (this == CallTypeEnum.vaccination) {
      return AppColors.vaccinationColor;
    } else if (this == CallTypeEnum.nebulization) {
      return AppColors.nebulizationColor;
    } else {
      return Colors.black54;
    }
  }

  String getMessageByCallType(GlobalConfigurationsModel globalConfig, {bool isListView = false}) {
    switch (this) {
      case CallTypeEnum.vitalSign:
        return !isListView ? globalConfig.callForVitalSignText : globalConfig.vitalSignText;
      case CallTypeEnum.doctor:
        return !isListView ? globalConfig.callForDoctorText : globalConfig.doctorText;
      case CallTypeEnum.procedure:
        return !isListView ? globalConfig.callForProcedureText : globalConfig.procedureText;
      case CallTypeEnum.vaccination:
        return !isListView ? globalConfig.callForVaccinationText : globalConfig.vaccinationText;
      case CallTypeEnum.nebulization:
        return !isListView ? globalConfig.callForNebulizationText : globalConfig.nebulizationText;
      case CallTypeEnum.none:
        return !isListView ? globalConfig.callForVitalSignText : globalConfig.vitalSignText;

      default:
        return globalConfig.callForVitalSignText;
    }
  }

  SvgPicture getIconByCallType(double height, {double? width, BoxFit fit = BoxFit.contain}) {
    String iconPath = "";
    if (this == CallTypeEnum.vitalSign) {
      iconPath = AppAssets.vitalSignIcon;
    } else if (this == CallTypeEnum.doctor) {
      iconPath = AppAssets.doctorIcon;
    } else if (this == CallTypeEnum.procedure) {
      iconPath = AppAssets.procedureIcon;
    } else if (this == CallTypeEnum.vaccination) {
      iconPath = AppAssets.vaccinationIcon;
    } else if (this == CallTypeEnum.nebulization) {
      iconPath = AppAssets.nebulizationIcon;
    }

    return SvgPicture.asset(
      iconPath.isEmpty ? "assets/images/wait.svg" : iconPath,
      height: height,
      width: width,
      color: getColorByCallType(),
      // fit: fit,
    );
  }

  int getIdFromCallTypeEnum() {
    switch (this) {
      case CallTypeEnum.vitalSign:
        return 1;
      case CallTypeEnum.doctor:
        return 2;
      case CallTypeEnum.procedure:
        return 3;
      case CallTypeEnum.vaccination:
        return 4;
      case CallTypeEnum.nebulization:
        return 5;
      case CallTypeEnum.none:
        return 1;
    }
  }

  String getCallTextFromCallType(TicketData ticket) {
    switch (this) {
      case CallTypeEnum.vitalSign:
        return ticket.callForVitalSignText;
      case CallTypeEnum.doctor:
        return ticket.callForDoctorText;
      case CallTypeEnum.procedure:
        return ticket.callForProcedureText;
      case CallTypeEnum.vaccination:
        return ticket.callForVaccinationText;
      case CallTypeEnum.nebulization:
        return ticket.callForNebulizationText;
      default:
        return ticket.callForVitalSignText;
    }
  }
}

extension XCallTypeInt on int {
  CallTypeEnum toCallTypeEnum() {
    if (this == 1) return CallTypeEnum.vitalSign;
    if (this == 2) return CallTypeEnum.doctor;
    if (this == 3) return CallTypeEnum.procedure;
    if (this == 4) return CallTypeEnum.vaccination;
    if (this == 5) return CallTypeEnum.nebulization;
    return CallTypeEnum.vitalSign;
  }
}
