import { HIGHLIGHTING_CONFIG } from './httpRequestConstants'
import type { QueueAnalysis, CDNAnalysis } from '../types/httpRequest'
import styles from '../RequestRowSummary.module.css'

export const getTotalResponseTimeColor = (microseconds?: number) => {
  if (!microseconds) return HIGHLIGHTING_CONFIG.COLORS.DURATION.DEFAULT
  
  const totalResponseTimeMs = microseconds / 1000
  
  if (totalResponseTimeMs > HIGHLIGHTING_CONFIG.TOTAL_RESPONSE_TIME_THRESHOLDS.SLOW) {
    return { backgroundColor: '#ffebee', color: '#c62828' } // Red for > 200ms
  } else if (totalResponseTimeMs >= HIGHLIGHTING_CONFIG.TOTAL_RESPONSE_TIME_THRESHOLDS.FAST) {
    return { backgroundColor: '#fff8e1', color: '#f57c00' } // Yellow for 100-200ms
  } else {
    return { backgroundColor: '#e8f5e8', color: '#2e7d32' } // Green for < 100ms
  }
}

export const getTotalResponseTimeClass = (microseconds?: number): string => {
  if (!microseconds) return ''
  
  const totalResponseTimeMs = microseconds / 1000
  
  if (totalResponseTimeMs > HIGHLIGHTING_CONFIG.TOTAL_RESPONSE_TIME_THRESHOLDS.SLOW) {
    return styles.danger // Red for > 200ms
  } else if (totalResponseTimeMs >= HIGHLIGHTING_CONFIG.TOTAL_RESPONSE_TIME_THRESHOLDS.FAST) {
    return styles.warning // Yellow for 100-200ms
  } else {
    return styles.success // Green for < 100ms
  }
}
export const getConnectionClass = (time: number): string => {
  if (time<50000) {
    return styles.success;
  }
  if (time<180000) {
    return styles.warning;
  }
  return styles.danger;
}
export const getProtocolClass = (protocol?: string): string => {
    if (!protocol) return ''

    switch (protocol.toLowerCase()) {
        case 'http/2':
        case 'h2':
        return styles.warning
        case 'http/3':
        case 'h3':
        return styles.success
        case 'http/1.1':
        return styles.danger
        default:
        return styles.danger
    }
}

export const getDataRateClass = (transferSize?: number, contentLength?: number, durationMicroseconds?: number): string => {
  if (!durationMicroseconds || durationMicroseconds <= 0) return ''
  
  // Prefer content-length over transfer size for more accurate data rate calculation
  const bytes = contentLength && contentLength > 0 ? contentLength : transferSize
  if (!bytes) return ''
  
  // Convert duration from microseconds to seconds
  const durationSeconds = durationMicroseconds / 1000000
  
  // Calculate bytes per second
  const bytesPerSecond = bytes / durationSeconds
  
  // Convert to standard units for comparison
  const mbps = bytesPerSecond / (1024 * 1024) // MB/s
  const kbps = bytesPerSecond / 1024 // KB/s
  
  if (mbps >= 1) {
    return styles.success // Green for >= 1 MB/s
  } else if (kbps >= 100) {
    return styles.warning // Yellow for 100 KB/s - 1 MB/s
  } else {
    return styles.danger // Red for < 100 KB/s
  }
}

export const getServerLatencyClass = (microseconds?: number): string => {
  if (!microseconds) return ''
  
  if (microseconds > HIGHLIGHTING_CONFIG.SERVER_LATENCY_THRESHOLDS.SLOW) {
    return styles.danger
  } else if (microseconds >= HIGHLIGHTING_CONFIG.SERVER_LATENCY_THRESHOLDS.MEDIUM) {
    return styles.warning
  } else if (microseconds < HIGHLIGHTING_CONFIG.SERVER_LATENCY_THRESHOLDS.FAST) {
    return styles.success
  }
  
  return ''
}

export const getDurationClass = (microseconds?: number): string => {
  if (!microseconds) return ''
  
  const durationMs = microseconds / 1000
  
  if (durationMs > HIGHLIGHTING_CONFIG.DURATION_THRESHOLDS.SLOW) {
    return styles.danger
  } else if (durationMs >= HIGHLIGHTING_CONFIG.DURATION_THRESHOLDS.MEDIUM) {
    return styles.warning
  } else if (durationMs < HIGHLIGHTING_CONFIG.DURATION_THRESHOLDS.FAST) {
    return styles.success
  }
  
  return ''
}

export const getSizeClass = (bytes?: number): string => {
  if (!bytes) return ''
  
  const kb = bytes / 1024
  if (kb >= HIGHLIGHTING_CONFIG.SIZE_THRESHOLDS.LARGE) {
    return styles.danger
  } else if (kb >= HIGHLIGHTING_CONFIG.SIZE_THRESHOLDS.MEDIUM) {
    return styles.warning
  } else if (kb <= HIGHLIGHTING_CONFIG.SIZE_THRESHOLDS.SMALL) {
    return styles.success
  }
  
  return ''
}

export const getQueueAnalysisIcon = (analysis?: QueueAnalysis): string => {
  if (!analysis) return ''
  
  switch (analysis.reason) {
    case 'connection_limit': return '🔗'
    case 'priority_queue': return '⚡'
    case 'resource_contention': return '🏁'
    default: return '❓'
  }
}

export const getCDNIcon = (analysis?: CDNAnalysis): string => {
  if (!analysis) return ''
  
  switch (analysis.provider) {
    case 'cloudflare': return '☁️'
    case 'akamai': return '🌐' 
    case 'aws_cloudfront': return '☁️'
    case 'fastly': return '⚡'
    case 'azure_cdn': return '☁️'
    case 'google_cdn': return '🔍'
    case 'cdn77': return '🌐'
    case 'keycdn': return '🔑'
    default: return '🌍'
  }
}

export const getCDNDisplayName = (provider: CDNAnalysis['provider']): string => {
  switch (provider) {
    case 'cloudflare': return 'Cloudflare'
    case 'akamai': return 'Akamai'
    case 'aws_cloudfront': return 'CloudFront'
    case 'fastly': return 'Fastly'
    case 'azure_cdn': return 'Azure CDN'
    case 'google_cdn': return 'Google CDN'
    case 'cdn77': return 'CDN77'
    case 'keycdn': return 'KeyCDN'
    default: return 'Unknown CDN'
  }
}

export const getPriorityIcon = (priority?: string): string => {
  if (!priority) return '➖'
  
  const upperPriority = priority.toUpperCase()
  switch (upperPriority) {
    case 'VERYHIGH': return '🔥' // Very high priority - fire/urgent
    case 'HIGH': return '🔺' // High priority - red triangle up
    case 'MEDIUM': return '🟡' // Medium priority - yellow circle
    case 'LOW': return '🔹' // Low priority - red triangle down
    case 'VERYLOW': return '🐢' // Very low priority - small blue diamond
    default: return '➖' // Unknown/empty priority
  }
}