<template>
  <!-- #ifdef APP -->
  <page-intro content="本页演示文件系统 API:递归获取目录、创建文件夹、读写/复制/重命名/删除文件、同步与异步接口等,操作结果在日志区展示。"></page-intro>
  <text>显示简易操作日志(可滚动查看),详细日志需真机运行查看</text><button size="mini" @click="data.log=''">清空日志</button>
  <scroll-view style="max-height: 300px;">
    <text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ data.log }}</text>
  </scroll-view>
  <scroll-view style="flex: 1;">
  <!-- #endif -->
    <!-- #ifdef MP -->
    <text style="margin: 2px; padding: 2px; border: 1px solid #000000;">{{ data.log }}</text>
    <!-- #endif -->
    <button class="btnstyle" type="primary" @tap="statFileInfoTest"
      id="btn-stat-file">递归获取目录files的Stats对象{{data.statFile}}</button>
    <button class="btnstyle" type="primary" @tap="mkdirTest" id="btn-mkdir">创建文件夹{{data.mkdirFile}}</button>
    <button class="btnstyle" type="primary" @tap="writeFileTest" id="btn-write-file">覆盖写入文件{{data.writeFile}}</button>
    <button class="btnstyle" type="primary" @tap="readDirTest" id="btn-read-dir">读取文件夹{{data.readDir}}</button>
    <button class="btnstyle" type="primary" @tap="readFileTest" id="btn-read-file">读取文件{{data.readFile}}</button>
    <button class="btnstyle" type="primary" @tap="copyFileTest"
      id="btn-copy-file">复制文件{{data.copyFromFile}}到{{data.copyToFile}}</button>
    <button class="btnstyle" type="primary" @tap="renameFileTest"
      id="btn-rename-file">重命名文件{{data.renameFromFile}}到{{data.renameToFile}}</button>
    <button class="btnstyle" type="primary" @tap="accessFileTest"
      id="btn-access-file">判断文件{{data.accessFile}}是否存在</button>
    <button class="btnstyle" type="primary" @tap="unlinkTest" id="btn-unlink-file">删除文件{{data.unlinkFile}}</button>
    <button class="btnstyle" type="primary" @tap="unlinkAllFileTest"
      id="btn-clear-file">删除文件夹{{data.rmDirFile}}下的所有文件</button>
    <button class="btnstyle" type="primary" @tap="rmdirTest" id="btn-remove-dir">删除文件夹{{data.rmDirFile}}</button>

    <!-- #ifndef MP-ALIPAY -->
    <button class="btnstyle" type="primary" @tap="statFileInfoSyncTest"
      id="btn-stat-file-sync">同步递归获取目录files的Stats对象{{data.statFile}}</button>
    <button class="btnstyle" type="primary" @tap="appendFileTest"
      id="btn-append-file">在文件{{data.readFile}}结尾追加内容</button>
    <button class="btnstyle" type="primary" @tap="appendFileSyncTest"
      id="btn-append-file-sync">同步在文件{{data.readFile}}结尾追加内容</button>
    <!-- #endif -->
    <button class="btnstyle" type="primary" @tap="writeFileSyncTest"
      id="btn-write-file-sync">同步覆盖写入文件{{data.writeFile}}</button>
    <button class="btnstyle" type="primary" @tap="readFileSyncTest"
      id="btn-read-file-sync">同步读取文件{{data.readFile}}</button>
    <button class="btnstyle" type="primary" @tap="unlinkSyncTest"
      id="btn-unlink-file-sync">同步删除文件{{data.unlinkFile}}</button>
    <button class="btnstyle" type="primary" @tap="mkdirSyncTest" id="btn-mkdir-sync">同步创建文件夹{{data.mkdirFile}}</button>
    <button class="btnstyle" type="primary" @tap="rmdirSyncTest"
      id="btn-remove-dir-sync">同步删除文件夹{{data.rmDirFile}}</button>
    <button class="btnstyle" type="primary" @tap="readDirSyncTest"
      id="btn-read-dir-sync">同步读取文件夹{{data.readDir}}</button>
    <!-- #ifndef MP-ALIPAY -->
    <button class="btnstyle" type="primary" @tap="accessFileSyncTest"
      id="btn-access-file-sync">同步判断文件{{data.accessFile}}是否存在</button>
    <button class="btnstyle" type="primary" @tap="renameFileSync"
      id="btn-rename-file-sync">同步重命名文件{{data.renameFromFile}}到{{data.renameToFile}}</button>
    <!-- #endif -->
    <button class="btnstyle" type="primary" @tap="copyFileSyncTest"
      id="btn-copy-file-sync">同步复制文件{{data.copyFromFile}}到{{data.copyToFile}}</button>
    <!-- #ifndef MP-ALIPAY -->
    <button class="btnstyle" type="primary" @tap="removeSavedFileTest" id="btn-remove-saved-file">删除已保存的本地文件</button>
    <button class="btnstyle" type="primary" @tap="getSavedFileListTest"
      id="btn-getsaved-filelist">获取该已保存的本地缓存文件列表</button>
    <!-- #endif -->
    <!-- #ifndef MP-ALIPAY -->
    <button class="btnstyle" type="primary" @tap="truncateFileTest"
      id="btn-truncate-file">对文件{{data.writeFile}}内容进行截断操作</button>
    <button class="btnstyle" type="primary" @tap="openFileTest" id="btn-open-file">打开文件{{data.readFile}},返回描述符</button>
    <button class="btnstyle" type="primary" @tap="openFileSyncTest('r',true)"
      id="btn-open-file-sync">同步打开文件{{data.readFile}},返回描述符</button>
    <button class="btnstyle" type="primary" @tap="closeTest" id="btn-close-file">通过文件描述符关闭文件{{data.readFile}}</button>
    <button class="btnstyle" type="primary" @tap="closeSyncTest"
      id="btn-close-file-sync">通过文件描述符同步关闭文件{{data.readFile}}</button>
    <button class="btnstyle" type="primary" @tap="writeTest" id="btn-write">通过文件描述符写入文件{{data.readFile}}</button>
    <button class="btnstyle" type="primary" @tap="writeSyncTest"
      id="btn-write-sync">同步通过文件描述符写入文件{{data.readFile}}</button>
    <button class="btnstyle" type="primary" @tap="fstatTest"
      id="btn-fstat-file">通过文件描述符获取{{data.statFile}}的状态信息</button>
    <button class="btnstyle" type="primary" @tap="fstatSyncTest"
      id="btn-fstat-file-sync">同步通过文件描述符获取{{data.statFile}}的状态信息</button>
    <button class="btnstyle" type="primary" @tap="ftruncateFileTest"
      id="btn-ftruncate-file">通过文件描述符对文件{{data.writeFile}}内容进行截断</button>
    <button class="btnstyle" type="primary" @tap="ftruncateFileSyncTest"
      id="btn-ftruncate-file-sync">同步通过文件描述符对文件{{data.writeFile}}内容进行截断</button>
    <button class="btnstyle" type="primary" @tap="testWriteReadFileBuffer" id="btn-writereadfile-buffer">写入/读取
      ArrayBuffer</button>
    <button class="btnstyle" type="primary" @tap="testWriteReadBuffer" id="btn-writeread-buffer">通过文件描述符写入/读取
      ArrayBuffer</button>
    <button class="btnstyle" type="primary" @tap="testWriteReadSyncBuffer" id="btn-writereadsync-buffer">通过文件描述符同步写入/读取
      ArrayBuffer</button>
    <button class="btnstyle" type="primary" @tap="testAppendFileBuffer" id="btn-appendfile-buffer">在文件末尾追加
      ArrayBuffer</button>
    <button class="btnstyle" type="primary" @tap="testAppendFileBufferSync" id="btn-appendfilesync-buffer">同步在文件末尾追加
      ArrayBuffer</button>
    <!-- #endif -->

    <!-- #ifdef APP -->
    <button class="btnstyle" type="primary" @tap="copyStaticToFilesTest"
      id="btn-copyStatic-file">从static目录复制文件到a目录</button>
    <button class="btnstyle" type="primary" @tap="saveFileTest" id="btn-save-file">保存临时文件到本地, filePath=null</button>
    <button class="btnstyle" type="primary" @tap="saveFileTest1" id="btn-save-file1">保存临时文件到本地,
      filePath=xxx/path.txt</button>
    <button class="btnstyle" type="primary" @tap="saveFileTest2" id="btn-save-file2">保存临时文件到本地,
      filePath=xxx/path</button>
    <button class="btnstyle" type="primary" @tap="saveFileTest3" id="btn-save-file3">保存临时文件到本地,
      filePath=xxx/path/</button>

    <button class="btnstyle" type="primary" @tap="saveFileAndReadFileTest" id="btn-save-file-read-file">saveFile成功后验证是否可以readFile</button>
    <button class="btnstyle" type="primary" @tap="saveFileSyncTest" id="btn-save-file-sync">同步保存临时文件到本地</button>
    <button class="btnstyle" type="primary" @tap="unzipFileTest" id="btn-unzip-file">解压文件</button>
    <!-- #ifdef VUE3-VAPOR -->
    <button class="btnstyle" type="primary" @tap="unzipFileSyncTest" id="btn-unzip-file-sync">同步解压文件</button>
    <!-- #endif -->
    <!-- #ifndef MP-ALIPAY -->
    <button class="btnstyle" type="primary" @tap="truncateFileSyncTest"
      id="btn-truncate-file-sync">同步对文件{{data.writeFile}}内容进行截断操作</button>
    <button class="btnstyle" type="primary" @tap="readCompressedFileTest"
      id="btn-compressed-file">读取指定压缩类型的本地文件内容</button>
    <button class="btnstyle" type="primary" @tap="readCompressedFileSyncTest"
      id="btn-compressed-file-sync">同步读取指定压缩类型的本地文件内容</button>
    <button class="btnstyle" type="primary" @tap="readZipEntry" id="btn-readzip-entry">读取压缩包内的文件</button>
    <!-- #endif -->
    <button class="btnstyle" type="primary" @tap="testWriteReadFileSyncBuffer" id="btn-writereadfilesync-buffer">同步写入/读取
      ArrayBuffer</button>
    <button class="btnstyle" type="primary" @tap="testReadFileEncoding('base64')">readFile(content://base64)</button>
    <button class="btnstyle" type="primary" @tap="testReadFileEncoding('utf-8')">readFile(content://utf-8)</button>
    <button class="btnstyle" type="primary" @tap="testReadFileEncoding('ascii')">readFile(content://ascii)</button>
    <button class="btnstyle" type="primary" @tap="testReadFileArrayBuffer()">readFile(content://arraybuffer)</button>
    <button class="btnstyle" type="primary"
      @tap="testReadFileSyncEncoding('base64')">readFileSync(content://base64)</button>
    <button class="btnstyle" type="primary"
      @tap="testReadFileSyncEncoding('utf-8')">readFileSync(content://utf-8)</button>
    <button class="btnstyle" type="primary"
      @tap="testReadFileSyncEncoding('ascii')">readFileSync(content://ascii)</button>
    <button class="btnstyle" type="primary"
      @tap="testReadFileSyncArrayBuffer()">readFileSync(content://arraybuffer)</button>
    <button class="btnstyle" type="primary" @tap="copyFileByContent()">copyFile(content://)</button>
    <button class="btnstyle" type="primary" @tap="copyFileSyncByContent()">copyFileSync(content://)</button>

    <!-- #endif -->
    <button class="btnstyle" type="primary" @tap="gotoExplore()">前往沙盒文件管理器</button>
    <button class="btnstyle" type="primary" @tap="gotoTestStatic()">前往Static文件测试</button>
    <button class="btnstyle" type="primary" @tap="gotoGetFileInfo()">前往getFileInfo摘要测试</button>
    <view style="height: 4px;"></view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script setup lang="ts">
  import { createFileSystemManagerState } from './file-system-manager-state.ts'
  import { createBasicAsyncActions } from './file-system-manager-basic-async-actions.ts'
  import { createBasicSyncActions } from './file-system-manager-basic-sync.ts'
  import { createStorageActions } from './file-system-manager-storage-actions.ts'
  import { createFdActions } from './file-system-manager-fd.ts'
  import { createBufferActions } from './file-system-manager-buffer.ts'
  import { createContentActions } from './file-system-manager-content.ts'

  const data = reactive(createFileSystemManagerState())

  const {
    statFileInfoTest,
    copyFileTest,
    renameFileTest,
    readDirTest,
    writeFileTest,
    readFileTest,
    rmdirTest,
    mkdirTest,
    accessFileTest,
    unlinkTest,
    unlinkAllFileTest,
    copyStaticToFilesTest,
    // #ifndef MP-ALIPAY
    appendFileTest,
    // #endif
  } = createBasicAsyncActions(data)

  const {
    writeFileSyncTest,
    readFileSyncTest,
    unlinkSyncTest,
    mkdirSyncTest,
    rmdirSyncTest,
    readDirSyncTest,
    copyFileSyncTest,
    // #ifndef MP-ALIPAY
    accessFileSyncTest,
    renameFileSync,
    appendFileSyncTest,
    statFileInfoSyncTest,
    // #endif
  } = createBasicSyncActions(data)

  const {
    unzipFileTest,
    unzipFileSyncTest,
    // #ifndef MP-ALIPAY
    saveFileTest,
    saveFileAndReadFileTest,
    saveFileTest1,
    saveFileTest2,
    saveFileTest3,
    saveFileSyncTest,
    getSavedFileListTest,
    truncateFileTest,
    truncateFileSyncTest,
    readCompressedFileTest,
    readCompressedFileSyncTest,
    removeSavedFileTest,
    // #endif
  } = createStorageActions(data, writeFileSyncTest)

  const {
    // #ifndef MP-ALIPAY
    openFileTest,
    openFileSyncTest,
    closeSyncTest,
    closeTest,
    writeTest,
    writeSyncTest,
    fstatTest,
    fstatSyncTest,
    ftruncateFileTest,
    ftruncateFileSyncTest,
    readZipEntry,
    // #endif
  } = createFdActions(data)

  const {
    testReadFileBuffer,
    testWriteReadFileBuffer,
    testReadFileSyncBuffer,
    testWriteReadFileSyncBuffer,
    // #ifndef MP-ALIPAY
    testWriteReadBuffer,
    testWriteReadSyncBuffer,
    testAppendFileBufferSync,
    testAppendFileBuffer,
    // #endif
  } = createBufferActions(
    data
    // #ifndef MP-ALIPAY
    , openFileSyncTest
    // #endif
  )

  const {
    // #ifdef APP
    testReadFileEncoding,
    testReadFileArrayBuffer,
    testReadFileSyncEncoding,
    testReadFileSyncArrayBuffer,
    copyFileByContent,
    copyFileSyncByContent,
    // #endif
    // #ifndef MP-ALIPAY
    testOpenFlagWrite,
    testWriteLongString,
    // #endif
  } = createContentActions(data)

  const gotoExplore = () => {
    uni.navigateTo({
      url: "/pages/API/get-file-system-manager/filemanage"
    })
  }
  const gotoTestStatic = () => {
    uni.navigateTo({
      url: "/pages/API/get-file-system-manager/testStatic"
    })
  }
  const gotoGetFileInfo = () => {
    uni.navigateTo({
      url: "/pages/API/get-file-system-manager/getFileInfo"
    })
  }

  defineExpose({
    data,
    // #ifndef MP-ALIPAY
    testOpenFlagWrite,
    testWriteLongString
    // #endif
  })
</script>

<style>
  .btnstyle {
    margin: 4px;
  }
</style>