// packages/feature_game_sequence/lib/screens/sequence_game_screen.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:feature_common/feature_common.dart';
import 'package:service_api/service_api.dart';
import '../controllers/sequence_game_controller.dart';
import '../models/sequence_models.dart';
import '../widgets/sequence_button_widget.dart';

class SequenceGameScreen extends StatefulWidget {
  const SequenceGameScreen({super.key});

  @override
  State<SequenceGameScreen> createState() => _SequenceGameScreenState();
}

class _SequenceGameScreenState extends State<SequenceGameScreen> {
  bool _isDialogShowing = false;
  Timer? _presentationTimer;
  
  @override
  void initState() {
    super.initState();
    final controller = Provider.of<SequenceGameController>(context, listen: false);
    
    controller.addListener(() {
      if (controller.currentState == SequenceGameState.presenting && _presentationTimer == null) {
        _runPresentationAnimation(controller);
      }
    });
  }

  @override
  void dispose() {
    _presentationTimer?.cancel();
    super.dispose();
  }

  void _runPresentationAnimation(SequenceGameController controller) {
    _presentationTimer?.cancel();
    
    final List<SequenceButtonId> sequence = controller.currentSequence;
    final double speed = controller.difficulty.presentationSpeed;
    final Duration stepDuration = Duration(milliseconds: (speed * 1000).round());
    
    int step = 0;
    
    _presentationTimer = Timer.periodic(stepDuration, (timer) {
      if (step < sequence.length) {
        controller.setActivePresentation(sequence[step]); 
        controller.presentationStep = step + 1;
        step++;
      } else {
        timer.cancel();
        controller.setActivePresentation(null); 
        _presentationTimer = null; 

        if (mounted) {
           controller.runPostPresentationDelay();
        }
      }
    });
  }

