import { common } from "@kit.AbilityKit";
import { OpenFiles } from "./OpenFiles";
import { filePreview } from "@kit.PreviewKit";
import { fileUri } from "@kit.CoreFileKit";

const MineType: Record<string, string> = {
  'txt': 'text/plain',
  'cpp': 'text/x-c++src',
  'c': 'text/x-csrc',
  'h': 'text/x-chdr',
  'java': 'text/x-java',
  'xhtml': 'application/xhtml+xml',
  'xml': 'text/xml',
  'html': 'text/html',
  'htm': 'text/html',
  'jpg': 'image/jpeg',
  'png': 'image/png',
  'gif': 'image/gif',
  'webp': 'image/webp',
  'bmp': 'image/bmp',
  'svg': 'image/svg+xml',
  'm4a': 'audio/mp4a-latm',
  'aac': 'audio/aac',
  'mp3': 'audio/mpeg',
  'ogg': 'audio/ogg',
  'wav': 'audio/x-wav',
  'mp4': 'video/mp4',
  'mkv': 'video/x-matroska',
  'ts': 'video/mp2ts',
  'pdf': 'application/pdf',
  'doc': 'application/msword',
  'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  'xls': 'application/vnd.ms-excel',
  'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  'ppt': 'application/vnd.ms-powerpoint',
  'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  'csv': 'text/comma-separated-values',
}

export class FileMd5Utils {
  static openFile(context: common.UIAbilityContext, fileName: string, filePath: string) {
    if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.png', '.gif', '.jpg', '.jpeg', '.bmp'])) {
      context.startAbility(OpenFiles.getImageFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.htm', '.html', '.php', '.jsp'])) {
      context.startAbility(OpenFiles.getHtmlFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.jar', '.zip', '.rar', '.gz', '.img'])) {
      context.startAbility(OpenFiles.getApkFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.mp3', '.wav', '.ogg', '.midi'])) {
      context.startAbility(OpenFiles.getAudioFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.mp4', '.rmvb', '.avi', '.flv'])) {
      context.startAbility(OpenFiles.getVideoFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.txt','.java','.c','.cpp','.py','.xml','.json','.log'])) {
      context.startAbility(OpenFiles.getTextFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.pdf'])) {
      context.startAbility(OpenFiles.getPdfFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.doc', '.docx'])) {
      context.startAbility(OpenFiles.getWordFileWant(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.xls', '.xlsx'])) {
      context.startAbility(OpenFiles.getExcelFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else if (FileMd5Utils.checkEndsWithInStringArray(fileName, ['.ppt', '.pptx'])) {
      context.startAbility(OpenFiles.getPPTFileIntent(FileMd5Utils.getUriFromPath(filePath)))
    } else {
      return false;
    }
    return true;
  }

  /**
   * 检查文件后缀
   *
   * @param checkItsEnd
   * @param fileEndings
   * @return
   */
  static checkEndsWithInStringArray(checkItsEnd: string, fileEndings: string[]): boolean {
    for (let i = 0; i < fileEndings.length; i++) {
      if (checkItsEnd.endsWith(fileEndings[i])) {
        return true;
      }
    }
    return false;
  }

  /**
   * 获取文件的资源路径
   * @param filePath 文件路径
   * @returns file://开头的系统资源路径
   */
  static getUriFromPath(filePath: string) {
    if (filePath.startsWith('file:')) {
      return filePath;
    }
    return fileUri.getUriFromPath(filePath);
  }

  static openPreview(context: Context, fileName: string, filePath: string) {
    let fileInfo: filePreview.PreviewInfo = {
      title: fileName,
      uri: fileUri.getUriFromPath(filePath),
      mimeType: FileMd5Utils.getMimeType(fileName)
    };

    return filePreview.openPreview(context, fileInfo);
  }

  static getMimeType(fileName: string) {
    const fileType: string = fileName.substring(fileName.lastIndexOf('.') + 1)
    return MineType[fileType] || ''
  }
}