import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../../core/protocol/dtc_map.dart'; import '../../../core/providers/sensor_provider.dart'; import '../../../core/providers/settings_provider.dart'; import '../../../core/bluetooth/bt_poller.dart'; import 'widgets/dtc_row.dart'; class DtcScreen extends ConsumerWidget { const DtcScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final state = ref.watch(latestSensorProvider); final ecuType = ref.watch(settingsProvider).ecuType; final dtcMap = ecuType == EcuType.s300 ? s300DtcMap : kproDtcMap; final active = decodeDtcs(state.errBytes, dtcMap); return Column( children: [ // Header bar Container( color: const Color(0xFF1A1A1A), padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10), child: Row( children: [ Icon( active.isEmpty ? Icons.check_circle_outline : Icons.warning_amber_rounded, color: active.isEmpty ? Colors.green : Colors.amber, size: 18, ), const SizedBox(width: 8), Text( active.isEmpty ? 'No active fault codes' : '${active.length} active DTC${active.length > 1 ? "s" : ""}', style: TextStyle( color: active.isEmpty ? Colors.green : Colors.amber, fontSize: 13, fontWeight: FontWeight.w600, ), ), const Spacer(), Text( ecuType == EcuType.s300 ? 'S300' : 'KPro', style: const TextStyle(color: Colors.grey, fontSize: 12), ), ], ), ), const Divider(height: 1, color: Colors.white10), // DTC list Expanded( child: active.isEmpty ? const Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.check_circle_outline, size: 56, color: Colors.green), SizedBox(height: 12), Text('All clear — no fault codes', style: TextStyle( color: Colors.grey, fontSize: 15)), ], ), ) : ListView.separated( itemCount: active.length, separatorBuilder: (_, __) => const Divider(height: 1, color: Colors.white10), itemBuilder: (context, i) { final full = active[i]; // "P0130 — O2 Sensor (front)" final parts = full.split(' — '); final code = parts.first.trim(); final desc = parts.length > 1 ? parts.sublist(1).join(' — ') : ''; return DtcRow(code: code, description: desc); }, ), ), ], ); } }