// packages/feature_game_spider/lib/widgets/bottom_bar_widget.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/spider_card.dart';
import '../controllers/spider_game_controller.dart';
import 'card_widget.dart';

class BottomBarWidget extends StatelessWidget {
  final double cardWidth;
  final double cardHeight;

  const BottomBarWidget({
    super.key, // 👈 GameScreen에서 _stockKey가 전달됨
    required this.cardWidth,
    required this.cardHeight,
  });

  @override
  Widget build(BuildContext context) {
    final controller = Provider.of<SpiderGameController>(context);
    final state = controller.currentState;
    final theme = Theme.of(context);

    return Container(
      height: cardHeight + 20,
      color: theme.bottomAppBarTheme.color, 
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // 1. 파운데이션 (왼쪽)
          _buildFoundationPiles(context, state.foundation), 
          
          // 2. 이동 횟수
          Text(
            "이동: ${state.moves}",
            style: TextStyle(
              color: theme.colorScheme.onSurface,
              fontSize: 18,
              fontWeight: FontWeight.bold
            ),
          ),
          
          // 3. 스톡 (오른쪽)
          // 🔽 [수정] _buildStockPile에 key 전달
          _buildStockPile(context, controller, state.stock, key),
        ],
      ),
    );
  }

  // 🔽 [수정] Key? key 파라미터 추가
  Widget _buildStockPile(BuildContext context, SpiderGameController controller, List<SpiderCard> stock, Key? key) {
    return GestureDetector(
      key: key, // 👈 [수정] GameScreen에서 전달받은 _stockKey를 여기에 할당
      onTap: (){
        debugPrint("[LOG] BottomBarWidget: Stock pile tapped!");
        controller.dealFromStock();
        },
      child: Container(
        width: cardWidth,
        height: cardHeight,
        decoration: BoxDecoration(
          color: Theme.of(context).primaryColor, 
          border: Border.all(color: Colors.white54, width: 0.5),
          borderRadius: BorderRadius.circular(cardWidth * 0.08),
        ),
        child: (stock.isEmpty)
            ? Center(child: Icon(Icons.block, color: Colors.white.withOpacity(0.5), size: cardWidth * 0.5))
            : Center(
                child: Text(
                  "${(stock.length / 10).ceil()}",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: cardWidth * 0.5,
                    fontWeight: FontWeight.bold,
                    shadows: [Shadow(blurRadius: 2, color: Colors.black.withOpacity(0.5))]
                  ),
                ),
              ),
      ),
    );
  }

  // 🔽 [수정] key 파라미터 제거 (Foundation은 위치 계산이 필요 없음)
  Widget _buildFoundationPiles(BuildContext context, List<List<SpiderCard>> foundation) {
    return SizedBox(
      // key: foundationKey, (제거)
      width: (cardWidth * 0.7) * 4 + cardWidth,
      height: cardHeight,
      child: Stack(
        children: List.generate(8, (index) {
          return Positioned(
            left: index * (cardWidth * 0.15), 
            child: Container(
              width: cardWidth,
              height: cardHeight,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.white54, width: 0.5),
                borderRadius: BorderRadius.circular(cardWidth * 0.08),
                color: Colors.black.withOpacity(0.2),
              ),
              child: (foundation.length > index && foundation[index].isNotEmpty)
                  ? CardWidget(
                      card: foundation[index].last..isFaceUp=true,
                      width: cardWidth,
                      height: cardHeight,
                      isDraggable: false, // 👈 [추가]
                    )
                  : Center(child: Icon(Icons.diamond_outlined, color: Colors.white.withOpacity(0.3), size: cardWidth * 0.3)),
            ),
          );
        }).toList(),
      ),
    );
  }
}