import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';

/// 菜单项数据模型
class MenuItem {
  final String icon;
  final String title;
  final Color iconColor;
  final VoidCallback? onTap;

  MenuItem({
    required this.icon,
    required this.title,
    required this.iconColor,
    this.onTap,
  });
}

/// 菜单列表组件
class MenuList extends StatelessWidget {
  const MenuList({super.key});

  // 获取菜单项列表（最多6个）
  List<MenuItem> _getMenuItems() {
    return [
      MenuItem(
        icon: Icons.movie.codePoint.toString(),
        title: 'Movies&TV',
        iconColor: const Color(0xFFFF3B30),
        onTap: () {
          // 处理点击事件
          print('Movies&TV tapped');
        },
      ),
      MenuItem(
        icon: Icons.people.codePoint.toString(),
        title: 'Social',
        iconColor: const Color(0xFF007AFF),
        onTap: () {
          print('Social tapped');
        },
      ),
      MenuItem(
        icon: Icons.headset_mic.codePoint.toString(),
        title: 'Support',
        iconColor: const Color(0xFF34C759),
        onTap: () {
          print('Support tapped');
        },
      ),
      MenuItem(
        icon: Icons.sports_basketball.codePoint.toString(),
        title: 'Sport',
        iconColor: const Color(0xFFFF9500),
        onTap: () {
          print('Sport tapped');
        },
      ),
      MenuItem(
        icon: Icons.music_note.codePoint.toString(),
        title: 'Movies&TV',
        iconColor: const Color(0xFF00C7BE),
        onTap: () {
          print('Music tapped');
        },
      ),
      MenuItem(
        icon: Icons.videogame_asset.codePoint.toString(),
        title: 'Movies&TV',
        iconColor: const Color(0xFFAF52DE),
        onTap: () {
          print('Game tapped');
        },
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    final menuItems = _getMenuItems();

    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      padding: EdgeInsets.symmetric(vertical: 16.w),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3, // 每排3个
        crossAxisSpacing: 8.w, // 水平间距
        mainAxisSpacing: 8.w, // 垂直间距
        childAspectRatio: 4 / 3, // 宽高比
      ),
      itemCount: menuItems.length > 6 ? 6 : menuItems.length, // 最多6个
      itemBuilder: (context, index) {
        return _buildMenuItem(menuItems[index]);
      },
    );
  }

  /// 构建单个菜单项
  Widget _buildMenuItem(MenuItem item) {
    return GestureDetector(
      onTap: item.onTap,
      child: Container(
        decoration: BoxDecoration(
          color: Get.reactiveTheme.cardColor,
          borderRadius: BorderRadius.circular(10.r),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 图标
            Icon(
              IconData(int.parse(item.icon), fontFamily: 'MaterialIcons'),
              size: 20.w,
              color: item.iconColor,
            ),
            // 标题
            Text(
              item.title,
              textAlign: TextAlign.center,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 12.sp,
                height: 1.7,
                color: Get.theme.hintColor,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
