import 'package:flutter/material.dart';

import 'package:asciineverdie/l10n/app_localizations.dart';
import 'package:asciineverdie/src/core/model/game_state.dart';
import 'package:asciineverdie/src/core/util/roman.dart' show intToRoman;

/// 스토리 페이지 (캐로셀)
///
/// Plot 진행 상황 표시.
class StoryPage extends StatelessWidget {
  const StoryPage({
    super.key,
    required this.plotStageCount,
    required this.plot,
  });

  final int plotStageCount;
  final ProgressBarState plot;

  @override
  Widget build(BuildContext context) {
    final localizations = L10n.of(context);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        // 헤더
        _buildSectionHeader(context, localizations.plotDevelopment),

        // Plot 목록
        Expanded(child: _buildPlotList(context)),

        // Plot 프로그레스
        _buildProgressSection(context),
      ],
    );
  }

  Widget _buildSectionHeader(BuildContext context, String title) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      color: Theme.of(context).colorScheme.primaryContainer,
      child: Text(
        title,
        style: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 18,
          color: Theme.of(context).colorScheme.onPrimaryContainer,
        ),
      ),
    );
  }

  Widget _buildPlotList(BuildContext context) {
    final localizations = L10n.of(context);

    if (plotStageCount == 0) {
      return Center(
        child: Text(
          localizations.prologue,
          style: const TextStyle(fontSize: 18, color: Colors.grey),
        ),
      );
    }

    return ListView.builder(
      itemCount: plotStageCount,
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      itemBuilder: (context, index) {
        final isCompleted = index < plotStageCount - 1;
        final label = index == 0
            ? localizations.prologue
            : localizations.actNumber(intToRoman(index));

        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 4),
          child: Row(
            children: [
              Icon(
                isCompleted ? Icons.check_box : Icons.check_box_outline_blank,
                size: 18,
                color: isCompleted ? Colors.green : Colors.grey,
              ),
              const SizedBox(width: 8),
              Expanded(
                child: Text(
                  label,
                  style: TextStyle(
                    fontSize: 18,
                    decoration: isCompleted ? TextDecoration.lineThrough : null,
                    color: isCompleted ? Colors.grey : null,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildProgressSection(BuildContext context) {
    final progress = plot.max > 0
        ? (plot.position / plot.max).clamp(0.0, 1.0)
        : 0.0;

    return Padding(
      padding: const EdgeInsets.all(12),
      child: LinearProgressIndicator(
        value: progress,
        backgroundColor: Colors.purple.withValues(alpha: 0.2),
        valueColor: const AlwaysStoppedAnimation<Color>(Colors.purple),
        minHeight: 12,
      ),
    );
  }
}
