import { PrefUtils } from '../utils/PrefUtils';
import { JSON } from '@kit.ArkTS';

/**
 * 保存到相册的媒体列表
 */
export class LocalMediaManager {
  static add(uri: string) {
    let array = PrefUtils.getStringArray('local_record')
    if (!array.includes(uri)) {
      array.push(uri)
    }
    PrefUtils.put('local_record', JSON.stringify(array))
  }

  static delete(uri: string) {
    let array = PrefUtils.getStringArray('local_record')
    if (array.includes(uri)) {
      array.splice(array.indexOf(uri), 1)
    }
    PrefUtils.put('local_record', JSON.stringify(array))
  }

  static getAllVideos(): Array<string> {
    let array = PrefUtils.getStringArray('local_record')
    return array.filter(item => item.endsWith('.mp4'))
  }

  static deleteAllVideos() {
    let imageArray = LocalMediaManager.getAllImages()
    let audioArray = LocalMediaManager.getAllAudios()
    let newArray = imageArray.concat(audioArray)
    PrefUtils.put('local_record', JSON.stringify(newArray))
  }

  static getAllAudios(): Array<string> {
    let array = PrefUtils.getStringArray('local_record')
    return array.filter(item => item.endsWith('.mp3'))
  }

  static deleteAllAudios() {
    let videoArray = LocalMediaManager.getAllVideos()
    let audioArray = LocalMediaManager.getAllImages()
    let newArray = videoArray.concat(audioArray)
    PrefUtils.put('local_record', JSON.stringify(newArray))
  }

  static getAllImages(): Array<string> {
    let array = PrefUtils.getStringArray('local_record')
    return array.filter(item => item.endsWith('.jpeg') || item.endsWith('.jpg') || item.endsWith('.png'))
  }

  static deleteAllImages() {
    let videoArray = LocalMediaManager.getAllVideos()
    let audioArray = LocalMediaManager.getAllAudios()
    let newArray = videoArray.concat(audioArray)
    PrefUtils.put('local_record', JSON.stringify(newArray))
  }

  static getAll(): Array<string> {
    return PrefUtils.getStringArray('local_record')
  }

  static deleteAll() {
    PrefUtils.remove('local_record')
  }
}