import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_livestock/data/model/local/live_tmp/livestock_local_model.dart';
import 'package:rasadyar_livestock/data/model/response/live_tmp/livestock_model.dart';
import 'package:rasadyar_livestock/data/utils/mapper.dart';

class LiveStockStorageService extends GetxService {
  final String _liveStockBoxName = 'LiveStockBox';
  late IsolatedBox<LivestockLocalModel> _LiveStockbox;


  @override
  void onInit() {
    super.onInit();
    wLog('LiveStockStorageService onInit');
    IsolatedHive.openBox<LivestockLocalModel>(_liveStockBoxName).then((value) {
      _LiveStockbox = value;
    });
  }

  Future<void> saveLiveStockData(LivestockData livestockData) async {
    LivestockLocalModel tmp = livestockData.toLocal();
    await _LiveStockbox.add(tmp);
  }

  Future<void> saveBulkLiveStockData(Iterable<LivestockData> livesList) async {
    var tmp = livesList.map((e) => e.toLocal()).toList();
    await _LiveStockbox.addAll(tmp);
  }

  Future<List<LivestockData>> getLiveStockData() async {
    var liveLocationList = await _LiveStockbox.values;
    var res = liveLocationList.map((e) => e.toData()).toList();
    return res;
  }

  Future<int> deleteLiveBox() async {
    return await _LiveStockbox.clear();
  }
}
