import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/routines/bloc/functions_bloc/functions_bloc_bloc.dart';
import 'package:syncrow_web/pages/routines/models/device_functions.dart';
import 'package:syncrow_web/pages/routines/models/wps/wps_operational_value.dart';
import 'package:syncrow_web/pages/routines/widgets/routine_dialogs/wall_sensor/time_wheel.dart';

class WpsOperationalValuesList extends StatelessWidget {
  final List<WpsOperationalValue> values;
  final dynamic selectedValue;
  final AllDevicesModel? device;
  final String operationName;
  final String selectCode;

  const WpsOperationalValuesList({
    required this.values,
    required this.selectedValue,
    required this.device,
    required this.operationName,
    required this.selectCode,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return operationName == 'Nobody Time'
        ? _buildTimeWheel(context)
        : ListView.builder(
            padding: const EdgeInsets.all(20),
            itemCount: values.length,
            itemBuilder: (context, index) => _buildValueItem(context, values[index]),
          );
  }

  Widget _buildTimeWheel(BuildContext context) {
    final currentTotalSeconds = selectedValue as int? ?? 0;
    return TimeWheelPicker(
      initialHours: currentTotalSeconds ~/ 3600,
      initialMinutes: (currentTotalSeconds % 3600) ~/ 60,
      initialSeconds: currentTotalSeconds % 60,
      onTimeChanged: (h, m, s) => _updateTotalSeconds(context, h, m, s),
    );
  }

  Widget _buildValueItem(BuildContext context, WpsOperationalValue value) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildValueIcon(context, value),
          Expanded(child: _buildValueDescription(value)),
          _buildValueRadio(context, value),
        ],
      ),
    );
  }

  Widget _buildValueIcon(context, WpsOperationalValue value) {
    return Column(
      children: [
        if (_shouldShowTextDescription) Text(value.description.replaceAll("cm", '')),
        SvgPicture.asset(value.icon, width: 25, height: 25),
      ],
    );
  }

  bool get _shouldShowTextDescription =>
      operationName == 'Far Detection' ||
      operationName == 'Motionless Detection Sensitivity';

  Widget _buildValueDescription(WpsOperationalValue value) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      child: Text(value.description),
    );
  }

  Widget _buildValueRadio(context, WpsOperationalValue value) {
    return Radio<dynamic>(
      value: value.value,
      groupValue: selectedValue,
      onChanged: (_) => _selectValue(context, value.value),
    );
  }

  void _selectValue(BuildContext context, dynamic value) {
    context.read<FunctionBloc>().add(
          AddFunction(
            functionData: DeviceFunctionData(
              entityId: device?.uuid ?? '',
              functionCode: selectCode,
              operationName: operationName,
              value: value,
            ),
          ),
        );
  }

  void _updateTotalSeconds(BuildContext context, int h, int m, int s) {
    context.read<FunctionBloc>().add(
          AddFunction(
            functionData: DeviceFunctionData(
              entityId: device?.uuid ?? '',
              functionCode: selectCode,
              operationName: operationName,
              value: h * 3600 + m * 60 + s,
            ),
          ),
        );
  }
}
