import 'package:flutter/material.dart';
import '../model/filament_model.dart';

class FilamentCard extends StatelessWidget {
  final FilamentModel filament;
  final VoidCallback? onTap;
  final VoidCallback? onEdit;
  final VoidCallback? onDelete;

  const FilamentCard({
    super.key,
    required this.filament,
    this.onTap,
    this.onEdit,
    this.onDelete,
  });

  Color _getColorFromString(String colorName) {
    final colorMap = {
      'red': Colors.red,
      'blue': Colors.blue,
      'green': Colors.green,
      'yellow': Colors.yellow,
      'black': Colors.black,
      'white': Colors.white,
      'orange': Colors.orange,
      'purple': Colors.purple,
      'pink': Colors.pink,
      'grey': Colors.grey,
      'brown': Colors.brown,
    };

    return colorMap[colorName.toLowerCase()] ?? Colors.grey;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [Colors.white, Colors.blue.shade50],
        ),
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(15),
            blurRadius: 10,
            offset: Offset(0, 4),
          ),
        ],
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(16),
          child: Padding(
            padding: EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Header with color indicator and name
                Row(
                  children: [
                    // Color indicator
                    Container(
                      width: 48,
                      height: 48,
                      decoration: BoxDecoration(
                        color: _getColorFromString(filament.color),
                        borderRadius: BorderRadius.circular(12),
                        border: Border.all(
                          color: Colors.grey.shade300,
                          width: 2,
                        ),
                        boxShadow: [
                          BoxShadow(
                            color: _getColorFromString(
                              filament.color,
                            ).withAlpha(100),
                            blurRadius: 8,
                            offset: Offset(0, 2),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(width: 12),
                    // Name and Type
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            filament.name,
                            style: TextStyle(
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                              color: Colors.deepPurple.shade700,
                            ),
                          ),
                          SizedBox(height: 4),
                          Container(
                            padding: EdgeInsets.symmetric(
                              horizontal: 8,
                              vertical: 4,
                            ),
                            decoration: BoxDecoration(
                              color: Colors.deepPurple.shade100,
                              borderRadius: BorderRadius.circular(8),
                            ),
                            child: Text(
                              filament.type,
                              style: TextStyle(
                                fontSize: 12,
                                fontWeight: FontWeight.w600,
                                color: Colors.deepPurple.shade700,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    // Action buttons
                    if (onEdit != null)
                      IconButton(
                        icon: Icon(Icons.edit_outlined),
                        color: Colors.blue.shade600,
                        onPressed: onEdit,
                      ),
                    if (onDelete != null)
                      IconButton(
                        icon: Icon(Icons.delete_outline),
                        color: Colors.red.shade400,
                        onPressed: onDelete,
                      ),
                  ],
                ),
                SizedBox(height: 16),

                // Details Grid
                Wrap(
                  spacing: 12,
                  runSpacing: 12,
                  children: [
                    if (filament.manufacturer != null)
                      _buildDetailChip(
                        icon: Icons.business,
                        label: filament.manufacturer!,
                        color: Colors.blue,
                      ),
                    _buildDetailChip(
                      icon: Icons.scale,
                      label: '${filament.weight - filament.weightUsed}g',
                      color: Colors.green,
                    ),
                    _buildDetailChip(
                      icon: Icons.euro,
                      label: filament.price.toStringAsFixed(2),
                      color: Colors.orange,
                    ),
                    if (filament.pices > 0)
                      _buildDetailChip(
                        icon: Icons.inventory_2,
                        label: '${filament.pices} Stk.',
                        color: Colors.purple,
                      ),
                    _buildDetailChip(
                        icon: Icons.scale,
                        label: '${filament.weightUsed}g',
                        color: Colors.red,
                      ),
                  ],
                ),

                // Temperature info
                if (filament.printingTemp != null || filament.bedTemp != null)
                  Padding(
                    padding: EdgeInsets.only(top: 12),
                    child: Container(
                      padding: EdgeInsets.all(12),
                      decoration: BoxDecoration(
                        color: Colors.orange.shade50,
                        borderRadius: BorderRadius.circular(12),
                        border: Border.all(
                          color: Colors.orange.shade200,
                          width: 1,
                        ),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          if (filament.printingTemp != null)
                            _buildTempInfo(
                              icon: Icons.print,
                              label: 'Druck',
                              temp: '${filament.printingTemp}°C',
                            ),
                          if (filament.printingTemp != null &&
                              filament.bedTemp != null)
                            Container(
                              width: 1,
                              height: 30,
                              color: Colors.orange.shade200,
                            ),
                          if (filament.bedTemp != null)
                            _buildTempInfo(
                              icon: Icons.bed,
                              label: 'Bett',
                              temp: '${filament.bedTemp}°C',
                            ),
                        ],
                      ),
                    ),
                  ),

                // Notes
                if (filament.notes != null && filament.notes!.isNotEmpty)
                  Padding(
                    padding: EdgeInsets.only(top: 12),
                    child: Container(
                      padding: EdgeInsets.all(12),
                      decoration: BoxDecoration(
                        color: Colors.amber.shade50,
                        borderRadius: BorderRadius.circular(12),
                        border: Border.all(
                          color: Colors.amber.shade200,
                          width: 1,
                        ),
                      ),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Icon(
                            Icons.note_alt_outlined,
                            size: 18,
                            color: Colors.amber.shade700,
                          ),
                          SizedBox(width: 8),
                          Expanded(
                            child: Text(
                              filament.notes!,
                              style: TextStyle(
                                fontSize: 13,
                                color: Colors.grey.shade700,
                                fontStyle: FontStyle.italic,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),

                // Purchase date
                if (filament.purchaseDate != null)
                  Padding(
                    padding: EdgeInsets.only(top: 8),
                    child: Row(
                      children: [
                        Icon(
                          Icons.calendar_today,
                          size: 14,
                          color: Colors.grey.shade500,
                        ),
                        SizedBox(width: 6),
                        Text(
                          'Gekauft: ${filament.purchaseDate}',
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey.shade600,
                          ),
                        ),
                      ],
                    ),
                  ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildDetailChip({
    required IconData icon,
    required String label,
    required MaterialColor color,
  }) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 6),
      decoration: BoxDecoration(
        color: color.shade50,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: color.shade200, width: 1),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, size: 16, color: color.shade700),
          SizedBox(width: 6),
          Text(
            label,
            style: TextStyle(
              fontSize: 13,
              fontWeight: FontWeight.w500,
              color: color.shade700,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildTempInfo({
    required IconData icon,
    required String label,
    required String temp,
  }) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Icon(icon, size: 18, color: Colors.orange.shade700),
        SizedBox(width: 6),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              label,
              style: TextStyle(
                fontSize: 11,
                color: Colors.grey.shade600,
                fontWeight: FontWeight.w500,
              ),
            ),
            Text(
              temp,
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.bold,
                color: Colors.orange.shade800,
              ),
            ),
          ],
        ),
      ],
    );
  }
}
