import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher.dart';

class AppAboutDialog extends StatelessWidget {
  const AppAboutDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<PackageInfo>(
      future: PackageInfo.fromPlatform(),
      builder: (context, snapshot) {
        final packageInfo = snapshot.data;
        
        return AlertDialog(
          backgroundColor: const Color(0xFF1A1F3A),
          title: Row(
            children: [
              Image.asset(
                'assets/logo.png',
                width: 32,
                height: 32,
                errorBuilder: (context, error, stackTrace) => 
                  const Icon(Icons.analytics, color: Color(0xFF50E3C2), size: 32),
              ),
              const SizedBox(width: 12),
              const Text(
                'rmtPocketWatcher',
                style: TextStyle(color: Colors.white),
              ),
            ],
          ),
          content: SizedBox(
            width: 400,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Version ${packageInfo?.version ?? 'Unknown'}',
                  style: const TextStyle(
                    color: Color(0xFF50E3C2),
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 16),
                const Text(
                  'Star Citizen AUEC Price Tracker',
                  style: TextStyle(color: Colors.white70, fontSize: 14),
                ),
                const SizedBox(height: 8),
                const Text(
                  'Bloomberg-style terminal interface for real-time RMT price monitoring',
                  style: TextStyle(color: Colors.white70, fontSize: 12),
                ),
                const SizedBox(height: 16),
                const Divider(color: Color(0xFF50E3C2)),
                const SizedBox(height: 16),
                _buildInfoRow('Developer', 'Lambda Banking Conglomerate'),
                const SizedBox(height: 8),
                _buildInfoRow('Platform', 'Flutter Desktop'),
                const SizedBox(height: 8),
                _buildInfoRow('Build', packageInfo?.buildNumber ?? 'Unknown'),
                const SizedBox(height: 16),
                const Text(
                  'Features:',
                  style: TextStyle(
                    color: Color(0xFF50E3C2),
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 8),
                _buildFeatureItem('Real-time AUEC price tracking'),
                _buildFeatureItem('Multiple vendor monitoring'),
                _buildFeatureItem('Historical price charts'),
                _buildFeatureItem('Price alerts & notifications'),
                _buildFeatureItem('System tray integration'),
                _buildFeatureItem('Automatic updates'),
                const SizedBox(height: 16),
                const Text(
                  'Data Sources:',
                  style: TextStyle(
                    color: Color(0xFF50E3C2),
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 8),
                _buildFeatureItem('Eldorado.gg'),
                _buildFeatureItem('PlayerAuctions'),
              ],
            ),
          ),
          actions: [
            TextButton(
              onPressed: () => _launchUrl('https://git.hudsonriggs.systems/LambdaBankingConglomerate/rmtPocketWatcher'),
              child: const Text(
                'Source Code',
                style: TextStyle(color: Color(0xFF50E3C2)),
              ),
            ),
            TextButton(
              onPressed: () => _launchUrl('https://git.hudsonriggs.systems/LambdaBankingConglomerate/rmtPocketWatcher/releases'),
              child: const Text(
                'Releases',
                style: TextStyle(color: Color(0xFF50E3C2)),
              ),
            ),
            ElevatedButton(
              onPressed: () => Navigator.of(context).pop(),
              style: ElevatedButton.styleFrom(
                backgroundColor: const Color(0xFF50E3C2),
                foregroundColor: Colors.black,
              ),
              child: const Text('Close'),
            ),
          ],
        );
      },
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 80,
          child: Text(
            '$label:',
            style: const TextStyle(
              color: Colors.white70,
              fontSize: 12,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        Expanded(
          child: Text(
            value,
            style: const TextStyle(color: Colors.white, fontSize: 12),
          ),
        ),
      ],
    );
  }

  Widget _buildFeatureItem(String feature) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, bottom: 4),
      child: Row(
        children: [
          const Icon(
            Icons.check_circle,
            color: Color(0xFF50E3C2),
            size: 12,
          ),
          const SizedBox(width: 8),
          Text(
            feature,
            style: const TextStyle(color: Colors.white70, fontSize: 12),
          ),
        ],
      ),
    );
  }

  void _launchUrl(String url) async {
    final uri = Uri.parse(url);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri);
    }
  }
}