import 'package:flutter/material.dart';

/// Threshold for time elapsed (e.g., 48 hours)
const Duration THRESHOLD_DURATION = Duration(hours: 48);

/// Action text labels
class ButtonActions {
  static const String checkIn = "Check In";
  static const String checkOut = "Check Out";
  static const String requestRegularize = "Regularize";
  static const String rejected = "Rejected";
  static const String approved = "Approved";
  static const String requested = "Requested";
  static const String approve = "Approve";
  static const String reject = "Reject";
}

/// Action colors mapping
class AttendanceActionColors {
  static const Map<String, Color> colors = {
    ButtonActions.checkIn: Colors.green,
    ButtonActions.checkOut: Colors.red,
    ButtonActions.requestRegularize: Colors.blue,
    ButtonActions.rejected: Colors.orange,
    ButtonActions.approved: Colors.green,
    ButtonActions.requested: Colors.yellow,
    ButtonActions.approve: Colors.green,
    ButtonActions.reject: Colors.red,
  };
}

/// Attendance button helper utilities
class AttendanceButtonHelper {
  static String getUniqueKey(String employeeId, String logId) {
    return '${employeeId}_$logId';
  }

  static bool isLogFromYesterday(DateTime? checkIn, DateTime? checkOut) {
    final yesterday = DateTime.now().subtract(const Duration(days: 1));
    final yesterdayOnly =
        DateTime(yesterday.year, yesterday.month, yesterday.day);

    return checkIn != null &&
        checkOut != null &&
        DateUtils.isSameDay(checkIn, yesterdayOnly) &&
        DateUtils.isSameDay(checkOut, yesterdayOnly);
  }

  static bool isTodayApproved(int activity, DateTime? checkIn) {
    final today = DateTime.now();
    return activity == 4 && DateUtils.isSameDay(checkIn, today);
  }

  static bool isApprovedButNotToday(int activity, bool isTodayApproved) {
    return activity == 4 && !isTodayApproved;
  }

  static bool isTimeElapsed(DateTime? time,
      [Duration threshold = THRESHOLD_DURATION]) {
    if (time == null) return false;
    return DateTime.now().difference(time).compareTo(threshold) > 0;
  }

  static bool isButtonDisabled({
    required bool isUploading,
    required bool isYesterday,
    required int activity,
    required bool isApprovedButNotToday,
  }) {
    return isUploading ||
        isYesterday ||
        activity == 2 ||
        activity == 5 ||
        isApprovedButNotToday;
  }

  static String getActionText(
      int activity, DateTime? checkIn, DateTime? checkOut) {
    switch (activity) {
      case 0:
        return ButtonActions.checkIn;
      case 1:
        if (checkOut == null && isTimeElapsed(checkIn)) {
          return ButtonActions.requestRegularize;
        }
        return ButtonActions.checkOut;
      case 2:
        return ButtonActions.requestRegularize;
      case 4:
        return ButtonActions.checkIn;
      case 5:
        return ButtonActions.rejected;
      default:
        return ButtonActions.checkIn;
    }
  }

  static Color getprimary({
    required bool isYesterday,
    required bool isTodayApproved,
    required int activity,
  }) {
    if (isYesterday) return Colors.grey;
    if (isTodayApproved) return Colors.green;

    return AttendanceActionColors.colors[
            activity == 0 ? ButtonActions.checkIn : ButtonActions.checkOut] ??
        Colors.grey;
  }

  static bool isOlderThanDays(DateTime? date, int days) {
    if (date == null) return false;

    // Get today's date with time set to midnight (ignoring the time)
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);

    // Compare the date part (ignore time) of the provided date with today's date
    final compareDate = DateTime(date.year, date.month, date.day);
    final difference = today.difference(compareDate).inDays;

    return difference >=
        days; // Return true if the difference is greater than or equal to 'days'
  }

  static String getButtonText({
    required int activity,
    required DateTime? checkIn,
    required DateTime? checkOut,
    required bool isTodayApproved,
  }) {
    if (activity == 5) return ButtonActions.rejected;
    if (isTodayApproved) return ButtonActions.checkIn;
    if (activity == 4) return ButtonActions.approved;
    if (activity == 2) return ButtonActions.requested;

    if (activity == 0) {
      if (isTimeElapsed(checkIn)) {
        return ButtonActions.requestRegularize;
      }
      return ButtonActions.checkIn;
    }

    if (activity == 1) {
      if (checkOut == null && isTimeElapsed(checkIn)) {
        return ButtonActions.requestRegularize;
      }
      return ButtonActions.checkOut;
    }

    return ButtonActions.checkOut;
  }
}
