import 'package:hub/shared/helpers/base_storage.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite/sqlite_api.dart';
import 'package:path/path.dart';

class SQLiteStorage implements BaseStorage {
  SQLiteStorage._();

  static final String _dbName = 'database.db';
  static final int _dbVersion = 1;

  static const String tableKeychain = 'keychain';

  static final SQLiteStorage instance = SQLiteStorage._();

  late final _database;

  @override
  Future<void> init() async {
    _database = await openDatabase(
      join(await getDatabasesPath(), _dbName),
      version: _dbVersion,
      onCreate: _onCreate,
      onUpgrade: _onUpdate,
      onDowngrade: _onDowngrade,
    );
    // await _onInitLocalVariables(_database);
  }

  _onCreate(Database database, int version) async {
    await database.execute(_tableKeychain);
  }

  _onUpdate(Database database, int oldVersion, int newVersion) async {}
  _onDowngrade(Database database, int oldVersion, int newVersion) async {}

  String get _tableKeychain => '''
    CREATE TABLE keychain (
      key TEXT UNIQUE,
      value TEXT,
      type TEXT,
      updateAt TEXT,
      resolvedAt TEXT,
      createdAt TEXT
    );
  ''';

  List<Map<String, dynamic>> get _localVariables => [
        {'key': SQLiteStorageKey.devUUID.value, 'value': '', 'type': 'user'},
        {'key': SQLiteStorageKey.userUUID.value, 'value': '', 'type': 'user'},
        {'key': SQLiteStorageKey.userDevUUID.value, 'value': '', 'type': 'user'},
        {'key': SQLiteStorageKey.status.value, 'value': '', 'type': 'user'},
        {'key': SQLiteStorageKey.userName.value, 'value': '', 'type': 'user'},
        {'key': SQLiteStorageKey.clientUUID.value, 'value': '', 'type': 'local'},
        {'key': SQLiteStorageKey.ownerUUID.value, 'value': '', 'type': 'local'},
        {'key': SQLiteStorageKey.clientName.value, 'value': '', 'type': 'local'},
        {'key': SQLiteStorageKey.whatsapp.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.provisional.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.pets.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.local.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.notify.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.fingerprint.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.access.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.panic.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.person.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.requestOSNotification.value, 'value': 'false', 'type': 'util'},
        {'key': SQLiteStorageKey.petAmount.value, 'value': '', 'type': 'local'},
      ];

  @override
  Future<String?> get(String key) async {
    var response = await _database.query('keychain', where: 'key = ?', whereArgs: [key]);
    if (response.isEmpty) {
      return null;
    }
    return response.first['value'];
  }

  @override
  Future<void> set(String key, String value) async {
    var date = DateTime.now().toIso8601String();
    await _database.insert(
        tableKeychain,
        {
          'key': key,
          'value': value,
          'type': 'local',
          'updateAt': date,
          'resolvedAt': date,
          'createdAt': date,
        },
        conflictAlgorithm: ConflictAlgorithm.replace);
  }

  @override
  Future<void> delete(String key) async {
    await _database.delete('keychain', where: 'key = ?', whereArgs: [key]);
  }

  @override
  Future<void> clearAll() async {
    await _database.delete('keychain');
  }
}