  void _showGameCompletion(SequenceGameController controller) async {
    String formatSequenceScore(int primary, int? secondary) {
      final roundsCompleted = primary;
      final roundsFailed = secondary ?? 0;
      return '성공 ${roundsCompleted}회 / 실패 ${roundsFailed}회';
    }

    Future<void> saveSequenceProgress(String playerName) async {
      final bool isLevelClear = controller.maxAchievedLength >= controller.difficulty.maxGameLength;

      if (!isLevelClear) {
        debugPrint("레벨 클리어 실패: 도달(${controller.maxAchievedLength}) < 목표(${controller.difficulty.maxGameLength})");
        return; 
      }

      final identityService = IdentityService();
      final int currentMaxLevel = await identityService.getMaxUnlockedLevel(gameType: 'SEQUENCE');
      if (currentMaxLevel < 99) {
        if (controller.difficulty.levelIndex >= currentMaxLevel) {
          int nextLevelIndex = controller.difficulty.levelIndex + 1;
          if (nextLevelIndex > SequenceDifficulties.allDifficulties.length) {
            await identityService.saveMaxUnlockedLevel(99, gameType: 'SEQUENCE');
          } else {
            await identityService.saveMaxUnlockedLevel(nextLevelIndex, gameType: 'SEQUENCE');
          }
        }
      }
    }
    
    await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => GameCompletionScreen(
          args: GameResultArgs(
            gameType: 'SEQUENCE',
            contextId: controller.difficulty.contextId,
            primaryScore: controller.roundsCompleted,
            secondaryScore: controller.roundsFailed,
            userId: controller.userId,
            userName: controller.userName,
            scoreFormatter: formatSequenceScore,
            onProgressSave: saveSequenceProgress,
          ),
        ),
      ),
    );

    if (mounted && Navigator.canPop(context)) {
      Navigator.pop(context);
    }
  }

  // 🔽 [🔥 수정] 순서 제시 상태 표시 바
  Widget _buildSequenceDisplay(SequenceGameController controller) {
    final bool isPresenting = controller.currentState == SequenceGameState.presenting;
    
    // [🔥 핵심 수정] processing 상태도 입력 상태로 간주해야 점멸 현상이 사라짐
    final bool isInput = controller.currentState == SequenceGameState.input || 
                         controller.currentState == SequenceGameState.processing;
                         
    final bool isDelay = controller.currentState == SequenceGameState.postDelay;
    
    final int displayLength = controller.currentSequenceLength;
    final int stepsShown = controller.presentationStep;

    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List.generate(displayLength, (index) {
          
          String symbol = "?";
          if (isInput || isDelay) {
            if (index < controller.userInput.length) {
              symbol = controller.activeSymbolPool[controller.userInput[index].index];
            }
          } else if (isPresenting) {
            if (index < stepsShown) {
              symbol = controller.activeSymbolPool[controller.currentSequence[index].index];
            }
          }

          final bool isCurrentStep = isPresenting && index == stepsShown - 1;
          // [🔥 추가] 입력 중인 칸 강조
          final bool isCurrentInput = isInput && index == controller.userInput.length;

          return Container(
            width: 30,
            height: 30,
            margin: const EdgeInsets.symmetric(horizontal: 4),
            decoration: BoxDecoration(
              color: isCurrentStep ? Colors.orange.shade300 : Colors.grey.shade300,
              borderRadius: BorderRadius.circular(5),
              border: Border.all(
                // 입력 대기 커서 강조
                color: isCurrentInput ? Theme.of(context).primaryColor : Colors.transparent, 
                width: 2,
              )
            ),
            child: Center(
              child: Text(symbol, style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
                color: (isInput && index >= controller.userInput.length) ? Colors.black38 : Colors.black, 
              )),
            ),
          );
        }),
      ),
    );
  }
  
  Widget _buildFeedbackWidget(SequenceGameController controller, ThemeData theme) {
    if (controller.showFeedback) {
      final String text = controller.isLastInputCorrect ? "성공! 다음 순서로..." : "오답! 🚨";
      final Color color = controller.isLastInputCorrect ? Colors.green : theme.colorScheme.error;
      
      return Text(text, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: color));
    }
    
    if (controller.currentState == SequenceGameState.presenting) {
        return const Text(" ", style: TextStyle(fontSize: 24)); 
    }
    
    return const SizedBox.shrink();
  }


  @override
  Widget build(BuildContext context) {
    final controller = context.watch<SequenceGameController>();
    final state = controller.currentState;
    final theme = Theme.of(context); // 👈 [수정] theme 변수 정의 확인
    
    if (state == SequenceGameState.completed && !_isDialogShowing) {
      _isDialogShowing = true;
      Future.delayed(const Duration(milliseconds: 500), () { 
        if (mounted) _showGameCompletion(controller);
      });
    }

    String statusText;
    Color statusColor = Colors.grey;
    if (state == SequenceGameState.presenting) {
      statusText = "👀 순서 제시 중...";
      statusColor = Colors.orange;
    } else if (state == SequenceGameState.postDelay) {
      statusText = "준비! ☝️";
      statusColor = Colors.green;
    } else if (state == SequenceGameState.input || state == SequenceGameState.processing) {
      statusText = "👉 입력 대기 중 (${controller.remainingRoundTime.toStringAsFixed(1)}s)";
      statusColor = theme.primaryColor;
    } else if (state == SequenceGameState.completed) {
      bool isClear = controller.maxAchievedLength >= controller.difficulty.maxGameLength;
      statusText = isClear ? "목표 달성! 🎉" : "게임 종료 (실패)";
      statusColor = isClear ? Colors.green : Colors.red;
    } else {
      statusText = "시작 대기 중";
    }

    final List<SequenceButtonId> availableButtons = SequenceButtonId.values.sublist(0, controller.difficulty.buttonsCount);
    
    return Scaffold(
      appBar: AppBar(
        title: Text("길이: ${controller.currentSequenceLength} / ${controller.difficulty.maxGameLength}"),
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 16.0),
            child: Center(
              child: Text("시간: ${(controller.secondsElapsed ~/ 60).toString().padLeft(2, '0')}:${(controller.secondsElapsed % 60).toString().padLeft(2, '0')}", style: const TextStyle(fontSize: 18)),
            ),
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _buildSequenceDisplay(controller),
            
            _buildFeedbackWidget(controller, theme),

            const SizedBox(height: 10),
            Text(statusText, style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold, color: statusColor)),
            Text("최고 기록: ${controller.maxAchievedLength} | 성공: ${controller.roundsCompleted}", style: const TextStyle(fontSize: 16, color: Colors.grey)), 
            const SizedBox(height: 40),

            SizedBox(
              width: 300,
              child: GridView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: controller.difficulty.buttonsCount > 4 ? 3 : 2, 
                  mainAxisSpacing: 16,
                  crossAxisSpacing: 16,
                  childAspectRatio: 1.5,
                ),
                itemCount: availableButtons.length,
                itemBuilder: (context, index) {
                  final buttonId = availableButtons[index];
                  
                  return SequenceButtonWidget(
                    id: buttonId,
                    onTap: (state == SequenceGameState.input && !controller.showFeedback) 
                        ? () => controller.recordUserInput(buttonId) 
                        : null,
                  );
                },
              ),
            ),
            const SizedBox(height: 80),
            
            if (state == SequenceGameState.completed)
                ElevatedButton(
                    onPressed: controller.restartGame,
                    child: const Text('다시 시작', style: TextStyle(fontSize: 20)),
                )
          ],
        ),
      ),
    );
  }
}