import '/backend/api_requests/api_calls.dart';
import '/components/alert_box_custom_widget.dart';
import '/components/appbar_widget.dart';
import '/components/emptylist_widget.dart';
import '/components/nav_bar1_widget.dart';
import '/components/no_permission_widget.dart';
import '/components/permission_list_view_widget.dart';
import '/flutterlib/flutter_button_tabbar.dart';
import '/flutterlib/flutter_theme.dart';
import '/flutterlib/flutter_util.dart';
import '/flutterlib/flutter_widgets.dart';
import '/flutterlib/custom_functions.dart' as functions;
import 'dart:async';
import 'user_widget.dart' show UserWidget;
import 'package:aligned_dialog/aligned_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';

class UserModel extends FlutterModel<UserWidget> {
  ///  Local state fields for this page.

  String? roleID;

  List<int> permissionsID = [];
  void addToPermissionsID(int item) => permissionsID.add(item);
  void removeFromPermissionsID(int item) => permissionsID.remove(item);
  void removeAtIndexFromPermissionsID(int index) =>
      permissionsID.removeAt(index);
  void insertAtIndexInPermissionsID(int index, int item) =>
      permissionsID.insert(index, item);
  void updatePermissionsIDAtIndex(int index, Function(int) updateFn) =>
      permissionsID[index] = updateFn(permissionsID[index]);

  bool checkPermission21 = false;

  bool checkPermission22 = false;

  bool checkPermission23 = false;

  bool checkPermission24 = false;

  bool checkPermission16 = false;

  ///  State fields for stateful widgets in this page.

  final unfocusNode = FocusNode();
  // Stores action output result for [Backend Call - API (Check role has permission)] action in user widget.
  ApiCallResponse? apiResult16;
  // Stores action output result for [Backend Call - API (Check role has permission)] action in user widget.
  ApiCallResponse? apiResult21;
  // Stores action output result for [Backend Call - API (Check role has permission)] action in user widget.
  ApiCallResponse? apiResult22;
  // Stores action output result for [Backend Call - API (Check role has permission)] action in user widget.
  ApiCallResponse? apiResult23;
  // Stores action output result for [Backend Call - API (Check role has permission)] action in user widget.
  ApiCallResponse? apiResult24;
  Completer<ApiCallResponse>? apiRequestCompleter2;
  Completer<ApiCallResponse>? apiRequestCompleter1;
  // State field(s) for TabBar widget.
  TabController? tabBarController;
  int get tabBarCurrentIndex =>
      tabBarController != null ? tabBarController!.index : 0;

  // Stores action output result for [Backend Call - API (Delete User)] action in Text widget.
  ApiCallResponse? apiResult6id;
  // Model for PermissionListView component.
  late PermissionListViewModel permissionListViewModel;
  // Model for noPermission component.
  late NoPermissionModel noPermissionModel1;
  // Model for noPermission component.
  late NoPermissionModel noPermissionModel2;
  // Model for NavBar1 component.
  late NavBar1Model navBar1Model;
  // Model for appbar component.
  late AppbarModel appbarModel;

  /// Initialization and disposal methods.

  void initState(BuildContext context) {
    permissionListViewModel =
        createModel(context, () => PermissionListViewModel());
    noPermissionModel1 = createModel(context, () => NoPermissionModel());
    noPermissionModel2 = createModel(context, () => NoPermissionModel());
    navBar1Model = createModel(context, () => NavBar1Model());
    appbarModel = createModel(context, () => AppbarModel());
  }

  void dispose() {
    unfocusNode.dispose();
    tabBarController?.dispose();
    permissionListViewModel.dispose();
    noPermissionModel1.dispose();
    noPermissionModel2.dispose();
    navBar1Model.dispose();
    appbarModel.dispose();
  }

  /// Action blocks are added here.

  /// Additional helper methods are added here.

  Future waitForApiRequestCompleted2({
    double minWait = 0,
    double maxWait = double.infinity,
  }) async {
    final stopwatch = Stopwatch()..start();
    while (true) {
      await Future.delayed(Duration(milliseconds: 50));
      final timeElapsed = stopwatch.elapsedMilliseconds;
      final requestComplete = apiRequestCompleter2?.isCompleted ?? false;
      if (timeElapsed > maxWait || (requestComplete && timeElapsed > minWait)) {
        break;
      }
    }
  }

  Future waitForApiRequestCompleted1({
    double minWait = 0,
    double maxWait = double.infinity,
  }) async {
    final stopwatch = Stopwatch()..start();
    while (true) {
      await Future.delayed(Duration(milliseconds: 50));
      final timeElapsed = stopwatch.elapsedMilliseconds;
      final requestComplete = apiRequestCompleter1?.isCompleted ?? false;
      if (timeElapsed > maxWait || (requestComplete && timeElapsed > minWait)) {
        break;
      }
    }
  }
}
