import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.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'; import 'package:syncrow_web/utils/extension/build_context_x.dart'; class ACHelper { static Future?> showACFunctionsDialog( BuildContext context, List> functions, ) async { List acFunctions = functions.whereType().toList(); String? selectedFunction; dynamic selectedValue = 20; String? selectedCondition = "=="; List _selectedConditions = [false, true, false]; return showDialog?>( context: context, builder: (BuildContext context) { return StatefulBuilder( builder: (context, setState) { return AlertDialog( contentPadding: EdgeInsets.zero, content: _buildDialogContent( context, setState, acFunctions, selectedFunction, selectedValue, selectedCondition, _selectedConditions, (fn) => selectedFunction = fn, (val) => selectedValue = val, (cond) => selectedCondition = cond, ), ); }, ); }, ); } /// Build dialog content for AC functions dialog static Widget _buildDialogContent( BuildContext context, StateSetter setState, List acFunctions, String? selectedFunction, dynamic selectedValue, String? selectedCondition, List selectedConditions, Function(String?) onFunctionSelected, Function(dynamic) onValueSelected, Function(String?) onConditionSelected, ) { return Container( width: selectedFunction != null ? 600 : 360, height: 450, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(20), ), padding: const EdgeInsets.only(top: 20), child: Column( mainAxisSize: MainAxisSize.min, children: [ _buildDialogHeader(context), Flexible( child: Row( children: [ _buildFunctionsList( context, setState, acFunctions, selectedFunction, onFunctionSelected, ), if (selectedFunction != null) _buildValueSelector( context, setState, selectedFunction, selectedValue, selectedCondition, selectedConditions, onValueSelected, onConditionSelected, acFunctions, ), ], ), ), _buildDialogFooter( context, selectedFunction, selectedValue, selectedCondition, ), ], ), ); } /// Build header for AC functions dialog static Widget _buildDialogHeader(BuildContext context) { return Column( children: [ Text( 'AC Condition', 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, ), ), ], ); } /// Build functions list for AC functions dialog static Widget _buildFunctionsList( BuildContext context, StateSetter setState, List acFunctions, String? selectedFunction, Function(String?) onFunctionSelected, ) { return Expanded( child: ListView.separated( shrinkWrap: false, physics: const AlwaysScrollableScrollPhysics(), itemCount: acFunctions.length, separatorBuilder: (context, index) => const Divider( color: ColorsManager.dividerColor, ), itemBuilder: (context, index) { final function = acFunctions[index]; return ListTile( leading: SvgPicture.asset( function.icon, width: 24, height: 24, ), title: Text( function.operationName, style: context.textTheme.bodyMedium, ), trailing: const Icon( Icons.arrow_forward_ios, size: 16, color: ColorsManager.textGray, ), onTap: () => setState(() => onFunctionSelected(function.code)), ); }, ), ); } /// Build value selector for AC functions dialog static Widget _buildValueSelector( BuildContext context, StateSetter setState, String selectedFunction, dynamic selectedValue, String? selectedCondition, List selectedConditions, Function(dynamic) onValueSelected, Function(String?) onConditionSelected, List acFunctions, ) { if (selectedFunction == 'temp_set' || selectedFunction == 'temp_current') { return Expanded( child: _buildTemperatureSelector( context, setState, selectedValue, selectedCondition, selectedConditions, onValueSelected, onConditionSelected, ), ); } final selectedFn = acFunctions.firstWhere((f) => f.code == selectedFunction); final values = selectedFn.getOperationalValues(); return Expanded( child: _buildOperationalValuesList( context, setState, values, selectedValue, onValueSelected, ), ); } /// Build temperature selector for AC functions dialog static Widget _buildTemperatureSelector( BuildContext context, StateSetter setState, dynamic selectedValue, String? selectedCondition, List selectedConditions, Function(dynamic) onValueSelected, Function(String?) onConditionSelected, ) { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildConditionToggle( context, setState, selectedConditions, onConditionSelected, ), const SizedBox(height: 20), _buildTemperatureDisplay(context, selectedValue), const SizedBox(height: 20), _buildTemperatureSlider( context, setState, selectedValue, onValueSelected, ), ], ); } /// Build condition toggle for AC functions dialog static Widget _buildConditionToggle( BuildContext context, StateSetter setState, List selectedConditions, Function(String?) onConditionSelected, ) { return ToggleButtons( onPressed: (int index) { setState(() { for (int i = 0; i < selectedConditions.length; i++) { selectedConditions[i] = i == index; } onConditionSelected(index == 0 ? "<" : index == 1 ? "==" : ">"); }); }, borderRadius: const BorderRadius.all(Radius.circular(8)), selectedBorderColor: ColorsManager.primaryColorWithOpacity, selectedColor: Colors.white, fillColor: ColorsManager.primaryColorWithOpacity, color: ColorsManager.primaryColorWithOpacity, constraints: const BoxConstraints( minHeight: 40.0, minWidth: 40.0, ), isSelected: selectedConditions, children: const [Text("<"), Text("="), Text(">")], ); } /// Build temperature display for AC functions dialog static Widget _buildTemperatureDisplay( BuildContext context, dynamic selectedValue) { return Container( padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10), decoration: BoxDecoration( color: ColorsManager.primaryColorWithOpacity.withOpacity(0.1), borderRadius: BorderRadius.circular(10), ), child: Text( '${selectedValue ?? 20}°C', style: context.textTheme.headlineMedium!.copyWith( color: ColorsManager.primaryColorWithOpacity, ), ), ); } static Widget _buildTemperatureSlider( BuildContext context, StateSetter setState, dynamic selectedValue, Function(dynamic) onValueSelected, ) { final currentValue = selectedValue is int ? selectedValue.toDouble() : 20.0; return Slider( value: currentValue, min: 16, max: 30, divisions: 14, label: '${currentValue.toInt()}°C', onChanged: (value) { setState(() => onValueSelected(value.toInt())); }, ); } static Widget _buildOperationalValuesList( BuildContext context, StateSetter setState, List values, dynamic selectedValue, Function(dynamic) onValueSelected, ) { return ListView.builder( shrinkWrap: false, physics: const AlwaysScrollableScrollPhysics(), itemCount: values.length, itemBuilder: (context, index) { final value = values[index]; return ListTile( leading: SvgPicture.asset( value.icon, width: 24, height: 24, ), title: Text( value.description, style: context.textTheme.bodyMedium, ), trailing: Radio( value: value.value, groupValue: selectedValue, onChanged: (newValue) { setState(() => onValueSelected(newValue)); }, ), ); }, ); } static Widget _buildDialogFooter( BuildContext context, String? selectedFunction, dynamic selectedValue, String? selectedCondition, ) { return Container( decoration: const BoxDecoration( border: Border( top: BorderSide( color: ColorsManager.greyColor, ), ), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ _buildFooterButton( context, 'Cancel', selectedFunction != null ? 299 : 179, () => Navigator.pop(context), ), _buildFooterButton( context, 'Confirm', selectedFunction != null ? 299 : 179, selectedFunction != null && selectedValue != null ? () => Navigator.pop(context, { 'function': selectedFunction, 'value': selectedValue, 'condition': selectedCondition ?? "==", }) : null, ), ], ), ); } static Widget _buildFooterButton( BuildContext context, String text, double width, VoidCallback? onTap, ) { return GestureDetector( onTap: onTap, child: SizedBox( height: 50, width: width, child: Center( child: Text( text, style: context.textTheme.bodyMedium!.copyWith( color: onTap != null ? ColorsManager.primaryColorWithOpacity : ColorsManager.textGray, ), ), ), ), ); } }