import 'package:flutter/material.dart';
import 'package:hub/backend/schema/enums/enums.dart';
import 'package:hub/components/molecular_components/menu_item/menu_item.dart';

import '/flutter_flow/flutter_flow_icon_button.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import 'menu_list_view_component_model.dart';

export 'menu_list_view_component_model.dart';

///

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

  final bool expandable;
  final MenuItem item;

  final List<MenuEntry> options;
  final Future Function()? changeMenuStyle;

  @override
  State<MenuListViewComponentWidget> createState() =>
      _MenuListViewComponentWidgetState();
}

class _MenuListViewComponentWidgetState
    extends State<MenuListViewComponentWidget> {
  late MenuListViewComponentModel _model;

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

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

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

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return buildMenuItem(context);
  }

  Widget buildMenuItem(BuildContext context) {
    switch (widget.item) {
      case MenuItem.button:
        return SizedBox(
          height: 100,
          width: double.infinity,
          child: ListView.builder(
            addAutomaticKeepAlives: true,
            padding: const EdgeInsets.symmetric(horizontal: 15),
            shrinkWrap: true,
            physics: const AlwaysScrollableScrollPhysics(),
            scrollDirection: Axis.horizontal,
            itemCount: widget.options.length,
            itemBuilder: (context, index) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: SizedBox(
                    height: 115, width: 115, child: widget.options[index]),
              );
            },
          ),
        );
      case MenuItem.card:
        return SizedBox(
          width: double.infinity,
          height: MediaQuery.sizeOf(context).height * 0.8,
          child: ListView.builder(
            addAutomaticKeepAlives: true,
            padding: const EdgeInsets.symmetric(horizontal: 15),
            shrinkWrap: true,
            physics: const AlwaysScrollableScrollPhysics(),
            scrollDirection: Axis.vertical,
            itemCount: widget.options.length,
            itemBuilder: (context, index) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: SizedBox(
                    height: 115, width: 115, child: widget.options[index]),
              );
            },
          ),
        );
      case MenuItem.tile:
        return buildMenuList(context);
    }
  }

  Widget buildMenuList(BuildContext context) {
    return Expanded(
      // width: MediaQuery.of(context).size.width,
      // height: MediaQuery.of(context).size.height,
      child: ListView.separated(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        shrinkWrap: true,
        physics: const AlwaysScrollableScrollPhysics(),
        scrollDirection: Axis.vertical,
        itemCount: widget.options.length,
        itemBuilder: (context, index) {
          return SizedBox(
            child: widget.options[index],
          );
        },
        separatorBuilder: (context, index) {
          return const Divider(thickness: 0.2);
        },
      ),
    );
  }

  Row buildExpandableButton(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();
            },
          ),
        ),
      ],
    );
  }
}
