
import 'package:nextcloud/nextcloud.dart';

import '../../webdavApi.dart';
import 'cacheableFile.dart';
import 'listFilesParams.dart';
import 'listFilesResponse.dart';

class ListFiles extends WebdavApi<ListFilesParams> {
  ListFilesParams params;

  ListFiles(this.params) : super(params);

  @override
  Future<ListFilesResponse> run() async {
    var davFiles = (await (await WebdavApi.webdav).propfind(PathUri.parse(params.path))).toWebDavFiles();
    var files = davFiles.map(CacheableFile.fromDavFile).toSet();

    // webdav handles subdirectories wrong, this is a fix
    // currently this fix is not needed anymore
    // if(EndpointData().getEndpointMode() == EndpointMode.stage) {
    //   files = files.map((e) { // somehow
    //     e.path = e.path.split("mobile/cloud/remote.php/webdav")[1];
    //     return e;
    //   }).toSet();
    // }

    // somehow the current working folder is also listed, it is filtered here.
    files.removeWhere((element) => element.path == '/${params.path}/' || element.path == '/');

    return ListFilesResponse(files);
  }
}
