import 'package:flutter/material.dart';
import 'player_service.dart';
import 'package:intl/intl.dart';

class FullPlayerView extends StatelessWidget {
  const FullPlayerView({super.key});
  static const Color terminalGreen = Color(0xFFE87D25);

  @override
  Widget build(BuildContext context) {
    final service = PlayerService();
    final track = service.currentTrack.value!;

    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        title: const Text("AUDIO_DIAGNOSTICS"),
        leading: IconButton(icon: const Icon(Icons.keyboard_arrow_down), onPressed: () => Navigator.pop(context)),
      ),
      body: Column(
        children: [
          const SizedBox(height: 20),
          // MASSIVE ALBUM ART WITH SYSTEM BORDER
          Container(
            margin: const EdgeInsets.all(30),
            decoration: BoxDecoration(
              border: Border.all(color: terminalGreen, width: 2),
              boxShadow: [BoxShadow(color: terminalGreen.withValues(alpha: 0.1), blurRadius: 20)],
            ),
            child: AspectRatio(
              aspectRatio: 1,
              child: Image.network(track['imageUrl'], fit: BoxFit.cover),
            ),
          ),

          Text(track['title'].toString().toUpperCase(),
              style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold, letterSpacing: 4)),
          const Text("SOURCE: SAGE_ARCHIVE_V.064", style: TextStyle(color: Colors.white24, fontSize: 10)),

          const Spacer(),

          // PROGRESS BAR
          StreamBuilder<Duration>(
              stream: service.player.positionStream,
              builder: (context, snapshot) {
                final pos = snapshot.data ?? Duration.zero;
                final total = service.player.duration ?? Duration.zero;
                return Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 40),
                  child: Column(
                    children: [
                      LinearProgressIndicator(
                        value: total.inMilliseconds > 0 ? pos.inMilliseconds / total.inMilliseconds : 0,
                        backgroundColor: Colors.white10,
                        color: terminalGreen,
                      ),
                      const SizedBox(height: 10),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(_formatDuration(pos), style: const TextStyle(fontSize: 10, color: terminalGreen)),
                          Text(_formatDuration(total), style: const TextStyle(fontSize: 10, color: terminalGreen)),
                        ],
                      )
                    ],
                  ),
                );
              }
          ),

          // CONTROLS
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 40),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconButton(icon: const Icon(Icons.replay_10, color: terminalGreen), onPressed: () => service.player.seek(service.player.position - const Duration(seconds: 10))),
                const SizedBox(width: 30),
                StreamBuilder<bool>(
                    stream: service.player.playingStream,
                    builder: (context, snapshot) {
                      final isPlaying = snapshot.data ?? false;
                      return GestureDetector(
                        onTap: () => service.toggle(),
                        child: Container(
                          padding: const EdgeInsets.all(20),
                          decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: terminalGreen)),
                          child: Icon(isPlaying ? Icons.pause : Icons.play_arrow, color: terminalGreen, size: 40),
                        ),
                      );
                    }
                ),
                const SizedBox(width: 30),
                IconButton(icon: const Icon(Icons.forward_30, color: terminalGreen), onPressed: () => service.player.seek(service.player.position + const Duration(seconds: 30))),
              ],
            ),
          ),
          const SizedBox(height: 20),
        ],
      ),
    );
  }

  String _formatDuration(Duration d) {
    String twoDigits(int n) => n.toString().padLeft(2, "0");
    return "${twoDigits(d.inMinutes)}:${twoDigits(d.inSeconds.remainder(60))}";
  }
}