import '/backend/api_requests/api_calls.dart';
import '/components/permission_list_container1_widget.dart';
import '/components/permission_list_container2_widget.dart';
import '/components/permission_list_container3_widget.dart';
import '/components/permission_list_container_widget.dart';
import '/flutterlib/flutter_drop_down.dart';
import '/flutterlib/flutter_theme.dart';
import '/flutterlib/flutter_util.dart';
import '/flutterlib/flutter_widgets.dart';
import '/flutterlib/form_field_controller.dart';
import '/flutterlib/custom_functions.dart' as functions;
import 'dart:async';
import 'permission_list_view_widget.dart' show PermissionListViewWidget;
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';

class PermissionListViewModel
    extends FlutterModel<PermissionListViewWidget> {
  ///  Local state fields for this component.

  String roleID = '';

  List<int> getPermissionsLIst = [];
  void addToGetPermissionsLIst(int item) => getPermissionsLIst.add(item);
  void removeFromGetPermissionsLIst(int item) =>
      getPermissionsLIst.remove(item);
  void removeAtIndexFromGetPermissionsLIst(int index) =>
      getPermissionsLIst.removeAt(index);
  void insertAtIndexInGetPermissionsLIst(int index, int item) =>
      getPermissionsLIst.insert(index, item);
  void updateGetPermissionsLIstAtIndex(int index, Function(int) updateFn) =>
      getPermissionsLIst[index] = updateFn(getPermissionsLIst[index]);

  List<int> getPermissionsLIst1 = [];
  void addToGetPermissionsLIst1(int item) => getPermissionsLIst1.add(item);
  void removeFromGetPermissionsLIst1(int item) =>
      getPermissionsLIst1.remove(item);
  void removeAtIndexFromGetPermissionsLIst1(int index) =>
      getPermissionsLIst1.removeAt(index);
  void insertAtIndexInGetPermissionsLIst1(int index, int item) =>
      getPermissionsLIst1.insert(index, item);
  void updateGetPermissionsLIst1AtIndex(int index, Function(int) updateFn) =>
      getPermissionsLIst1[index] = updateFn(getPermissionsLIst1[index]);

  List<int> getPermissionsList2 = [];
  void addToGetPermissionsList2(int item) => getPermissionsList2.add(item);
  void removeFromGetPermissionsList2(int item) =>
      getPermissionsList2.remove(item);
  void removeAtIndexFromGetPermissionsList2(int index) =>
      getPermissionsList2.removeAt(index);
  void insertAtIndexInGetPermissionsList2(int index, int item) =>
      getPermissionsList2.insert(index, item);
  void updateGetPermissionsList2AtIndex(int index, Function(int) updateFn) =>
      getPermissionsList2[index] = updateFn(getPermissionsList2[index]);

  List<int> getPermissionsList3 = [];
  void addToGetPermissionsList3(int item) => getPermissionsList3.add(item);
  void removeFromGetPermissionsList3(int item) =>
      getPermissionsList3.remove(item);
  void removeAtIndexFromGetPermissionsList3(int index) =>
      getPermissionsList3.removeAt(index);
  void insertAtIndexInGetPermissionsList3(int index, int item) =>
      getPermissionsList3.insert(index, item);
  void updateGetPermissionsList3AtIndex(int index, Function(int) updateFn) =>
      getPermissionsList3[index] = updateFn(getPermissionsList3[index]);

  int? loopCounter = 0;

  List<String> filteredRemovePermissions = [];
  void addToFilteredRemovePermissions(String item) =>
      filteredRemovePermissions.add(item);
  void removeFromFilteredRemovePermissions(String item) =>
      filteredRemovePermissions.remove(item);
  void removeAtIndexFromFilteredRemovePermissions(int index) =>
      filteredRemovePermissions.removeAt(index);
  void insertAtIndexInFilteredRemovePermissions(int index, String item) =>
      filteredRemovePermissions.insert(index, item);
  void updateFilteredRemovePermissionsAtIndex(
          int index, Function(String) updateFn) =>
      filteredRemovePermissions[index] =
          updateFn(filteredRemovePermissions[index]);

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

  Completer<ApiCallResponse>? apiRequestCompleter;
  // Stores action output result for [Backend Call - API (Get Permission of Role)] action in PermissionListView widget.
  ApiCallResponse? apiResults0e;
  // Stores action output result for [Backend Call - API (Get Permission of Role)] action in PermissionListView widget.
  ApiCallResponse? apiResults0a;
  // Stores action output result for [Backend Call - API (Get Permission of Role)] action in PermissionListView widget.
  ApiCallResponse? apiResults0c;
  // Stores action output result for [Backend Call - API (Get Permission of Role)] action in PermissionListView widget.
  ApiCallResponse? apiResults0d;
  // State field(s) for role1223 widget.
  String? role1223Value;
  FormFieldController<String>? role1223ValueController;
  // Model for PermissionListContainer component.
  late PermissionListContainerModel permissionListContainerModel;
  // Model for PermissionListContainer1 component.
  late PermissionListContainer1Model permissionListContainer1Model;
  // Model for PermissionListContainer2 component.
  late PermissionListContainer2Model permissionListContainer2Model;
  // Model for PermissionListContainer3 component.
  late PermissionListContainer3Model permissionListContainer3Model;
  // Stores action output result for [Backend Call - API (Add permission to role)] action in Button widget.
  ApiCallResponse? apiResulttvj;
  // Stores action output result for [Backend Call - API (Remove Permission to role)] action in Button widget.
  ApiCallResponse? apiResulttvq;

  /// Initialization and disposal methods.

  void initState(BuildContext context) {
    permissionListContainerModel =
        createModel(context, () => PermissionListContainerModel());
    permissionListContainer1Model =
        createModel(context, () => PermissionListContainer1Model());
    permissionListContainer2Model =
        createModel(context, () => PermissionListContainer2Model());
    permissionListContainer3Model =
        createModel(context, () => PermissionListContainer3Model());
  }

  void dispose() {
    permissionListContainerModel.dispose();
    permissionListContainer1Model.dispose();
    permissionListContainer2Model.dispose();
    permissionListContainer3Model.dispose();
  }

  /// Action blocks are added here.

  /// Additional helper methods are added here.

  Future waitForApiRequestCompleted({
    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 = apiRequestCompleter?.isCompleted ?? false;
      if (timeElapsed > maxWait || (requestComplete && timeElapsed > minWait)) {
        break;
      }
    }
  }
}
