import 'package:flutter/material.dart';
import 'package:hub/backend/api_requests/api_manager.dart';
import 'package:hub/components/molecular_components/message_opt_modal/opt_modal_widget.dart';
import 'package:hub/flutter_flow/flutter_flow_model.dart';
import 'package:hub/flutter_flow/request_manager.dart';
import 'package:hub/pages/acess_history_page/acess_history_page_widget.dart';
import 'package:hub/shared/helpers/base_storage.dart';
import 'package:hub/shared/helpers/storage_helper.dart';

class AcessHistoryPageModel extends FlutterFlowModel<AccessHistoryScreen> {
  late final String devUUID;
  late final String userUUID;
  late final String cliUUID;

  final unfocusNode = FocusNode();
  final _accessHistoryManager = FutureRequestManager<ApiCallResponse>();
  Future<ApiCallResponse> accessHistory({
    String? uniqueQueryKey,
    bool? overrideCache,
    required Future<ApiCallResponse> Function() requestFn,
  }) =>
      _accessHistoryManager.performRequest(
        uniqueQueryKey: uniqueQueryKey,
        overrideCache: overrideCache,
        requestFn: requestFn,
      );
  void clearAccessHistoryCache() => _accessHistoryManager.clear();
  void clearAccessHistoryCacheKey(String? uniqueKey) => _accessHistoryManager.clearRequest(uniqueKey);

  @override
  void initState(BuildContext context) {
    initDatabase();
  }

  Future<void> initDatabase() async {
    devUUID = (await StorageHelper().get(SQLiteStorageKey.devUUID.value, Storage.SQLiteStorage)) ?? '';
    userUUID = (await StorageHelper().get(SQLiteStorageKey.userUUID.value, Storage.SQLiteStorage)) ?? '';
    cliUUID = (await StorageHelper().get(SQLiteStorageKey.clientUUID.value, Storage.SQLiteStorage)) ?? '';
  }

  @override
  void dispose() {
    unfocusNode.dispose();

    clearAccessHistoryCache();
  }

  Future toggleOptionsAction(BuildContext context) async {
    await showModalBottomSheet(
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      useSafeArea: true,
      context: context,
      builder: (context) {
        return GestureDetector(
          onTap: () => unfocusNode.canRequestFocus
              ? FocusScope.of(context).requestFocus(unfocusNode)
              : FocusScope.of(context).unfocus(),
          child: Padding(
            padding: MediaQuery.viewInsetsOf(context),
            child: const OptModalWidget(),
          ),
        );
      },
    );
  }
}
