import { systemShare } from '@kit.ShareKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
import { AppUtil, FileUtil } from '@pura/harmony-utils';

export class ShareManager {
  /**
   * 分享文件
   * @param path
   */
  static shareFile(path: string) {
    // 获取精准的utd类型
    let ext = ''
    let type = ''
    if (path.endsWith('.mp4')) {
      ext = '.mp4'
      type = utd.UniformDataType.VIDEO
    } else if (path.endsWith('.mp3')) {
      ext = '.mp3'
      type = utd.UniformDataType.AUDIO
    } else if (path.endsWith('.jpeg')) {
      ext = '.jpeg'
      type = utd.UniformDataType.IMAGE
    }
    let utdTypeId = utd.getUniformDataTypeByFilenameExtension(ext, type);
    let shareData: systemShare.SharedData = new systemShare.SharedData({
      utd: utdTypeId,
      uri: FileUtil.getUriFromPath(path)
    });
    let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
    controller.show(AppUtil.getContext(), {
      selectionMode: systemShare.SelectionMode.SINGLE,
      previewMode: systemShare.SharePreviewMode.DETAIL,
    }).then(() => {
      console.info('ShareController show success.');
    }).catch((error: BusinessError) => {
      console.error(`ShareController show error. code: ${error.code}, message: ${error.message}`);
    });
  }
}