import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../domain/models/grocery_item.dart';
import 'item_detail_cubit.dart';
import 'item_detail_state.dart';

class ItemDetailScreen extends StatelessWidget {
  final String barcode;

  const ItemDetailScreen({super.key, required this.barcode});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ItemDetailCubit, ItemDetailState>(
      builder: (context, state) {
        if (state.isLoading) {
          return Scaffold(
            appBar: AppBar(),
            body: const Center(child: CircularProgressIndicator()),
          );
        }
        if (state.item == null) {
          return Scaffold(
            appBar: AppBar(title: const Text('Not Found')),
            body: const Center(child: Text('Item not found')),
          );
        }
        return _ItemDetailContent(item: state.item!);
      },
    );
  }
}

class _ItemDetailContent extends StatelessWidget {
  final GroceryItem item;

  const _ItemDetailContent({required this.item});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Scaffold(
      appBar: AppBar(title: Text(item.name)),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Row(
                  children: [
                    Container(
                      width: 80,
                      height: 80,
                      decoration: BoxDecoration(
                        color: theme.colorScheme.primaryContainer,
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Icon(
                        _iconForCategory(item.category),
                        size: 40,
                        color: theme.colorScheme.onPrimaryContainer,
                      ),
                    ),
                    const SizedBox(width: 16),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(item.name, style: theme.textTheme.titleLarge),
                          const SizedBox(height: 4),
                          Text(
                            item.brand,
                            style: theme.textTheme.bodyMedium?.copyWith(
                              color: theme.colorScheme.onSurfaceVariant,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            _DetailRow(label: 'Category', value: item.category),
            _DetailRow(label: 'Barcode', value: item.barcode),
            if (item.price != null)
              _DetailRow(
                label: 'Price',
                value: '\$${item.price!.toStringAsFixed(2)}',
              ),
            if (item.description != null) ...[
              const SizedBox(height: 16),
              Text('Description', style: theme.textTheme.titleMedium),
              const SizedBox(height: 8),
              Text(item.description!, style: theme.textTheme.bodyLarge),
            ],
          ],
        ),
      ),
    );
  }

  IconData _iconForCategory(String category) {
    return switch (category.toLowerCase()) {
      'dairy' => Icons.water_drop,
      'bakery' => Icons.bakery_dining,
      'eggs' => Icons.egg,
      'produce' => Icons.eco,
      'meat' => Icons.restaurant,
      'beverages' => Icons.local_drink,
      _ => Icons.shopping_bag,
    };
  }
}

class _DetailRow extends StatelessWidget {
  final String label;
  final String value;

  const _DetailRow({required this.label, required this.value});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 100,
            child: Text(
              label,
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                    color: Theme.of(context).colorScheme.onSurfaceVariant,
                  ),
            ),
          ),
          Expanded(
            child: Text(value, style: Theme.of(context).textTheme.bodyLarge),
          ),
        ],
      ),
    );
  }
}