import 'package:flutter/material.dart';

import 'package:asciineverdie/l10n/app_localizations.dart';
import 'package:asciineverdie/src/core/model/game_state.dart';

/// 퀘스트 페이지 (캐로셀)
///
/// 퀘스트 히스토리 및 현재 퀘스트 진행 상황 표시.
class QuestPage extends StatelessWidget {
  const QuestPage({super.key, required this.questHistory, required this.quest});

  final List<HistoryEntry> questHistory;
  final ProgressBarState quest;

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

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

        // 퀘스트 목록
        Expanded(child: _buildQuestList(context)),

        // 퀘스트 프로그레스
        _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 _buildQuestList(BuildContext context) {
    final localizations = L10n.of(context);

    if (questHistory.isEmpty) {
      return Center(
        child: Text(
          localizations.noActiveQuests,
          style: const TextStyle(fontSize: 18, color: Colors.grey),
        ),
      );
    }

    return ListView.builder(
      itemCount: questHistory.length,
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      itemBuilder: (context, index) {
        final entry = questHistory[index];
        final isCurrentQuest =
            index == questHistory.length - 1 && !entry.isComplete;

        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 4),
          child: Row(
            children: [
              if (isCurrentQuest)
                const Icon(Icons.arrow_right, size: 18, color: Colors.blue)
              else
                Icon(
                  entry.isComplete
                      ? Icons.check_box
                      : Icons.check_box_outline_blank,
                  size: 18,
                  color: entry.isComplete ? Colors.green : Colors.grey,
                ),
              const SizedBox(width: 8),
              Expanded(
                child: Text(
                  entry.caption,
                  style: TextStyle(
                    fontSize: 18,
                    decoration: entry.isComplete
                        ? TextDecoration.lineThrough
                        : null,
                    color: isCurrentQuest
                        ? Colors.blue
                        : entry.isComplete
                        ? Colors.grey
                        : null,
                    fontWeight: isCurrentQuest ? FontWeight.bold : null,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildProgressSection(BuildContext context) {
    final localizations = L10n.of(context);
    final progress = quest.max > 0
        ? (quest.position / quest.max).clamp(0.0, 1.0)
        : 0.0;
    final percentage = (progress * 100).toInt();

    return Padding(
      padding: const EdgeInsets.all(12),
      child: Column(
        children: [
          LinearProgressIndicator(
            value: progress,
            backgroundColor: Colors.green.withValues(alpha: 0.2),
            valueColor: const AlwaysStoppedAnimation<Color>(Colors.green),
            minHeight: 12,
          ),
          const SizedBox(height: 4),
          Text(
            localizations.percentComplete(percentage),
            style: TextStyle(fontSize: 15, color: Colors.grey.shade600),
          ),
        ],
      ),
    );
  }
}
