import '/flutterlib/flutter_theme.dart';
import '/flutterlib/flutter_util.dart';
import '/flutterlib/custom_functions.dart' as functions;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'checkbox_model.dart';
export 'checkbox_model.dart';

class CheckboxWidget extends StatefulWidget {
  const CheckboxWidget({
    Key? key,
    this.permissionList,
    this.getPermissions,
  }) : super(key: key);

  final dynamic permissionList;
  final List<int>? getPermissions;

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

class _CheckboxWidgetState extends State<CheckboxWidget> {
  late CheckboxModel _model;

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

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

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

    super.dispose();
  }

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

    return Theme(
      data: ThemeData(
        checkboxTheme: CheckboxThemeData(
          visualDensity: VisualDensity.standard,
          materialTapTargetSize: MaterialTapTargetSize.padded,
        ),
        unselectedWidgetColor: FlutterTheme.of(context).secondaryText,
      ),
      child: CheckboxListTile(
        value: _model.checkboxValue ??= functions.comparePermission(
            widget.getPermissions!.toList(),
            getJsonField(
              widget.permissionList,
              r'''$.id''',
            )),
        onChanged: (newValue) async {
          setState(() => _model.checkboxValue = newValue!);
          if (newValue!) {
            FFAppState().update(() {
              FFAppState().addToAddPermissionsID(getJsonField(
                widget.permissionList,
                r'''$.id''',
              ).toString());
            });
          } else {
            FFAppState().update(() {
              FFAppState().addToRemovePermissionsID(getJsonField(
                widget.permissionList,
                r'''$.id''',
              ).toString());
            });
          }
        },
        title: Text(
          getJsonField(
            widget.permissionList,
            r'''$.display_name''',
          ).toString(),
          style: FlutterTheme.of(context).titleLarge.override(
                fontFamily: 'Outfit',
                fontSize: 16.0,
              ),
        ),
        tileColor: Colors.black,
        activeColor: Colors.white,
        checkColor: Colors.black,
        dense: false,
        controlAffinity: ListTileControlAffinity.trailing,
      ),
    );
  }
}
