import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../core/constants/ui_constants.dart';
import '../../core/providers/dynamic_theme_provider.dart';

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

  @override
  Widget build(BuildContext context) {
    return Consumer<DynamicThemeProvider>(
      builder: (context, dynamicThemeProvider, child) {
        if (!dynamicThemeProvider.isUsingDynamicTheme) {
          return const SizedBox.shrink();
        }

        final colorScheme = dynamicThemeProvider.dynamicColorScheme;
        final logoColors = dynamicThemeProvider.logoColors;
        final paletteInfo = dynamicThemeProvider.getColorPaletteInfo();

        if (colorScheme == null || logoColors.isEmpty) {
          return const SizedBox.shrink();
        }

        return Container(
          margin: const EdgeInsets.all(UIConstants.spacing16),
          padding: const EdgeInsets.all(UIConstants.spacing20),
          decoration: BoxDecoration(
            color: colorScheme.surface,
            borderRadius: BorderRadius.circular(UIConstants.radius16),
            border: Border.all(
              color: colorScheme.outline.withOpacity(0.2),
            ),
            boxShadow: [
              BoxShadow(
                color: colorScheme.shadow.withOpacity(0.1),
                blurRadius: 8,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Header
              Row(
                children: [
                  Icon(
                    Icons.palette,
                    color: colorScheme.primary,
                    size: UIConstants.iconSizeLarge,
                  ),
                  const SizedBox(width: UIConstants.spacing12),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Dynamic Theme Generated',
                          style:
                              Theme.of(context).textTheme.titleLarge?.copyWith(
                                    color: colorScheme.onSurface,
                                    fontWeight: FontWeight.w600,
                                  ),
                        ),
                        Text(
                          dynamicThemeProvider.getThemeDescription(),
                          style:
                              Theme.of(context).textTheme.bodyMedium?.copyWith(
                                    color: colorScheme.onSurfaceVariant,
                                  ),
                        ),
                      ],
                    ),
                  ),
                  IconButton(
                    onPressed: () => dynamicThemeProvider.resetToDefaultTheme(),
                    icon: Icon(
                      Icons.refresh,
                      color: colorScheme.primary,
                    ),
                    tooltip: 'Reset to Default Theme',
                  ),
                ],
              ),

              const SizedBox(height: UIConstants.spacing20),

              // Color Palette
              Text(
                'Color Palette',
                style: Theme.of(context).textTheme.titleMedium?.copyWith(
                      color: colorScheme.onSurface,
                      fontWeight: FontWeight.w600,
                    ),
              ),
              const SizedBox(height: UIConstants.spacing12),

              // Color Swatches
              Wrap(
                spacing: UIConstants.spacing12,
                runSpacing: UIConstants.spacing12,
                children: logoColors.take(5).map((color) {
                  final index = logoColors.indexOf(color);
                  final labels = [
                    'Primary',
                    'Secondary',
                    'Tertiary',
                    'Accent 1',
                    'Accent 2'
                  ];

                  return Column(
                    children: [
                      Container(
                        width: 60,
                        height: 60,
                        decoration: BoxDecoration(
                          color: color,
                          shape: BoxShape.circle,
                          border: Border.all(
                            color: colorScheme.outline.withOpacity(0.3),
                            width: 2,
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: color.withOpacity(0.3),
                              blurRadius: 8,
                              offset: const Offset(0, 2),
                            ),
                          ],
                        ),
                        child: Center(
                          child: Icon(
                            Icons.check,
                            color: _getContrastColor(color),
                            size: 24,
                          ),
                        ),
                      ),
                      const SizedBox(height: UIConstants.spacing4),
                      Text(
                        index < labels.length
                            ? labels[index]
                            : 'Color ${index + 1}',
                        style: Theme.of(context).textTheme.bodySmall?.copyWith(
                              color: colorScheme.onSurfaceVariant,
                              fontWeight: FontWeight.w500,
                            ),
                      ),
                    ],
                  );
                }).toList(),
              ),

              const SizedBox(height: UIConstants.spacing20),

              // Theme Info
              Container(
                padding: const EdgeInsets.all(UIConstants.spacing16),
                decoration: BoxDecoration(
                  color: colorScheme.surfaceVariant.withOpacity(0.3),
                  borderRadius: BorderRadius.circular(UIConstants.radius12),
                  border: Border.all(
                    color: colorScheme.outline.withOpacity(0.2),
                  ),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Theme Information',
                      style: Theme.of(context).textTheme.titleSmall?.copyWith(
                            color: colorScheme.onSurface,
                            fontWeight: FontWeight.w600,
                          ),
                    ),
                    const SizedBox(height: UIConstants.spacing8),
                    _buildInfoRow(
                        'Total Colors', '${paletteInfo['totalColors']}'),
                    _buildInfoRow('Description', paletteInfo['description']),
                    if (paletteInfo['characteristics'] != null) ...[
                      _buildInfoRow(
                          'Warm Colors',
                          paletteInfo['characteristics']['warm']
                              ? 'Yes'
                              : 'No'),
                      _buildInfoRow(
                          'Cool Colors',
                          paletteInfo['characteristics']['cool']
                              ? 'Yes'
                              : 'No'),
                      _buildInfoRow(
                          'Neutral Colors',
                          paletteInfo['characteristics']['neutral']
                              ? 'Yes'
                              : 'No'),
                    ],
                  ],
                ),
              ),

              const SizedBox(height: UIConstants.spacing16),

              // Preview Buttons
              Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () {},
                      style: ElevatedButton.styleFrom(
                        backgroundColor: colorScheme.primary,
                        foregroundColor: colorScheme.onPrimary,
                        shape: RoundedRectangleBorder(
                          borderRadius:
                              BorderRadius.circular(UIConstants.radius12),
                        ),
                      ),
                      child: const Text('Primary Button'),
                    ),
                  ),
                  const SizedBox(width: UIConstants.spacing12),
                  Expanded(
                    child: OutlinedButton(
                      onPressed: () {},
                      style: OutlinedButton.styleFrom(
                        foregroundColor: colorScheme.primary,
                        side: BorderSide(color: colorScheme.outline),
                        shape: RoundedRectangleBorder(
                          borderRadius:
                              BorderRadius.circular(UIConstants.radius12),
                        ),
                      ),
                      child: const Text('Secondary Button'),
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.only(bottom: UIConstants.spacing4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            label,
            style: const TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.w500,
            ),
          ),
          Text(
            value,
            style: const TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.w400,
            ),
          ),
        ],
      ),
    );
  }

  Color _getContrastColor(Color backgroundColor) {
    final luminance = backgroundColor.computeLuminance();
    return luminance > 0.5 ? Colors.black : Colors.white;
  }
}
