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';

import '../../../../config/translations/strings_enum.dart';
import '../../../../utils/system_helper.dart';
import '../../../constants/iconfont/iconfont.dart';
import '../../../dialog/all_dialog.dart';
import '../../../routes/app_pages.dart';

/// 菜单项数据模型
class MenuItem {
  final IconData 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: IconFont.icon19,
        title: Strings.moviesAndTV.tr,
        iconColor: const Color(0xFFFF3B30),
        onTap: () {
          // 处理点击事件
          print('Movies&TV tapped');
          Get.toNamed(Routes.MEDIALOCATION);
        },
      ),
      MenuItem(
        icon: IconFont.icon26,
        title: Strings.social.tr,
        iconColor: const Color(0xFF007AFF),
        onTap: () {
          print('Social tapped');
          SystemHelper.openTestPage();
        },
      ),
      MenuItem(
        icon: IconFont.icon28,
        title: Strings.support.tr,
        iconColor: const Color(0xFF34C759),
        onTap: () {
          print('Support tapped');
          AllDialog.showUpdate(hasForceUpdate: true);
        },
      ),
      MenuItem(
        icon: IconFont.icon41,
        title: Strings.sport.tr,
        iconColor: const Color(0xFFFF9500),
        onTap: () {
          print('Sport tapped');
        },
      ),
      MenuItem(
        icon: IconFont.icon52,
        title: Strings.music.tr,
        iconColor: const Color(0xFF00C7BE),
        onTap: () {
          print('Music tapped');
        },
      ),
      MenuItem(
        icon: IconFont.icon53,
        title: Strings.game.tr,
        iconColor: const Color(0xFFAF52DE),
        onTap: () {
          print('Game tapped');
        },
      ),
    ];
  }

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

    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3, // 每排3个
        crossAxisSpacing: 8.w, // 水平间距
        mainAxisSpacing: 8.w, // 垂直间距
        childAspectRatio: 3 / 2, // 宽高比
      ),
      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(item.icon, size: 20.w, color: item.iconColor),
            4.verticalSpaceFromWidth,
            // 标题
            Text(
              item.title,
              textAlign: TextAlign.center,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 13.sp,
                height: 1.4,
                color: Get.theme.hintColor,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
