import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';


import 'package:path_provider/path_provider.dart';

class FileProcess {
  static bool isFolderCreated = false;
  static Directory? directory;

  static Future checkDocumentFolder() async {
    try {
      if (!isFolderCreated) {
        directory = await getApplicationDocumentsDirectory();
        await directory!.exists().then((value) {
          if (value) directory!.create();
          isFolderCreated = true;
        });
      }
    } catch (e) {
      print(e.toString());
    }
  }

  static void openFile(String fileName) {
    String dir = directory!.path + "/$fileName.pdf";
    // OpenFile.open(dir);
  }

  static Future<File> downloadFile(String base64Content, String fileName) async {
    Uint8List bytes = base64.decode(base64Content);
    await checkDocumentFolder();
    String dir = directory!.path + "/" + fileName + ".pdf";
    File file = File(dir);
    if (!file.existsSync()) file.create();
    await file.writeAsBytes(bytes);
    return file;
  }
}
