import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../services/bluetooth_service.dart'; class HardwareSettingsScreen extends ConsumerWidget { const HardwareSettingsScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final machineState = ref.watch(bluetoothServiceProvider); final bluetoothService = ref.read(bluetoothServiceProvider.notifier); return Scaffold( appBar: AppBar(title: const Text('Hardware Configuration')), body: ListView( padding: const EdgeInsets.all(16), children: [ if (machineState.isLegacyV4Firmware) const Padding( padding: EdgeInsets.all(16), child: Text( 'V4 firmware does not support configurable output mapping or a momentary power button.', ), ), if (!machineState.isLegacyV4Firmware) ...[ _SectionHeader(title: 'Output Mapping'), _OutputTile( label: 'Output 1', value: machineState.output0, onChanged: (v) => bluetoothService.updateHardwareSettings(output0: v!), ), ], _OutputTile( label: 'Output 2', value: machineState.output1, onChanged: machineState.isLegacyV4Firmware ? null : (v) => bluetoothService.updateHardwareSettings(output1: v!), ), _OutputTile( label: 'Output 3', value: machineState.output2, onChanged: machineState.isLegacyV4Firmware ? null : (v) => bluetoothService.updateHardwareSettings(output2: v!), ), const Divider(), _SectionHeader(title: 'Machine Quirks'), SwitchListTile( title: const Text('Installed as Timer'), subtitle: const Text('Prevent warmup after power outage'), value: machineState.installedAsTimer, onChanged: bluetoothService.updateInstalledAsTimer, ), if (!machineState.isLegacyV4Firmware) SwitchListTile( title: const Text('Power Button Flips Back'), subtitle: const Text('Enable for Silvia V5 or similar'), value: machineState.powerButtonFlipsBack, onChanged: (v) => bluetoothService.updateHardwareSettings(flips: v), ), const Divider(), _SectionHeader(title: 'Firmware'), ListTile( leading: const Icon(Icons.memory_outlined), title: const Text('Firmware Version'), subtitle: Text( machineState.firmwareVersion.isEmpty ? 'Unknown (connect to device)' : machineState.firmwareVersion, ), ), ListTile( leading: const Icon(Icons.system_update), title: const Text('Install bundled V10 firmware'), subtitle: const Text( 'Experimental STK500 update with read-back verification. ' 'Only for "pcb V9" controllers (not V4 / pcb V3). ' 'Requires Bluetooth Classic, live telemetry, heating off, no shot, and a boiler ' 'at or below 50 C. BLE flashing is not available.', ), enabled: bluetoothService.canUpdateFirmware, onTap: () async { final confirmed = await showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Update controller firmware?'), content: const Text( 'This replaces the controller firmware with the bundled V10 ' 'image. Only ATmega328P controllers with a compatible STK500 ' 'bootloader are supported. Settings may change after updating.\n\n' 'Do not operate the machine, close the app, or interrupt ' 'power/Bluetooth. Failure may require manual bootloader ' 'recovery. Continue only if you can recover your controller.', ), actions: [ TextButton( onPressed: () => Navigator.pop(context, false), child: const Text('Cancel'), ), FilledButton( onPressed: () => Navigator.pop(context, true), child: const Text('I understand - update'), ), ], ), ); if (confirmed == true) await bluetoothService.updateFirmware(); }, ), if (machineState.firmwareUpdateStatus.isNotEmpty) Padding( padding: const EdgeInsets.all(16), child: Text(machineState.firmwareUpdateStatus), ), if (machineState.firmwareUpdating) LinearProgressIndicator(value: machineState.firmwareProgress), ], ), ); } } class _OutputTile extends StatelessWidget { final String label; final int value; final ValueChanged? onChanged; const _OutputTile({ required this.label, required this.value, required this.onChanged, }); @override Widget build(BuildContext context) { return ListTile( title: Text(label), trailing: DropdownButton( value: value, items: const [ DropdownMenuItem(value: 112, child: Text('Pump')), DropdownMenuItem(value: 98, child: Text('Boiler')), DropdownMenuItem(value: 118, child: Text('Valve')), DropdownMenuItem(value: 105, child: Text('Indicator')), DropdownMenuItem(value: 103, child: Text('Grinder')), DropdownMenuItem(value: 110, child: Text('None')), ], onChanged: onChanged, ), ); } } class _SectionHeader extends StatelessWidget { final String title; const _SectionHeader({required this.title}); @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(vertical: 8.0), child: Text( title.toUpperCase(), style: TextStyle( color: Theme.of(context).colorScheme.primary, fontWeight: FontWeight.bold, fontSize: 12, ), ), ); } }