import 'package:flutter/material.dart';
import '../../../core/app_theme.dart';

class DeviceStatusCard extends StatelessWidget {
  final bool isOnline;
  final String deviceCode;
  final DateTime? lastSeen;

  const DeviceStatusCard({
    super.key,
    required this.isOnline,
    required this.deviceCode,
    this.lastSeen,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
      decoration: AppTheme.cardDecoration(),
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: isOnline
                  ? AppTheme.statusNormal.withValues(alpha: 0.12)
                  : Colors.red.withValues(alpha: 0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Icon(
              isOnline ? Icons.wifi : Icons.wifi_off,
              color: isOnline ? AppTheme.statusNormal : AppTheme.statusDanger,
              size: 26,
            ),
          ),
          const SizedBox(width: 16),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                isOnline ? 'Perangkat Online' : 'Perangkat Offline',
                style: const TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.w600,
                  color: AppTheme.textPrimary,
                ),
              ),
              Text(
                'Kode: $deviceCode',
                style: const TextStyle(
                  fontSize: 13,
                  color: AppTheme.textSecondary,
                ),
              ),
              if (lastSeen != null) ...[
                const SizedBox(height: 2),
                Text(
                  isOnline
                      ? 'Update: ${_formatTimeOnly(lastSeen!)}'
                      : 'Terakhir aktif: ${_formatDate(lastSeen!)}',
                  style: TextStyle(
                    fontSize: 11,
                    color: isOnline ? AppTheme.textSecondary : Colors.red.shade400,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ],
            ],
          ),
          const Spacer(),
          Container(
            width: 10,
            height: 10,
            decoration: BoxDecoration(
              color: isOnline ? AppTheme.statusNormal : AppTheme.statusDanger,
              shape: BoxShape.circle,
            ),
          ),
        ],
      ),
    );
  }

  String _formatTimeOnly(DateTime date) {
    return '${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}:${date.second.toString().padLeft(2, '0')}';
  }

  String _formatDate(DateTime date) {
    final now = DateTime.now();
    final diff = now.difference(date);
    
    if (diff.inDays == 0 && now.day == date.day) {
      // Hari ini
      return '${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
    } else if (diff.inDays < 2 && (now.day - date.day == 1 || now.day - date.day == -30)) {
      // Kemarin
      return 'Kemarin, ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
    } else {
      // Tanggal penuh
      final months = ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Ags', 'Sep', 'Okt', 'Nov', 'Des'];
      return '${date.day} ${months[date.month - 1]} ${date.year}, ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}';
    }
  }
}
