import 'package:digiplug/bat_theme/bat_theme.dart';
import 'package:digiplug/core/navigation/navigator.dart';
import 'package:digiplug/data/models/device.dart';
import 'package:digiplug/providers/home_data_provider.dart';
import 'package:digiplug/utils/enums.dart'; // PERBAIKAN: Import yang hilang ditambahkan di sini
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';

class DeviceCard extends StatelessWidget {
  final Device device;

  const DeviceCard({
    super.key,
    required this.device,
  });

  // Helper untuk menampilkan ikon berdasarkan jenisnya (PNG atau SVG)
  Widget _buildDeviceIcon(dynamic iconData, bool isActive, BatThemeData theme) {
    final iconColor = isActive
        ? theme.colors.primary
        : theme.colors.tertiary.withOpacity(0.6);

    if (iconData is String) {
      if (iconData.endsWith('.svg')) {
        return SvgPicture.asset(iconData,
            width: 32.w,
            height: 32.h,
            colorFilter: ColorFilter.mode(iconColor, BlendMode.srcIn),
            placeholderBuilder: (_) =>
                Icon(Icons.device_unknown, color: iconColor, size: 32.w));
      } else {
        return Image.asset(iconData,
            width: 32.w,
            height: 32.h,
            color: iconColor,
            errorBuilder: (_, __, ___) =>
                Icon(Icons.device_unknown, color: iconColor, size: 32.w));
      }
    }
    return Icon(Icons.device_unknown, color: iconColor, size: 32.w);
  }

  @override
  Widget build(BuildContext context) {
    var theme = BatThemeData.of(context);

    final homeDataProvider = context.read<HomeDataProvider>();

    return GestureDetector(
      onTap: () =>
          AppNavigator.pushNamed(device.type.routeName, arguments: device),
      child: Container(
        padding: EdgeInsets.all(16.r),
        decoration: BoxDecoration(
          color: theme.colors.tertiary.withOpacity(0.1),
          borderRadius: BorderRadius.circular(20.r),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildDeviceIcon(device.type.icon, device.active, theme),
                Transform.scale(
                  scale: 0.8,
                  alignment: Alignment.topRight,
                  child: Switch.adaptive(
                    activeColor: theme.colors.primary,
                    value: device.active,
                    onChanged: (bool v) {
                      homeDataProvider.toggleDeviceStatus(device.id);
                    },
                  ),
                ),
              ],
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(device.name,
                    style: theme.typography.bodyCopyMedium,
                    overflow: TextOverflow.ellipsis),
                SizedBox(height: 4.h),
                Text(device.room,
                    style: theme.typography.subtitle.copyWith(
                        color: theme.colors.tertiary.withOpacity(0.7))),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
