import 'dart:io' as io;

import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:universal_html/html.dart';

class StorageService {
  static late String appDocsDir;
  static late String appTempsDir;
  static const FlutterSecureStorage _storage = FlutterSecureStorage();

  static Future<void> createFile({
    required Uint8List bytes,
    required String subDirectory,
    required String name,
  }) async {
    final dir = io.Directory('$appDocsDir/$subDirectory');
    if (!await dir.exists()) {
      await dir.create(recursive: true);
    }
    final file = await io.File(
      '$appDocsDir/$subDirectory/$name',
    ).create(recursive: true);
    await file.writeAsBytes(bytes);
  }

  static Future<void> setValue({
    required String key,
    required dynamic value,
  }) async {
    await _storage.write(key: key, value: value.toString());
  }

  static Future getValue({required String key}) async {
    final result = await _storage.read(key: key);
    return result;
  }

  static Future<void> delete({required String key}) async {
    _storage.delete(key: key);
  }

  static Storage get webStorage => window.localStorage;
}
