import 'dart:io';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:wallpaper/utils/local_img_manager.dart';
part 'dbmanager.g.dart';

@HiveType(typeId: 0)
class LocalImageInfo extends HiveObject {
  @HiveField(0)
  String? localUrl;

  @HiveField(1)
  DateTime? date;
  
  // LocalImageInfo({required this.localUrl,required this.date});

}

class DBManager {
  static late Box<LocalImageInfo> box;
  static late String localDir;

  static Future<void> initAdapter()async {
    // DBManager.localDir = await LocalImgManager.getImgLocalDir();
    await Hive.initFlutter();
    Hive.registerAdapter(LocalImageInfoAdapter());
    var downloadAlbums = await Hive.openBox<LocalImageInfo>('DownloadAlbums');
    DBManager.box = downloadAlbums;
  }

  static addImg(String localFileName) {
    var item = LocalImageInfo()
    ..localUrl = localFileName
    ..date = DateTime.now();
    DBManager.box.add(item);
    DBManager.box.flush();
  }

  static List<LocalImageInfo> allImgs() {
    var lis = DBManager.box.values.toList();
    
    return lis ;
  }

  static void deleteImg(LocalImageInfo item ) {
    DBManager.box.delete(item.key);
    DBManager.box.flush();
  }

   
}