import '/backend/api_requests/api_calls.dart';
import '/components/checkbox_widget.dart';
import '/flutterlib/flutter_theme.dart';
import '/flutterlib/flutter_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'permission_list_model.dart';
export 'permission_list_model.dart';

class PermissionListWidget extends StatefulWidget {
  const PermissionListWidget({
    Key? key,
    this.roleID,
    this.getPermissionsList,
  }) : super(key: key);

  final String? roleID;
  final List<int>? getPermissionsList;

  @override
  _PermissionListWidgetState createState() => _PermissionListWidgetState();
}

class _PermissionListWidgetState extends State<PermissionListWidget> {
  late PermissionListModel _model;

  @override
  void setState(VoidCallback callback) {
    super.setState(callback);
    _model.onUpdate();
  }

  @override
  void initState() {
    super.initState();
    _model = createModel(context, () => PermissionListModel());
  }

  @override
  void dispose() {
    _model.maybeDispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    context.watch<FFAppState>();

    return Container(
      decoration: BoxDecoration(),
      child: FutureBuilder<ApiCallResponse>(
        future: NumstationGroup.groupPermissionsCall.call(
          token: FFAppState().token,
        ),
        builder: (context, snapshot) {
          // Customize what your widget looks like when it's loading.
          if (!snapshot.hasData) {
            return Center(
              child: SizedBox(
                width: 50.0,
                height: 50.0,
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(
                    FlutterTheme.of(context).info,
                  ),
                ),
              ),
            );
          }
          final columnGroupPermissionsResponse = snapshot.data!;
          return Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              Column(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Container(
                    decoration: BoxDecoration(),
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Container(
                          width: 358.0,
                          height: 38.0,
                          decoration: BoxDecoration(
                            color: Color(0xFFF4F7FA),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Padding(
                                padding: EdgeInsetsDirectional.fromSTEB(
                                    5.0, 0.0, 0.0, 0.0),
                                child: Text(
                                  getJsonField(
                                    columnGroupPermissionsResponse.jsonBody,
                                    r'''$.group[?(@.id == 3)].name''',
                                  ).toString(),
                                  style: FlutterTheme.of(context)
                                      .bodyMedium
                                      .override(
                                        fontFamily: 'Readex Pro',
                                        fontSize: 16.0,
                                      ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          decoration: BoxDecoration(),
                          child: Builder(
                            builder: (context) {
                              final group3 = getJsonField(
                                columnGroupPermissionsResponse.jsonBody,
                                r'''$.group[?(@.id == 3)].permissions[*]''',
                              ).toList();
                              return Column(
                                mainAxisSize: MainAxisSize.max,
                                children:
                                    List.generate(group3.length, (group3Index) {
                                  final group3Item = group3[group3Index];
                                  return CheckboxWidget(
                                    key: Key(
                                        'Key4rz_${group3Index}_of_${group3.length}'),
                                    permissionList: group3Item,
                                    getPermissions: widget.getPermissionsList,
                                  );
                                }),
                              );
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Column(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Container(
                    decoration: BoxDecoration(),
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Container(
                          width: 358.0,
                          height: 38.0,
                          decoration: BoxDecoration(
                            color: Color(0xFFF4F7FA),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Padding(
                                padding: EdgeInsetsDirectional.fromSTEB(
                                    5.0, 0.0, 0.0, 0.0),
                                child: Text(
                                  getJsonField(
                                    columnGroupPermissionsResponse.jsonBody,
                                    r'''$.group[?(@.id == 4)].name''',
                                  ).toString(),
                                  style: FlutterTheme.of(context)
                                      .bodyMedium
                                      .override(
                                        fontFamily: 'Readex Pro',
                                        fontSize: 16.0,
                                      ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          decoration: BoxDecoration(),
                          child: Builder(
                            builder: (context) {
                              final group4 = getJsonField(
                                columnGroupPermissionsResponse.jsonBody,
                                r'''$.group[?(@.id == 4)].permissions[*]''',
                              ).toList();
                              return Column(
                                mainAxisSize: MainAxisSize.max,
                                children:
                                    List.generate(group4.length, (group4Index) {
                                  final group4Item = group4[group4Index];
                                  return CheckboxWidget(
                                    key: Key(
                                        'Keymaw_${group4Index}_of_${group4.length}'),
                                    permissionList: group4Item,
                                    getPermissions: widget.getPermissionsList,
                                  );
                                }),
                              );
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              Column(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Container(
                    decoration: BoxDecoration(),
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      children: [
                        Container(
                          width: 358.0,
                          height: 38.0,
                          decoration: BoxDecoration(
                            color: Color(0xFFF4F7FA),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Padding(
                                padding: EdgeInsetsDirectional.fromSTEB(
                                    5.0, 0.0, 0.0, 0.0),
                                child: Text(
                                  getJsonField(
                                    columnGroupPermissionsResponse.jsonBody,
                                    r'''$.group[?(@.id == 5)].name''',
                                  ).toString(),
                                  style: FlutterTheme.of(context)
                                      .bodyMedium
                                      .override(
                                        fontFamily: 'Readex Pro',
                                        fontSize: 16.0,
                                      ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          decoration: BoxDecoration(),
                          child: Builder(
                            builder: (context) {
                              final group5 = getJsonField(
                                columnGroupPermissionsResponse.jsonBody,
                                r'''$.group[?(@.id == 5)].permissions[*]''',
                              ).toList();
                              return Column(
                                mainAxisSize: MainAxisSize.max,
                                children:
                                    List.generate(group5.length, (group5Index) {
                                  final group5Item = group5[group5Index];
                                  return CheckboxWidget(
                                    key: Key(
                                        'Key9tj_${group5Index}_of_${group5.length}'),
                                    permissionList: group5Item,
                                    getPermissions: widget.getPermissionsList,
                                  );
                                }),
                              );
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}
