// ABOUTME: Setting info tile widget for displaying read-only information and navigation
// ABOUTME: Provides consistent display for app information, version details, and navigation items
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// Widget for displaying read-only information or navigational settings
class SettingInfoTile extends StatelessWidget {
  final Widget leading;
  final String title;
  final String? subtitle;
  final Widget? trailing;
  final VoidCallback? onTap;
  final bool showArrow;
  final bool enabled;
  final bool copyable;

  const SettingInfoTile({
    super.key,
    required this.leading,
    required this.title,
    this.subtitle,
    this.trailing,
    this.onTap,
    this.showArrow = false,
    this.enabled = true,
    this.copyable = false,
  });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: leading,
      title: Text(
        title,
        style: Theme.of(context).textTheme.bodyLarge?.copyWith(
          fontWeight: FontWeight.w500,
          color: enabled 
              ? null 
              : Theme.of(context).colorScheme.onSurfaceVariant,
        ),
      ),
      subtitle: subtitle != null
          ? Text(
              subtitle!,
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                color: enabled
                    ? Theme.of(context).colorScheme.onSurfaceVariant
                    : Theme.of(context).colorScheme.onSurfaceVariant.withOpacity(0.6),
              ),
            )
          : null,
      trailing: _buildTrailing(context),
      onTap: _handleTap(context),
      enabled: enabled && (onTap != null || copyable),
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
    );
  }

  Widget? _buildTrailing(BuildContext context) {
    if (trailing != null) return trailing;
    
    if (showArrow && onTap != null) {
      return Icon(
        Icons.arrow_forward_ios,
        size: 16,
        color: enabled
            ? Theme.of(context).colorScheme.onSurfaceVariant
            : Theme.of(context).colorScheme.onSurfaceVariant.withOpacity(0.5),
      );
    }
    
    if (copyable && subtitle != null) {
      return Icon(
        Icons.copy,
        size: 20,
        color: enabled
            ? Theme.of(context).colorScheme.onSurfaceVariant
            : Theme.of(context).colorScheme.onSurfaceVariant.withOpacity(0.5),
      );
    }
    
    return null;
  }

  VoidCallback? _handleTap(BuildContext context) {
    if (!enabled) return null;
    
    if (onTap != null) return onTap;
    
    if (copyable && subtitle != null) {
      return () => _copyToClipboard(context, subtitle!);
    }
    
    return null;
  }

  void _copyToClipboard(BuildContext context, String text) {
    Clipboard.setData(ClipboardData(text: text));
    
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text('Copied "$text" to clipboard'),
        duration: const Duration(seconds: 2),
        behavior: SnackBarBehavior.floating,
      ),
    );
  }
}

/// Version info tile with special formatting
class VersionInfoTile extends SettingInfoTile {
  const VersionInfoTile({
    super.key,
    required super.title,
    required String version,
    String? buildNumber,
    super.onTap,
  }) : super(
          leading: const Icon(Icons.info_outline),
          subtitle: buildNumber != null ? '$version ($buildNumber)' : version,
          copyable: true,
        );
}

/// Link tile for external navigation
class LinkInfoTile extends SettingInfoTile {
  const LinkInfoTile({
    super.key,
    required super.leading,
    required super.title,
    super.subtitle,
    required super.onTap,
  }) : super(
          showArrow: true,
          trailing: const Icon(Icons.open_in_new, size: 18),
        );
}

/// Action tile for performing operations
class ActionInfoTile extends SettingInfoTile {
  final Color? actionColor;
  final bool destructive;

  const ActionInfoTile({
    super.key,
    required super.leading,
    required super.title,
    super.subtitle,
    required super.onTap,
    this.actionColor,
    this.destructive = false,
  });

  @override
  Widget build(BuildContext context) {
    final color = destructive 
        ? Theme.of(context).colorScheme.error
        : actionColor ?? Theme.of(context).colorScheme.primary;

    return ListTile(
      leading: IconTheme(
        data: IconThemeData(color: color),
        child: leading,
      ),
      title: Text(
        title,
        style: Theme.of(context).textTheme.bodyLarge?.copyWith(
          fontWeight: FontWeight.w500,
          color: enabled ? color : Theme.of(context).colorScheme.onSurfaceVariant,
        ),
      ),
      subtitle: subtitle != null
          ? Text(
              subtitle!,
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                color: enabled
                    ? Theme.of(context).colorScheme.onSurfaceVariant
                    : Theme.of(context).colorScheme.onSurfaceVariant.withOpacity(0.6),
              ),
            )
          : null,
      onTap: enabled ? onTap : null,
      enabled: enabled,
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
    );
  }
}

/// Status tile with status indicators
class StatusInfoTile extends SettingInfoTile {
  final bool isActive;
  final Color? activeColor;
  final Color? inactiveColor;

  const StatusInfoTile({
    super.key,
    required super.leading,
    required super.title,
    super.subtitle,
    super.onTap,
    required this.isActive,
    this.activeColor,
    this.inactiveColor,
  });

  @override
  Widget build(BuildContext context) {
    final statusColor = isActive
        ? activeColor ?? Theme.of(context).colorScheme.primary
        : inactiveColor ?? Theme.of(context).colorScheme.onSurfaceVariant;

    return ListTile(
      leading: leading,
      title: Text(
        title,
        style: Theme.of(context).textTheme.bodyLarge?.copyWith(
          fontWeight: FontWeight.w500,
        ),
      ),
      subtitle: subtitle != null
          ? Text(
              subtitle!,
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                color: Theme.of(context).colorScheme.onSurfaceVariant,
              ),
            )
          : null,
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 8,
            height: 8,
            decoration: BoxDecoration(
              color: statusColor,
              shape: BoxShape.circle,
            ),
          ),
          const SizedBox(width: 8),
          Text(
            isActive ? 'Active' : 'Inactive',
            style: Theme.of(context).textTheme.bodySmall?.copyWith(
              color: statusColor,
              fontWeight: FontWeight.w500,
            ),
          ),
          if (onTap != null) ...[
            const SizedBox(width: 8),
            const Icon(Icons.arrow_forward_ios, size: 16),
          ],
        ],
      ),
      onTap: onTap,
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
    );
  }
}
