import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:vitalapp/managers/interfaces/cache.dart';

class CacheManager implements ICacheManager {
  /// 清理文件缓存（全部）
  @override
  void clearApplicationCache() async {
    Directory docDirectory = await getApplicationDocumentsDirectory();
    Directory tempDirectory = await getTemporaryDirectory();

    if (docDirectory.existsSync()) {
      await deleteDirectory(docDirectory);
    }

    if (tempDirectory.existsSync()) {
      await deleteDirectory(tempDirectory);
    }
  }

  @override
  void clearApplicationImageCache() async {
    Directory tempDirectory = await getTemporaryDirectory();
    if (tempDirectory.existsSync()) {
      await deleteImageDirectory(tempDirectory);
    }
  }

  /// 清理单个文件
  @override
  Future<void> deleteDirectory(FileSystemEntity file) async {
    if (file is Directory) {
      final List<FileSystemEntity> children = file.listSync();
      for (final FileSystemEntity child in children) {
        await deleteDirectory(child);
        await child.delete();
      }
    }
  }

  /// 递归清理图像
  Future<void> deleteImageDirectory(FileSystemEntity file) async {
    if (file is File) {
      if (file.path.endsWith('.png') ||
          file.path.endsWith('.jpg') ||
          file.path.endsWith('.jpeg') ||
          file.path.endsWith('.mp4') ||
          file.path.endsWith('.avi') ||
          file.path.endsWith('.mov')) {
        await file.delete();
      }
    }
    if (file is Directory) {
      final List<FileSystemEntity> children = file.listSync();
      for (final FileSystemEntity child in children) {
        await deleteImageDirectory(child);
      }
    }
  }

  /// 获取缓存文件大小
  @override
  Future<double> getCacheSize() async {
    //获取文件夹
    Directory docDirectory = await getApplicationDocumentsDirectory();
    Directory tempDirectory = await getTemporaryDirectory();

    double size = 0;

    if (docDirectory.existsSync()) {
      size += await getTotalSizeOfFilesInDir(docDirectory);
    }
    if (tempDirectory.existsSync()) {
      size += await getTotalSizeOfFilesInDir(tempDirectory);
    }
    return size;
  }

  @override
  Future<double> getImageCacheSize() async {
    //获取文件夹
    Directory docDirectory = await getApplicationDocumentsDirectory();
    Directory tempDirectory = await getTemporaryDirectory();

    double size = 0;

    if (docDirectory.existsSync()) {
      size += await getTotalSizeOfImageInDir(docDirectory);
    }
    if (tempDirectory.existsSync()) {
      size += await getTotalSizeOfImageInDir(tempDirectory);
    }
    return size;
  }

  static Future<double> getTotalSizeOfFilesInDir(
      final FileSystemEntity file) async {
    if (file is File) {
      int length = await file.length();
      return double.parse(length.toString());
    }
    if (file is Directory) {
      final List<FileSystemEntity> children = file.listSync();
      double total = 0;
      for (final FileSystemEntity child in children) {
        total += await getTotalSizeOfFilesInDir(child);
      }
      return total;
    }
    return 0;
  }

  static Future<double> getTotalSizeOfImageInDir(
      final FileSystemEntity file) async {
    if (file is File) {
      if (file.path.endsWith('.png') ||
          file.path.endsWith('.jpg') ||
          file.path.endsWith('.jpeg') ||
          file.path.endsWith('.mp4') ||
          file.path.endsWith('.avi') ||
          file.path.endsWith('.mov')) {
        int length = await file.length();
        return double.parse(length.toString());
      } else {
        return 0;
      }
    }
    if (file is Directory) {
      final List<FileSystemEntity> children = file.listSync();
      double total = 0;
      for (final FileSystemEntity child in children) {
        total += await getTotalSizeOfImageInDir(child);
      }
      return total;
    }
    return 0;
  }
}
