import 'package:flutter/material.dart';

import 'package:asciineverdie/src/shared/retro_colors.dart';

/// 스플래시 화면 (세이브 파일 확인 중) - 레트로 스타일
class SplashScreen extends StatelessWidget {
  const SplashScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: RetroColors.deepBrown,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 타이틀 로고
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
              decoration: BoxDecoration(
                color: RetroColors.panelBg,
                border: Border.all(color: RetroColors.gold, width: 3),
              ),
              child: const Column(
                children: [
                  Icon(Icons.auto_awesome, size: 32, color: RetroColors.gold),
                  SizedBox(height: 12),
                  Text(
                    'ASCII',
                    style: TextStyle(
                      fontFamily: 'PressStart2P',
                      fontSize: 22,
                      color: RetroColors.gold,
                      shadows: [
                        Shadow(
                          color: RetroColors.goldDark,
                          offset: Offset(2, 2),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 4),
                  Text(
                    'NEVER DIE',
                    style: TextStyle(
                      fontFamily: 'PressStart2P',
                      fontSize: 16,
                      color: RetroColors.cream,
                      shadows: [
                        Shadow(color: RetroColors.brown, offset: Offset(1, 1)),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 32),
            // 레트로 로딩 바
            const SizedBox(width: 160, child: _RetroLoadingBar()),
          ],
        ),
      ),
    );
  }
}

/// 레트로 스타일 로딩 바 (애니메이션)
class _RetroLoadingBar extends StatefulWidget {
  const _RetroLoadingBar();

  @override
  State<_RetroLoadingBar> createState() => _RetroLoadingBarState();
}

class _RetroLoadingBarState extends State<_RetroLoadingBar>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1500),
      vsync: this,
    )..repeat();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    const segmentCount = 10;

    return AnimatedBuilder(
      animation: _controller,
      builder: (context, child) {
        return Container(
          height: 16,
          decoration: BoxDecoration(
            color: RetroColors.panelBg,
            border: Border.all(color: RetroColors.panelBorderOuter, width: 2),
          ),
          child: Row(
            children: List.generate(segmentCount, (index) {
              final progress = _controller.value * segmentCount;
              final distance = (index - progress).abs();
              final isLit = distance < 2 || (segmentCount - distance) < 2;
              final opacity = isLit ? 1.0 : 0.2;

              return Expanded(
                child: Container(
                  margin: const EdgeInsets.all(1),
                  decoration: BoxDecoration(
                    color: RetroColors.gold.withValues(alpha: opacity),
                  ),
                ),
              );
            }),
          ),
        );
      },
    );
  }
}
