import 'package:flutter/material.dart';
import 'package:slrpg_app/l10n/app_localizations.dart';
import '../../../../core/theme/app_theme.dart';
import '../../../../shared/domain/models/exercise_guide.dart';

class ExerciseGuideSheet extends StatelessWidget {
  final String exerciseId;

  const ExerciseGuideSheet({super.key, required this.exerciseId});

  @override
  Widget build(BuildContext context) {
    String lookupId = exerciseId;
    if (exerciseId.contains('kb_snatch')) lookupId = 'kb_snatch';

    final l10n = AppLocalizations.of(context)!;
    final library = ExerciseGuide.getLibrary(l10n);
    final guide = library[exerciseId];

    if (guide == null) {
      return Container(
        padding: const EdgeInsets.all(32),
        child: Text(l10n.guideNotFound,
            textAlign: TextAlign.center, style: const TextStyle(color: Colors.grey)),
      );
    }

    return DraggableScrollableSheet(
      initialChildSize: 0.85,
      minChildSize: 0.5,
      maxChildSize: 0.95,
      builder: (context, scrollController) {
        return Container(
          decoration: const BoxDecoration(
            color: AppTheme.surfaceColor,
            borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
          ),
          child: Column(
            children: [
              const SizedBox(height: 12),
              Container(
                width: 40,
                height: 4,
                decoration: BoxDecoration(
                  color: Colors.grey[700],
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
              Expanded(
                child: ListView(
                  controller: scrollController,
                  padding: const EdgeInsets.all(24),
                  children: [
                    Text(
                      guide.title.toUpperCase(),
                      style:
                          Theme.of(context).textTheme.headlineMedium?.copyWith(
                                color: AppTheme.primaryColor,
                                fontWeight: FontWeight.bold,
                              ),
                      textAlign: TextAlign.center,
                    ),
                    const SizedBox(height: 8),
                    _buildDifficultyBadge(context, guide.difficulty),
                    const SizedBox(height: 24),
                    Container(
                      padding: const EdgeInsets.all(16),
                      decoration: BoxDecoration(
                        color: AppTheme.backgroundColor,
                        borderRadius: BorderRadius.circular(12),
                        border: Border.all(color: Colors.white12),
                      ),
                      child: Text(
                        '"${guide.rpgLore}"',
                        style: const TextStyle(
                          fontStyle: FontStyle.italic,
                          color: AppTheme.textSecondary,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    const SizedBox(height: 32),
                    _buildSectionTitle(context, l10n.guideExecution),
                    const SizedBox(height: 16),
                    ...guide.steps
                        .asMap()
                        .entries
                        .map((entry) => _buildStep(entry.key + 1, entry.value)),
                    const SizedBox(height: 32),
                    _buildSectionTitle(context, l10n.guideMistakes),
                    const SizedBox(height: 16),
                    ...guide.commonMistakes.map((m) => _buildMistake(m)),
                    const SizedBox(height: 32),
                    _buildSectionTitle(context, l10n.guideAttributes),
                    const SizedBox(height: 16),
                    Wrap(
                      spacing: 8,
                      runSpacing: 8,
                      children: guide.muscles
                          .map((m) => Chip(
                                label: Text(m),
                                backgroundColor: AppTheme.primaryColor
                                    .withValues(alpha: 0.1),
                                labelStyle: const TextStyle(
                                    color: AppTheme.primaryColor),
                                side: BorderSide.none,
                              ))
                          .toList(),
                    ),
                    const SizedBox(height: 40),
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildDifficultyBadge(BuildContext context, String diff) {
    final l10n = AppLocalizations.of(context)!;
    Color color;
    String text = diff;

    switch (diff) {
      case 'Novice':
        color = Colors.green;
        text = l10n.guideDiffNovice;
        break;
      case 'Adept':
        color = Colors.orange;
        text = l10n.guideDiffAdept;
        break;
      case 'Master':
        color = AppTheme.errorColor;
        text = l10n.guideDiffMaster;
        break;
      default:
        color = Colors.grey;
    }

    return Center(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
        decoration: BoxDecoration(
          color: color.withValues(alpha: 0.2),
          borderRadius: BorderRadius.circular(12),
          border: Border.all(color: color.withValues(alpha: 0.5)),
        ),
        child: Text(
          text.toUpperCase(),
          style: TextStyle(
              color: color, fontSize: 12, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  Widget _buildSectionTitle(BuildContext context, String title) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: const TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
            letterSpacing: 1.2,
          ),
        ),
        const Divider(
            color: AppTheme.primaryColor, thickness: 2, endIndent: 250),
      ],
    );
  }

  Widget _buildStep(int index, String text) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 12.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 24,
            height: 24,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: AppTheme.primaryColor,
              shape: BoxShape.circle,
            ),
            child: Text('$index',
                style: const TextStyle(
                    color: Colors.black, fontWeight: FontWeight.bold)),
          ),
          const SizedBox(width: 16),
          Expanded(
              child: Text(text,
                  style: const TextStyle(color: Colors.white70, height: 1.4))),
        ],
      ),
    );
  }

  Widget _buildMistake(String text) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8.0),
      child: Row(
        children: [
          const Icon(Icons.close, color: AppTheme.errorColor, size: 20),
          const SizedBox(width: 12),
          Expanded(
              child: Text(text, style: const TextStyle(color: Colors.white70))),
        ],
      ),
    );
  }
}
