import 'package:flutter/material.dart';
import '../../widgets/color.dart';
import '../../routes/app_routes.dart';

class BerandaScreen extends StatelessWidget {
  final VoidCallback? onCobaSekarang;

  const BerandaScreen({super.key, this.onCobaSekarang});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.background,
      appBar: AppBar(
        backgroundColor: AppColors.cardWhite,
        surfaceTintColor: Colors.transparent,
        elevation: 1,
        title: Row(
          children: [
            Image.asset(
              'assets/images/Logo_Coffee_Scan.png',
              width: 30,
              errorBuilder: (c, e, s) =>
                  const Icon(Icons.coffee, color: AppColors.brownMain),
            ),
            const SizedBox(width: 10),
            RichText(
              text: TextSpan(
                style: TextStyle(
                  fontFamily: 'Montserrat',
                  fontSize: 20,
                  fontWeight: FontWeight.w900,
                ),
                children: const [
                  TextSpan(
                    text: 'Coffee',
                    style: TextStyle(color: AppColors.brownMain),
                  ),
                  TextSpan(
                    text: 'Scan',
                    style: TextStyle(color: AppColors.greenMain),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      body: SingleChildScrollView(
        physics: const BouncingScrollPhysics(),
        padding: const EdgeInsets.fromLTRB(20, 24, 20, 170),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildHeroCard(),
            const SizedBox(height: 35),
            _buildSectionHeader(Icons.analytics_rounded, "Performa Model"),
            const SizedBox(height: 16),
            Row(
              children: [
                _buildStatCard("91.70%", "Accuracy Train"),
                const SizedBox(width: 12),
                _buildStatCard("88.22%", "Accuracy Valid"),
                const SizedBox(width: 12),
                _buildStatCard("89.76%", "Accuracy Test"),
              ],
            ),
            const SizedBox(height: 35),
            _buildSectionHeader(
              Icons.local_fire_department_rounded,
              "Biji Kopi Arabika",
            ),
            const SizedBox(height: 16),
            Row(
              children: [
                _buildCoffeeItem(context, "Arabika Light", "Arabika_light.png"),
                const SizedBox(width: 12),
                _buildCoffeeItem(
                  context,
                  "Arabika Medium",
                  "Arabika_medium.png",
                ),
                const SizedBox(width: 12),
                _buildCoffeeItem(context, "Arabika Dark", "Arabika_dark.png"),
              ],
            ),
            const SizedBox(height: 35),
            _buildSectionHeader(
              Icons.local_fire_department_rounded,
              "Biji Kopi Robusta",
            ),
            const SizedBox(height: 16),
            Row(
              children: [
                _buildCoffeeItem(context, "Robusta Light", "Robusta_light.png"),
                const SizedBox(width: 12),
                _buildCoffeeItem(
                  context,
                  "Robusta Medium",
                  "Robusta_medium.png",
                ),
                const SizedBox(width: 12),
                _buildCoffeeItem(context, "Robusta Dark", "Robusta_dark.png"),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSectionHeader(IconData icon, String title) {
    return Row(
      children: [
        Icon(icon, color: AppColors.brownMain, size: 20),
        const SizedBox(width: 8),
        Text(
          title,
          style: TextStyle(
            fontFamily: 'Montserrat',
            fontSize: 18,
            fontWeight: FontWeight.w800,
            color: AppColors.textDark,
            letterSpacing: -0.5,
          ),
        ),
      ],
    );
  }

  Widget _buildHeroCard() {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        gradient: const LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [Color(0xFF4E342E), AppColors.heroCokelat, Color(0xFF8D6E63)],
          stops: [0.0, 0.4, 1.0],
        ),
        borderRadius: BorderRadius.circular(28),
        boxShadow: [
          BoxShadow(
            color: AppColors.heroCokelat.withOpacity(0.3),
            blurRadius: 25,
            offset: const Offset(0, 15),
          ),
        ],
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(28),
        child: Stack(
          children: [
            Positioned(
              right: -50,
              bottom: -50,
              child: CircleAvatar(
                radius: 100,
                backgroundColor: Colors.white.withOpacity(0.03),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 22, vertical: 28),
              child: Row(
                children: [
                  Expanded(
                    flex: 3,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "Kenali 3 tingkat roasting biji kopi robusta dan arabika dengan cepat!",
                          style: TextStyle(
                            fontFamily: 'Montserrat',
                            color: Colors.white,
                            fontWeight: FontWeight.w800,
                            fontSize: 15,
                            height: 1.4,
                          ),
                        ),
                        const SizedBox(height: 20),
                        ElevatedButton(
                          onPressed: onCobaSekarang,
                          style: ElevatedButton.styleFrom(
                            backgroundColor: AppColors.buttonCream,
                            foregroundColor: AppColors.textDark,
                            elevation: 0,
                            padding: const EdgeInsets.symmetric(
                              horizontal: 16,
                              vertical: 12,
                            ),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text(
                                "Coba Sekarang",
                                style: TextStyle(
                                  fontFamily: 'Montserrat',
                                  fontWeight: FontWeight.w700,
                                  fontSize: 11,
                                ),
                              ),
                              const SizedBox(width: 8),
                              const Icon(Icons.arrow_forward_rounded, size: 14),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(width: 15),
                  Expanded(
                    flex: 2,
                    child: Container(
                      height: 100,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black.withOpacity(0.1),
                            blurRadius: 15,
                            offset: const Offset(0, 8),
                          ),
                        ],
                      ),
                      padding: const EdgeInsets.all(12),
                      child: Image.asset(
                        'assets/images/bean.png',
                        fit: BoxFit.contain,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatCard(String value, String label) {
    return Expanded(
      child: Container(
        height: 110,
        decoration: BoxDecoration(
          color: AppColors.cardWhite,
          borderRadius: BorderRadius.circular(24),
          border: Border.all(color: Colors.black.withOpacity(0.04)),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.03),
              blurRadius: 15,
              offset: const Offset(0, 8),
            ),
          ],
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              value,
              style: TextStyle(
                fontFamily: 'Montserrat',
                fontSize: 17,
                fontWeight: FontWeight.w700,
                color: AppColors.greenMain,
              ),
            ),
            const SizedBox(height: 4),
            Text(
              label,
              style: TextStyle(
                fontFamily: 'Montserrat',
                fontSize: 10,
                color: AppColors.textDark.withOpacity(0.5),
                fontWeight: FontWeight.w600,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCoffeeItem(
    BuildContext context,
    String title,
    String imagePath,
  ) {
    return Expanded(
      child: Container(
        height: 120,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(24),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.04),
              blurRadius: 15,
              offset: const Offset(0, 8),
            ),
          ],
        ),
        child: Material(
          color: AppColors.cardWhite,
          borderRadius: BorderRadius.circular(24),
          child: InkWell(
            onTap: () {
              Navigator.pushNamed(
                context,
                AppRoutes.detail,
                arguments: {
                  'title': title,
                  'imagePath': 'assets/images/$imagePath',
                },
              );
            },
            borderRadius: BorderRadius.circular(24),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Hero(
                  tag: title,
                  child: Image.asset(
                    'assets/images/$imagePath',
                    height: 48,
                    fit: BoxFit.contain,
                    errorBuilder: (c, e, s) => const Icon(Icons.coffee),
                  ),
                ),
                const SizedBox(height: 10),
                Text(
                  title.split(" ").last,
                  style: TextStyle(
                    fontFamily: 'Montserrat',
                    fontSize: 11,
                    fontWeight: FontWeight.w700,
                    color: AppColors.textDark,
                  ),
                ),
                Text(
                  title.split(" ").first,
                  style: TextStyle(
                    fontFamily: 'Montserrat',
                    fontSize: 8,
                    fontWeight: FontWeight.w500,
                    color: AppColors.textDark.withOpacity(0.4),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
