import 'package:flutter/material.dart';

import '../../../../api/marianumcloud/webdav/queries/list_files/cacheable_file.dart';

/// Best-effort mapping from a file's MIME type / extension to a Material
/// icon that visually hints at its kind. Always returns a sensible fallback.
IconData iconForFile(CacheableFile file) {
  if (file.isDirectory) return Icons.folder;
  final mime = file.mimeType?.toLowerCase();
  if (mime != null && mime.isNotEmpty) {
    final byMime = _iconForMime(mime);
    if (byMime != null) return byMime;
  }
  final ext = _extensionOf(file.name);
  if (ext != null) {
    final byExt = _extensionIcons[ext];
    if (byExt != null) return byExt;
  }
  return Icons.insert_drive_file_outlined;
}

/// Icon for a bare file name (no [CacheableFile] / MIME available), e.g. in the
/// downloads overview. Falls back to a generic document icon.
IconData iconForFileName(String name) {
  final ext = _extensionOf(name);
  if (ext != null) {
    final byExt = _extensionIcons[ext];
    if (byExt != null) return byExt;
  }
  return Icons.insert_drive_file_outlined;
}

String? _extensionOf(String name) {
  final dot = name.lastIndexOf('.');
  if (dot <= 0 || dot == name.length - 1) return null;
  return name.substring(dot + 1).toLowerCase();
}

IconData? _iconForMime(String mime) {
  // Major media types first — these cover the long tail of variants.
  if (mime.startsWith('image/')) return Icons.image_outlined;
  if (mime.startsWith('video/')) return Icons.movie_outlined;
  if (mime.startsWith('audio/')) return Icons.audiotrack;
  if (mime.startsWith('font/')) return Icons.font_download_outlined;
  if (mime.startsWith('text/x-') ||
      mime.startsWith('text/javascript') ||
      mime.startsWith('text/css') ||
      mime.startsWith('text/html')) {
    return Icons.code;
  }
  if (mime.startsWith('text/')) return Icons.article_outlined;

  // Specific application/* types.
  const map = <String, IconData>{
    'application/pdf': Icons.picture_as_pdf_outlined,

    'application/msword': Icons.description_outlined,
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
        Icons.description_outlined,
    'application/vnd.oasis.opendocument.text': Icons.description_outlined,
    'application/rtf': Icons.description_outlined,

    'application/vnd.ms-excel': Icons.table_chart_outlined,
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
        Icons.table_chart_outlined,
    'application/vnd.oasis.opendocument.spreadsheet':
        Icons.table_chart_outlined,
    'application/vnd.ms-excel.sheet.macroenabled.12':
        Icons.table_chart_outlined,

    'application/vnd.ms-powerpoint': Icons.slideshow_outlined,
    'application/vnd.openxmlformats-officedocument.presentationml.presentation':
        Icons.slideshow_outlined,
    'application/vnd.oasis.opendocument.presentation': Icons.slideshow_outlined,

    'application/zip': Icons.folder_zip_outlined,
    'application/x-zip-compressed': Icons.folder_zip_outlined,
    'application/x-rar-compressed': Icons.folder_zip_outlined,
    'application/vnd.rar': Icons.folder_zip_outlined,
    'application/x-7z-compressed': Icons.folder_zip_outlined,
    'application/x-tar': Icons.folder_zip_outlined,
    'application/gzip': Icons.folder_zip_outlined,
    'application/x-bzip2': Icons.folder_zip_outlined,
    'application/x-xz': Icons.folder_zip_outlined,
    'application/zstd': Icons.folder_zip_outlined,

    'application/json': Icons.code,
    'application/ld+json': Icons.code,
    'application/xml': Icons.code,
    'application/x-yaml': Icons.code,
    'application/javascript': Icons.code,
    'application/x-sh': Icons.terminal,

    'text/calendar': Icons.calendar_month_outlined,
    'text/vcard': Icons.contact_page_outlined,

    'application/epub+zip': Icons.menu_book_outlined,
    'application/x-mobipocket-ebook': Icons.menu_book_outlined,

    'application/x-msdownload': Icons.terminal,
    'application/x-msi': Icons.terminal,
    'application/x-apple-diskimage': Icons.album_outlined,
    'application/vnd.android.package-archive': Icons.android_outlined,
    'application/octet-stream': Icons.insert_drive_file_outlined,

    'application/x-sqlite3': Icons.storage_outlined,
    'application/vnd.sqlite3': Icons.storage_outlined,

    'model/gltf-binary': Icons.view_in_ar_outlined,
    'model/gltf+json': Icons.view_in_ar_outlined,
    'model/stl': Icons.view_in_ar_outlined,
    'model/obj': Icons.view_in_ar_outlined,
  };
  return map[mime];
}

