import 'package:flutter/material.dart';
import 'package:hub/features/menu/index.dart';
import 'package:hub/flutter_flow/flutter_flow_icon_button.dart';
import 'package:hub/flutter_flow/flutter_flow_theme.dart';
import 'package:hub/flutter_flow/flutter_flow_util.dart';

class MenuStaggeredViewModel extends FlutterFlowModel<MenuStaggeredView> {
  @override
  void initState(BuildContext context) {}

  @override
  void dispose() {}
}

class MenuStaggeredView extends StatefulWidget {
  const MenuStaggeredView(
      {super.key,
      required this.changeMenuStyle,
      required this.item,
      required this.options,
      required this.isGrid});

  final EnumMenuItem item;
  final bool isGrid;
  final List<MenuItem?> options;
  final Future Function()? changeMenuStyle;

  @override
  State<MenuStaggeredView> createState() => _MenuStaggeredViewState();
}

class _MenuStaggeredViewState extends State<MenuStaggeredView> {
  late MenuStaggeredViewModel _model;

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

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

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

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final textScaler = MediaQuery.textScalerOf(context);
    final double scaledFontSize = 14 * textScaler.scale(1);
    final int crossAxisCount = _calculateCrossAxisCount(scaledFontSize);

    return Column(
      children: [
        _buildGridView(crossAxisCount),
      ]
          .addToStart(const SizedBox(height: 30))
          .addToEnd(const SizedBox(height: 30)),
    );
  }

  int _calculateCrossAxisCount(double scaledFontSize) {
    if (scaledFontSize >= 26) return 1;
    if (scaledFontSize >= 18) return 2;
    return 3;
  }

  Widget _buildGridView(int crossAxisCount) {
    return GridView.builder(
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: crossAxisCount,
        crossAxisSpacing: 10.0,
        mainAxisSpacing: 10.0,
        childAspectRatio: 1,
        mainAxisExtent: 100,
      ),
      itemCount: widget.options.length,
      padding: const EdgeInsets.symmetric(horizontal: 14),
      shrinkWrap: true,
      itemBuilder: (context, index) {
        return Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          alignment: Alignment.center,
          padding: const EdgeInsets.all(0),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(0),
            color: Colors.transparent,
            boxShadow: const [
              BoxShadow(
                color: Colors.transparent,
                blurRadius: 4,
                offset: Offset(0, 2),
              ),
            ],
          ),
          child: Center(child: widget.options[index]),
        );
      },
    );
  }

  Widget collapseExpandMethod(BuildContext context) {
    if (widget.isGrid) {
      return _buildCollapseButton(context);
    } else {
      return _buildExpandButton(context);
    }
  }

  Widget _buildCollapseButton(BuildContext context) {
    return Align(
      alignment: const AlignmentDirectional(0.0, 0.0),
      child: Container(
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(0.0),
            bottomRight: Radius.circular(0.0),
            topLeft: Radius.circular(0.0),
            topRight: Radius.circular(0.0),
          ),
        ),
        alignment: const AlignmentDirectional(0.0, 0.0),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: [
            Text(
              FFLocalizations.of(context).getVariableText(
                enText: 'Minimize',
                ptText: 'Minimizar',
              ),
              style: FlutterFlowTheme.of(context).title1.override(
                    fontFamily: 'Nunito',
                    color: FlutterFlowTheme.of(context).primaryText,
                    fontSize: 12.0,
                    fontWeight: FontWeight.w600,
                    fontStyle: FontStyle.normal,
                  ),
            ),
            Align(
              alignment: const AlignmentDirectional(0.0, 0.0),
              child: FlutterFlowIconButton(
                borderColor: Colors.transparent,
                borderRadius: 20.0,
                borderWidth: 1.0,
                buttonSize: 50.0,
                fillColor: const Color(0x00FFFFFF),
                icon: Icon(
                  Icons.keyboard_arrow_up_sharp,
                  color: FlutterFlowTheme.of(context).primary,
                ),
                onPressed: () async {
                  await widget.changeMenuStyle?.call();
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildExpandButton(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          FFLocalizations.of(context).getVariableText(
            enText: 'Expand',
            ptText: 'Expandir',
          ),
          style: FlutterFlowTheme.of(context).title1.override(
                fontFamily: 'Nunito',
                color: FlutterFlowTheme.of(context).primaryText,
                fontSize: 12.0,
                fontWeight: FontWeight.w600,
                fontStyle: FontStyle.normal,
              ),
        ),
        Align(
          alignment: const AlignmentDirectional(0.0, 0.0),
          child: FlutterFlowIconButton(
            borderColor: Colors.transparent,
            borderRadius: 20.0,
            borderWidth: 0.0,
            buttonSize: 50.0,
            fillColor: const Color(0x00FFFFFF),
            icon: Icon(
              Icons.keyboard_arrow_down_outlined,
              color: FlutterFlowTheme.of(context).primary,
            ),
            onPressed: () async {
              await widget.changeMenuStyle?.call();
            },
          ),
        ),
      ],
    );
  }
}
