import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart'; import 'package:syncrow_web/pages/routiens/bloc/routine_bloc.dart'; import 'package:syncrow_web/pages/routiens/models/ac/ac_function.dart'; import 'package:syncrow_web/pages/routiens/models/device_functions.dart'; import 'package:syncrow_web/utils/color_manager.dart'; mixin ACHelper { Future?> showACFunctionsDialog(BuildContext context, List> functions) { List acFunctions = functions.whereType().toList(); String? selectedFunction; dynamic selectedValue; return showDialog( context: context, builder: (BuildContext context) { return StatefulBuilder( builder: (context, setState) { return AlertDialog( contentPadding: EdgeInsets.zero, content: Container( width: selectedFunction != null ? 600 : 360, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(20), ), padding: const EdgeInsets.only(top: 20), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text( 'AC Functions', style: Theme.of(context).textTheme.bodyMedium!.copyWith( color: ColorsManager.primaryColorWithOpacity, fontWeight: FontWeight.bold, ), ), Padding( padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 50), child: Container( height: 1, width: double.infinity, color: ColorsManager.greyColor, ), ), Flexible( child: Row( children: [ Expanded( child: ListView.separated( shrinkWrap: true, itemCount: acFunctions.length, separatorBuilder: (context, index) => Divider(), itemBuilder: (context, index) { final function = acFunctions[index]; return ListTile( leading: Image.asset(function.icon, width: 24, height: 24), title: Text(function.operationName), trailing: Icon(Icons.arrow_forward_ios), onTap: () { setState(() { selectedFunction = function.code; selectedValue = null; }); }, ); }, ), ), if (selectedFunction != null) Container( width: 1, color: ColorsManager.greyColor, ), if (selectedFunction != null) Expanded( child: ListView.separated( shrinkWrap: true, itemCount: acFunctions .firstWhere((f) => f.code == selectedFunction) .getOperationalValues() .length, separatorBuilder: (context, index) => Divider(), itemBuilder: (context, index) { final operationalValue = acFunctions.firstWhere((f) => f.code == selectedFunction) ..getOperationalValues()[index]; return ListTile( leading: Image.asset(operationalValue.icon, width: 24, height: 24), title: Text(operationalValue.getOperationalValues()[index].description), trailing: Radio( value: operationalValue.getOperationalValues()[index].value, groupValue: selectedValue, onChanged: (value) { setState(() { selectedValue = value; }); }, ), ); }, ), ), ], ), ), Container( height: 1, width: double.infinity, color: ColorsManager.greyColor, ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ GestureDetector( onTap: () { Navigator.pop(context); }, child: Center( child: Text( 'Cancel', style: Theme.of(context).textTheme.bodyMedium!.copyWith(color: ColorsManager.greyColor), ), ), ), Container( height: 50, width: 1, color: ColorsManager.greyColor, ), GestureDetector( onTap: () { // Handle the confirmation action here Navigator.pop(context, { 'function': selectedFunction, 'value': selectedValue, }); }, child: Center( child: Text( 'Confirm', style: Theme.of(context).textTheme.bodyMedium!.copyWith( color: ColorsManager.primaryColorWithOpacity, ), ), ), ), ], ), ], ), ), ); }, ); }, ); } void handleACDeviceDrop(BuildContext context, Map data) { final device = data['device'] as AllDevicesModel; final acFunctions = device.deviceFunctions; showACFunctionsDialog(context, acFunctions).then((result) { if (result != null) { _addACDeviceToRoutine(context, data, result); } }); } void handleNonACDeviceDrop(BuildContext context, Map data) { context.read().add(AddToThenContainer(data)); } void _addACDeviceToRoutine(BuildContext context, Map deviceData, Map functionData) { final updatedData = { ...deviceData, 'function': functionData['function'], 'value': functionData['value'], }; context.read().add(AddToThenContainer(updatedData)); _logACFunctionSelection(functionData); } void _logACFunctionSelection(Map functionData) { print('Selected AC function: ${functionData['function']}, Value: ${functionData['value']}'); } }