// ignore_for_file: curly_braces_in_flow_control_structures, prefer_is_empty import 'dart:developer'; import 'package:hub/backend/api_requests/api_calls.dart'; import 'package:hub/shared/helpers/base_storage.dart'; import 'package:hub/shared/helpers/database_storage.dart'; import 'dart:convert'; import 'package:hub/shared/helpers/storage_helper.dart'; import 'package:sqflite/sqflite.dart'; class LicenseService implements BaseStorage { static final LicenseService _instance = LicenseService._internal(); factory LicenseService() => _instance; LicenseService._internal(); static const String tableLicense = 'license'; static String get createTableQuery => ''' CREATE TABLE $tableLicense ( key TEXT UNIQUE, display TEXT, expirationDate TEXT, startDate TEXT, quantity TEXT ); '''; static Future setupLicense() async { log('initLicense()'); for (var element in LicenseStorageKey.values.map((e) => e.value)) { await DatabaseStorage.database.insert( tableLicense, { 'key': element, 'display': 'VISIVEL', 'expirationDate': '', 'startDate': '', 'quantity': '', }, conflictAlgorithm: ConflictAlgorithm.ignore, ); } } @override Future init() async { await DatabaseStorage.instance.init(); } @override Future get(String key) async { var response = await DatabaseStorage.database.query(tableLicense, where: 'key = ?', whereArgs: [key]); if (response.isEmpty) { return null; } return response.first['value'].toString(); } @override Future set(String key, String value) async { log('setLicense($key, $value)'); Map valueMap = jsonDecode(value); log('String to Map: $value to $valueMap'); await DatabaseStorage.database.insert( tableLicense, { 'key': key, 'display': valueMap['display'], 'expirationDate': valueMap['expirationDate'], 'startDate': valueMap['startDate'], 'quantity': valueMap['quantity'], }, conflictAlgorithm: ConflictAlgorithm.replace); } @override Future delete(String key) async { await DatabaseStorage.database.delete(tableLicense, where: 'key = ?', whereArgs: [key]); } @override Future clearAll() async { await DatabaseStorage.database.delete(tableLicense); } static Future initLicenseService() async { // for (var element in LicenseStorageKey.values.map((e) => e.value)) { // await SQLiteStorage.database.insert( // SQLiteStorage.tableLicense, // { // 'key': element, // 'display': 'VISIVEL', // 'expirationDate': '', // 'startDate': '', // 'quantity': '', // }, // conflictAlgorithm: ConflictAlgorithm.ignore, // ); // } } static Future processLicense() async {} static Future fetchLicenses() async { try { log('Obtendo licenças...'); final response = await PhpGroup.getLicense(); final dynamic responseBody = response.jsonBody; log('Licenças obtidas: $responseBody'); if (response.jsonBody is! List) { late final String error; if (response.jsonBody is Map) error = response.jsonBody['error_msg']; else error = 'Erro desconhecido'; throw Exception('Erro ao consultar licenças: $error'); } if (responseBody == []) { await setupLicense(); return true; } for (var element in responseBody) { if (licenseContainsKey(element['key'])) { _saveModule(element); } } return true; } catch (e, s) { log('Erro ao obter licença', error: e, stackTrace: s); await setupLicense(); return true; } } static bool licenseContainsKey(final String key) { return LicenseStorageKey.values.map((e) => e.value).toList().contains(key); } static Future _saveModule(final dynamic body) async { if (body is Map) log('Salvando módulo: ${body.toString()}'); // if (body is Map) await StorageHelper().s(body['key'], ''); if (body is Map) await StorageHelper().s(body['key'], jsonEncode(body)); // StorageHelper.getInstance(Storage.SQLiteStorage).set(key, value); } static Future updateLicense(final dynamic body) async {} static Future catchLicense() async {} }