import 'package:flutter/material.dart';
import 'package:slrpg_app/l10n/app_localizations.dart';
import '../../../../core/theme/app_theme.dart';
import '../../../../shared/domain/entities/exercise.dart';
import '../../../../shared/domain/entities/workout_set.dart';
import '../../../../shared/domain/logic/plate_calculator.dart';
import '../widgets/plate_visualizer.dart';

class WorkoutContent extends StatelessWidget {
  final Exercise exercise;
  final WorkoutSet currentSet;
  final int currentSetIndex;
  final int totalSets;
  final PlateLoadResult plateResult;
  final VoidCallback onCompletePress;
  final VoidCallback onShowGuide;

  const WorkoutContent({
    super.key,
    required this.exercise,
    required this.currentSet,
    required this.currentSetIndex,
    required this.totalSets,
    required this.plateResult,
    required this.onCompletePress,
    required this.onShowGuide,
  });

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;

    return Container(
      decoration: BoxDecoration(
        color: AppTheme.surfaceColor.withValues(alpha: 0.95),
        borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.5),
            blurRadius: 20,
            offset: const Offset(0, -5),
          )
        ],
      ),
      child: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              padding: const EdgeInsets.fromLTRB(24, 24, 24, 100),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        exercise.exerciseName,
                        style: Theme.of(context)
                            .textTheme
                            .headlineMedium
                            ?.copyWith(
                              color: AppTheme.primaryColor,
                              fontWeight: FontWeight.bold,
                            ),
                        textAlign: TextAlign.center,
                      ),
                      IconButton(
                        icon: const Icon(Icons.info_outline,
                            color: Colors.white54),
                        onPressed: onShowGuide,
                      ),
                    ],
                  ),
                  Text(
                    l10n.battleSet(currentSetIndex + 1, totalSets),
                    style: Theme.of(context).textTheme.titleMedium?.copyWith(
                          color: Colors.white70,
                        ),
                  ),
                  const SizedBox(height: 24),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      _InfoBox(
                        label: l10n.battleWeight,
                        value: '${currentSet.targetWeightTotal} ${l10n.unitKg}',
                      ),
                      _InfoBox(
                        label: l10n.battleReps,
                        value:
                            '${currentSet.repsTarget}${currentSet.isAmrap ? "+" : ""}',
                      ),
                    ],
                  ),
                  const SizedBox(height: 24),
                  if (plateResult.bandAssistance != null)
                    _BandAssistanceInfo(
                      assistance: plateResult.bandAssistance!,
                      l10n: l10n,
                    )
                  else
                    PlateVisualizer(
                      plateConfiguration: plateResult.plateConfiguration,
                      isTwoSided: _isTwoSided(exercise.exerciseId),
                      exerciseName: exercise.exerciseName,
                    ),
                  const SizedBox(height: 32),
                ],
              ),
            ),
          ),
          _CompleteButton(onPressed: onCompletePress),
        ],
      ),
    );
  }

  bool _isTwoSided(String exerciseId) {
    return ['squat', 'row', 'bench', 'rdl', 'ohp', 'curl'].contains(exerciseId);
  }
}

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

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(
          label,
          style: const TextStyle(
            color: Colors.grey,
            fontSize: 12,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          value,
          style: const TextStyle(
            color: Colors.white,
            fontSize: 24,
            fontWeight: FontWeight.bold,
          ),
        ),
      ],
    );
  }
}

class _BandAssistanceInfo extends StatelessWidget {
  final String assistance;
  final AppLocalizations l10n;

  const _BandAssistanceInfo({
    required this.assistance,
    required this.l10n,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: AppTheme.primaryColor.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppTheme.primaryColor),
      ),
      child: Row(
        children: [
          const Icon(Icons.help, color: AppTheme.primaryColor, size: 32),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  l10n.battleAssistance,
                  style: const TextStyle(
                    color: AppTheme.primaryColor,
                    fontSize: 12,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  assistance,
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _CompleteButton extends StatelessWidget {
  final VoidCallback onPressed;

  const _CompleteButton({required this.onPressed});

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;

    return Container(
      color: AppTheme.surfaceColor,
      padding: const EdgeInsets.all(16),
      child: SafeArea(
        top: false,
        child: SizedBox(
          width: double.infinity,
          child: ElevatedButton(
            onPressed: onPressed,
            style: ElevatedButton.styleFrom(
              padding: const EdgeInsets.symmetric(vertical: 16),
              backgroundColor: AppTheme.primaryColor,
              foregroundColor: Colors.black,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
            ),
            child: Text(
              l10n.battleCompleteSet,
              style: const TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                letterSpacing: 1.2,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
