import 'package:flutter/material.dart';
import 'package:hub/features/menu/domain/index.dart';

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

enum MenuView { list_grid, list, grid }

class MenuListViewModel extends FlutterFlowModel<MenuListView> {
  @override
  void initState(BuildContext context) {}

  @override
  void dispose() {}
}

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

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

  @override
  State<MenuListView> createState() => _MenuListViewState();
}

class _MenuListViewState extends State<MenuListView> {
  late MenuListViewModel _model;

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

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

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

    super.dispose();
  }

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

  Widget buildMenuItem(BuildContext context) {
    switch (widget.item) {
      case EnumMenuItem.button:
        return _buildButtonListView();
      case EnumMenuItem.card:
        return _buildCardListView();
      case EnumMenuItem.tile:
        return buildMenuList(context);
    }
  }

  Widget _buildButtonListView() {
    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]),
          );
        },
      ),
    );
  }

  Widget _buildCardListView() {
    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]),
          );
        },
      ),
    );
  }

  Widget buildMenuList(BuildContext context) {
    return Expanded(
      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();
            },
          ),
        ),
      ],
    );
  }
}