const _extensionIcons = <String, IconData>{
  'jpg': Icons.image_outlined, 'jpeg': Icons.image_outlined,
  'png': Icons.image_outlined, 'gif': Icons.image_outlined,
  'webp': Icons.image_outlined, 'bmp': Icons.image_outlined,
  'tif': Icons.image_outlined, 'tiff': Icons.image_outlined,
  'heic': Icons.image_outlined, 'heif': Icons.image_outlined,
  'avif': Icons.image_outlined, 'ico': Icons.image_outlined,
  'raw': Icons.image_outlined, 'cr2': Icons.image_outlined,
  'nef': Icons.image_outlined, 'arw': Icons.image_outlined,
  'svg': Icons.gesture_outlined, 'eps': Icons.gesture_outlined,
  'ai': Icons.gesture_outlined,

  'mp4': Icons.movie_outlined, 'm4v': Icons.movie_outlined,
  'mov': Icons.movie_outlined, 'mkv': Icons.movie_outlined,
  'avi': Icons.movie_outlined, 'webm': Icons.movie_outlined,
  'flv': Icons.movie_outlined, 'wmv': Icons.movie_outlined,
  '3gp': Icons.movie_outlined, 'mpg': Icons.movie_outlined,
  'mpeg': Icons.movie_outlined, 'ogv': Icons.movie_outlined,

  'mp3': Icons.audiotrack, 'm4a': Icons.audiotrack, 'aac': Icons.audiotrack,
  'wav': Icons.audiotrack, 'flac': Icons.audiotrack, 'ogg': Icons.audiotrack,
  'oga': Icons.audiotrack, 'opus': Icons.audiotrack, 'wma': Icons.audiotrack,
  'aiff': Icons.audiotrack, 'aif': Icons.audiotrack,

  // Music notation
  'mscz': Icons.music_note, 'mscx': Icons.music_note,
  'musicxml': Icons.music_note, 'mxl': Icons.music_note,
  'midi': Icons.music_note, 'mid': Icons.music_note,

  'pdf': Icons.picture_as_pdf_outlined,

  'doc': Icons.description_outlined, 'docx': Icons.description_outlined,
  'odt': Icons.description_outlined, 'rtf': Icons.description_outlined,
  'pages': Icons.description_outlined,

  'xls': Icons.table_chart_outlined, 'xlsx': Icons.table_chart_outlined,
  'xlsm': Icons.table_chart_outlined, 'ods': Icons.table_chart_outlined,
  'csv': Icons.table_chart_outlined, 'tsv': Icons.table_chart_outlined,
  'numbers': Icons.table_chart_outlined,

  'ppt': Icons.slideshow_outlined, 'pptx': Icons.slideshow_outlined,
  'pps': Icons.slideshow_outlined, 'odp': Icons.slideshow_outlined,
  'key': Icons.slideshow_outlined,

  'txt': Icons.article_outlined, 'md': Icons.article_outlined,
  'markdown': Icons.article_outlined, 'log': Icons.article_outlined,
  'rst': Icons.article_outlined,

  'html': Icons.code, 'htm': Icons.code, 'xhtml': Icons.code,
  'css': Icons.code, 'scss': Icons.code, 'sass': Icons.code, 'less': Icons.code,
  'js': Icons.code, 'mjs': Icons.code, 'cjs': Icons.code,
  'ts': Icons.code, 'jsx': Icons.code, 'tsx': Icons.code,
  'dart': Icons.code, 'java': Icons.code, 'kt': Icons.code, 'kts': Icons.code,
  'py': Icons.code, 'rb': Icons.code, 'go': Icons.code, 'rs': Icons.code,
  'c': Icons.code, 'cpp': Icons.code, 'cc': Icons.code, 'cxx': Icons.code,
  'h': Icons.code, 'hpp': Icons.code, 'cs': Icons.code, 'm': Icons.code,
  'mm': Icons.code, 'php': Icons.code, 'pl': Icons.code, 'lua': Icons.code,
  'r': Icons.code, 'swift': Icons.code, 'scala': Icons.code, 'groovy': Icons.code,
  'sql': Icons.code, 'graphql': Icons.code, 'gql': Icons.code,
  'json': Icons.code, 'json5': Icons.code, 'xml': Icons.code,
  'yaml': Icons.code, 'yml': Icons.code,

  'sh': Icons.terminal, 'bash': Icons.terminal, 'zsh': Icons.terminal,
  'fish': Icons.terminal, 'ps1': Icons.terminal, 'bat': Icons.terminal,
  'cmd': Icons.terminal,

  'zip': Icons.folder_zip_outlined, 'rar': Icons.folder_zip_outlined,
  '7z': Icons.folder_zip_outlined, 'tar': Icons.folder_zip_outlined,
  'gz': Icons.folder_zip_outlined, 'bz2': Icons.folder_zip_outlined,
  'xz': Icons.folder_zip_outlined, 'tgz': Icons.folder_zip_outlined,
  'zst': Icons.folder_zip_outlined,

  // Config / settings (the "gear" cases)
  'ini': Icons.settings_outlined, 'cfg': Icons.settings_outlined,
  'conf': Icons.settings_outlined, 'env': Icons.settings_outlined,
  'toml': Icons.settings_outlined, 'plist': Icons.settings_outlined,
  'properties': Icons.settings_outlined,
  'editorconfig': Icons.settings_outlined,
  'gitignore': Icons.settings_outlined,
  'gitattributes': Icons.settings_outlined,
  'dockerignore': Icons.settings_outlined,
  'dockerfile': Icons.settings_outlined,

  'ttf': Icons.font_download_outlined, 'otf': Icons.font_download_outlined,
  'woff': Icons.font_download_outlined, 'woff2': Icons.font_download_outlined,
  'eot': Icons.font_download_outlined,

  'ics': Icons.calendar_month_outlined, 'ical': Icons.calendar_month_outlined,
  'vcf': Icons.contact_page_outlined, 'vcard': Icons.contact_page_outlined,

  'epub': Icons.menu_book_outlined, 'mobi': Icons.menu_book_outlined,
  'azw': Icons.menu_book_outlined, 'azw3': Icons.menu_book_outlined,

  'stl': Icons.view_in_ar_outlined, 'obj': Icons.view_in_ar_outlined,
  'fbx': Icons.view_in_ar_outlined, 'blend': Icons.view_in_ar_outlined,
  'glb': Icons.view_in_ar_outlined, 'gltf': Icons.view_in_ar_outlined,
  '3ds': Icons.view_in_ar_outlined,

  'exe': Icons.terminal, 'msi': Icons.terminal,
  'app': Icons.terminal, 'deb': Icons.terminal, 'rpm': Icons.terminal,
  'apk': Icons.android_outlined, 'ipa': Icons.terminal,
  'appimage': Icons.terminal,

  // Disc images
  'iso': Icons.album_outlined, 'img': Icons.album_outlined,
  'dmg': Icons.album_outlined,

  'db': Icons.storage_outlined, 'sqlite': Icons.storage_outlined,
  'sqlite3': Icons.storage_outlined,
};
