import 'package:flutter/material.dart';

import '../../../core/l10n/app_localizations.dart';
import '../../../core/theme/app_colors.dart';

class AppHomeScreen extends StatelessWidget {
  const AppHomeScreen({super.key});

  static const _projects = <_HotProject>[
    _HotProject(
      id: 'runes',
      icoImg: 'ico_runes.png',
      shortName: 'M',
      background: LinearGradient(
        colors: [Color(0xFF9F7AEA), Color(0xFF5A3FE0)],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      ),
    ),
    _HotProject(
      id: 'tesla',
      icoImg: 'ico_tesla.png',
      shortName: 'S',
      background: LinearGradient(
        colors: [Color(0xFFDEE9FF), Color(0xFF7B89A8)],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
      textColor: Color(0xFF1E2329),
    ),
    _HotProject(
      id: 'floki',
      icoImg: 'ico_floki.png',
      shortName: 'F',
      background: LinearGradient(
        colors: [Color(0xFF1E1E1E), Color(0xFFFFA726)],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
    ),
    _HotProject(
      id: 'vcAlliance',
      icoImg: 'ico_ftlm.png',
      shortName: 'VC',
      background: LinearGradient(
        colors: [Color(0xFF2490FF), Color(0xFF1D5FE6)],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      ),
    ),
    _HotProject(
      id: 'aur',
      icoImg: 'ico_aur.png',
      shortName: 'A',
      background: LinearGradient(
        colors: [Color(0xFF101E45), Color(0xFF13B9FF)],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
    ),
    _HotProject(
      id: 'vtf',
      icoImg: 'ico_vtf.png',
      shortName: 'VTF',
      background: LinearGradient(
        colors: [Color(0xFF03112E), Color(0xFF1F78FF)],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      ),
    ),
    _HotProject(
      id: 'footballCoin',
      icoImg: 'ico_zqb.png',
      shortName: 'FC',
      background: LinearGradient(
        colors: [Color(0xFFF6E7C9), Color(0xFFB5D98A)],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
      textColor: Color(0xFF1E2329),
    ),
    _HotProject(
      id: 'jarvis',
      icoImg: 'ico_jws.png',
      shortName: 'AI',
      background: LinearGradient(
        colors: [Color(0xFFD9FFD8), Color(0xFF9BE7C2)],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      ),
      textColor: Color(0xFF114C3A),
    ),
  ];

  void _handleProjectTap(BuildContext context) {
    _showDevelopingDialog(context);
  }

  static void _showDevelopingDialog(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final isDark = Theme.of(context).brightness == Brightness.dark;
    final dialogBg = isDark ? AppColors.darkBgSecondary : Colors.white;
    final titleColor = isDark ? AppColors.darkTextPrimary : Colors.black;
    final messageColor = isDark
        ? AppColors.darkTextSecondary
        : const Color(0xFF666666);
    final buttonBg = isDark ? AppColors.brand : Colors.black;
    final buttonFg = isDark ? Colors.black : Colors.white;

    showDialog<void>(
      context: context,
      barrierDismissible: true,
      builder: (dialogContext) => Dialog(
        backgroundColor: dialogBg,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
        insetPadding: const EdgeInsets.symmetric(horizontal: 48, vertical: 48),
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 28),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                l10n.appHomeComingSoon,
                style: TextStyle(
                  color: titleColor,
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                ),
              ),
              const SizedBox(height: 8),
              Text(
                l10n.appHomeDevelopingMessage,
                style: TextStyle(
                  color: messageColor,
                  fontSize: 13,
                ),
              ),
              const SizedBox(height: 20),
              SizedBox(
                width: double.infinity,
                child: TextButton(
                  onPressed: () => Navigator.of(dialogContext).pop(),
                  style: TextButton.styleFrom(
                    backgroundColor: buttonBg,
                    foregroundColor: buttonFg,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                    padding: const EdgeInsets.symmetric(vertical: 12),
                  ),
                  child: Text(l10n.confirmLabel),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final isDark = Theme.of(context).brightness == Brightness.dark;
    final backgroundColor = isDark ? AppColors.darkBg : Colors.white;
    final titleColor = isDark ? AppColors.darkTextPrimary : Colors.black;
    final sectionTitleColor = isDark ? AppColors.darkTextPrimary : Colors.black;

    return Scaffold(
      backgroundColor: backgroundColor,
      appBar: AppBar(
        backgroundColor: backgroundColor,
        elevation: 0,
        scrolledUnderElevation: 0,
        centerTitle: true,
        title: Text(
          l10n.appHomeTitle,
          style: TextStyle(
            color: titleColor,
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios_new, color: titleColor),
          onPressed: () => Navigator.of(context).maybePop(),
        ),
      ),
      body: SafeArea(
        top: false,
        child: SingleChildScrollView(
          padding: const EdgeInsets.fromLTRB(10, 8, 10, 24),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const _HeroBanner(),
              const SizedBox(height: 28),
              Text(
                l10n.appHomeHotProjects,
                style: TextStyle(
                  color: sectionTitleColor,
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 18),
              LayoutBuilder(
                builder: (context, constraints) {
                  const crossAxisCount = 5;
                  const spacing = 14.0;
                  final tileWidth =
                      (constraints.maxWidth - spacing * (crossAxisCount - 1)) /
                          crossAxisCount;

                  return Wrap(
                    spacing: spacing,
                    runSpacing: 22,
                    children: _projects
                        .map(
                          (project) => SizedBox(
                            width: tileWidth,
                            child: _ProjectTile(
                              label: _projectLabel(l10n, project.id),
                              project: project,
                              isDark: isDark,
                              onTap: () => _handleProjectTap(context),
                            ),
                          ),
                        )
                        .toList(),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  String _projectLabel(AppLocalizations l10n, String id) {
    switch (id) {
      case 'runes':
        return l10n.appHomeProjectRunes;
      case 'tesla':
        return l10n.appHomeProjectTesla;
      case 'floki':
        return l10n.appHomeProjectFloki;
      case 'vcAlliance':
        return l10n.appHomeProjectVcAlliance;
      case 'aur':
        return l10n.appHomeProjectAur;
      case 'vtf':
        return l10n.appHomeProjectVtf;
      case 'footballCoin':
        return l10n.appHomeProjectFootballCoin;
      case 'jarvis':
        return l10n.appHomeProjectJarvis;
      default:
        return id;
    }
  }
}

class _HeroBanner extends StatelessWidget {
  const _HeroBanner();

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;

    return Container(
      width: double.infinity,
      height: 200,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        gradient: const LinearGradient(
          colors: [Color(0xFF020202), Color(0xFF171103), Color(0xFF9A6800)],
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
        ),
        boxShadow: const [
          BoxShadow(
            color: Color(0x33000000),
            blurRadius: 24,
            offset: Offset(0, 10),
          ),
        ],
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(8),
        child: Stack(
          children: [
            Positioned(
              right: -10,
              top: -20,
              bottom: -10,
              child: Opacity(
                opacity: 0.9,
                child: Image.asset(
                  'assets/images/top_bg_app_home.png',
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Positioned(
              right: 0,
              top: 0,
              bottom: 0,
              width: 180,
              child: DecoratedBox(
                decoration: BoxDecoration(
                  gradient: RadialGradient(
                    colors: [
                      AppColors.brand.withValues(alpha: 0.45),
                      Colors.transparent,
                    ],
                    radius: 0.95,
                    center: const Alignment(0.6, -0.2),
                  ),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(18, 18, 18, 18),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  //钱包图标
                  Image.asset("assets/images/ico_iBit_wallet.png",width:30,),
                  const Spacer(),
                  Text(
                    l10n.appHomeHeroTitle,
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 19,
                      fontWeight: FontWeight.w700,
                    ),
                  ),
                  const SizedBox(height: 6),
                  RichText(
                    text: TextSpan(
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 19,
                        fontWeight: FontWeight.w700,
                      ),
                      children: [
                        TextSpan(
                          text: l10n.appHomeAnnualizedYieldPrefix,
                        ),
                        TextSpan(
                          text: '666%',
                          style: const TextStyle(
                            color: Color(0xFFF2B713),
                            fontSize: 22,
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                        TextSpan(
                          text: l10n.appHomeAnnualizedYieldSuffix,
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 16),
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 18,
                      vertical: 5,
                    ),
                    decoration: BoxDecoration(
                      border: Border.all(color: AppColors.brand, width: 1.2),
                      borderRadius: BorderRadius.circular(22),
                    ),
                    child: Text(
                      l10n.appHomeExclusiveBadge,
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 13,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _ProjectTile extends StatelessWidget {
  const _ProjectTile({
    required this.label,
    required this.project,
    required this.onTap,
    required this.isDark,
  });

  final String label;
  final _HotProject project;
  final VoidCallback onTap;
  final bool isDark;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            width: 50,
            height: 50,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: Image.asset(
                'assets/images/${project.icoImg}',
                fit: BoxFit.cover,
              ),
            ),
          ),
          const SizedBox(height: 10),
          Text(
            label,
            textAlign: TextAlign.center,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              color: isDark
                  ? AppColors.darkTextPrimary
                  : AppColors.lightTextSecondary,
              fontSize: 13,
              fontWeight: FontWeight.w500,
              height: 1.2,
            ),
          ),
        ],
      ),
    );
  }
}

class _HotProject {
  const _HotProject({
    required this.id,
    required this.icoImg,
    required this.shortName,
    required this.background,
    this.textColor = Colors.white,
  });

  final String id;
  final String icoImg;
  final String shortName;
  final Gradient background;
  final Color textColor;
}
