part of "flush_sensor_interface.dart";

class FlushSensorOptionsList extends StatelessWidget {
  final FlushSensorModel flushSensorModel;
  final DeviceModel deviceModel;
  final FlushSensorBloc bloc;
  const FlushSensorOptionsList(
      {super.key,
      required this.flushSensorModel,
      required this.bloc,
      required this.deviceModel});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FlushSensorOption(
          withArrow: false,
          sliderValue: '${flushSensorModel.illuminance.toString()} Lux',
          icon: Assets.assetsIconsPresenceSensorAssetsIlluminanceValue,
          label: 'Illuminance Value',
          onTap: () {},
        ),
        FlushSensorOption(
            withArrow: false,
            icon: Assets.assetsIconsPresenceSensorAssetsIlluminanceRecord,
            label: 'Presence Record',
            onTap: () {
              Navigator.of(context).push(MaterialPageRoute(
                builder: (_) => FlushPresenceRecords(
                    deviceId: deviceModel.uuid!,
                    code: FlushSensorModel.codePresenceState,
                    title: 'Illuminance Record'),
              ));
            }),
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 12),
          child: Text(
            'Configuration',
            style: Theme.of(context)
                .textTheme
                .bodyMedium
                ?.copyWith(fontWeight: FontWeight.bold),
          ),
        ),
        FlushSensorOption(
          withArrow: true,
          sliderValue: flushSensorModel.sensitivity.toString(),
          icon: Assets.assetsSensitivityFunction,
          label: 'Sensitivity',
          onTap: () => _showParameterDialog(
            min: 0,
            max: 9,
            value: flushSensorModel.sensitivity.toDouble(),
            context: context,
            title: 'Sensitivity',
            controlCode: FlushSensorModel.codeSensitivity,
            step: 1.0,
          ),
        ),
        FlushSensorOption(
          withArrow: true,
          sliderValue: "${flushSensorModel.nearDetection / 100} m",
          icon: Assets.detectionDistanceIcon,
          label: 'Min Detection Distance',
          onTap: () => _showParameterDialog(
            min: 0.0,
            max: 9.5,
            value: flushSensorModel.nearDetection.toDouble() / 100,
            context: context,
            title: 'Min Detection Distance',
            controlCode: FlushSensorModel.codeNearDetection,
            step: 0.1,
            description: 'm',
          ),
        ),
        FlushSensorOption(
          withArrow: true,
          sliderValue: '${flushSensorModel.farDetection / 100} m',
          icon: Assets.detectionDistanceIcon,
          label: 'Max Detection Distance',
          onTap: () => _showParameterDialog(
              description: 'm',
              min: 0.0,
              max: 9.5,
              value: flushSensorModel.farDetection / 100,
              context: context,
              title: 'Max Detection Distance',
              controlCode: FlushSensorModel.codeFarDetection,
              step: 0.1),
        ),
        FlushSensorOption(
          sliderValue: flushSensorModel.sensiReduce.toString(),
          withArrow: true,
          icon: Assets.triggerLevelIcon,
          label: 'Trigger Level',
          onTap: () => _showParameterDialog(
              min: 0,
              max: 3,
              value: flushSensorModel.sensiReduce.toDouble(),
              context: context,
              title: 'Trigger Level',
              controlCode: FlushSensorModel.codeSensiReduce,
              step: 1),
        ),
        FlushSensorOption(
          sliderValue: flushSensorModel.occurDistReduce.toString(),
          withArrow: true,
          icon: Assets.indentLevelIcon,
          label: 'Indent Level',
          onTap: () => _showParameterDialog(
              min: 0,
              max: 3,
              value: flushSensorModel.occurDistReduce.toDouble(),
              context: context,
              title: 'Indent Level',
              controlCode: FlushSensorModel.codeOccurDistReduce,
              step: 1),
        ),
        FlushSensorOption(
          sliderValue: '${flushSensorModel.presenceDelay / 10}',
          withArrow: true,
          icon: Assets.targetConfirmTimeIcon,
          label: 'Target Confirm Time',
          onTap: () => _showParameterDialog(
              min: 0.0,
              max: 0.5,
              value: flushSensorModel.presenceDelay.toDouble() / 10,
              context: context,
              title: 'Target Confirm Time',
              controlCode: FlushSensorModel.codePresenceDelay,
              step: 0.1),
        ),
        FlushSensorOption(
          sliderValue: '${flushSensorModel.noneDelay.toDouble() / 10}',
          withArrow: true,
          icon: Assets.disappeDelayIcon,
          label: 'Disappe Delay',
          onTap: () => _showParameterDialog(
              min: 20,
              max: 300,
              value: flushSensorModel.noneDelay.toDouble() / 10,
              context: context,
              title: 'Disappe Delay',
              controlCode: FlushSensorModel.codeNoneDelay,
              step: 1),
        ),
      ],
    );
  }

  void _showParameterDialog({
    required double step,
    required BuildContext context,
    required String title,
    required String controlCode,
    required double min,
    required double max,
    required double value,
    String? description = '',
  }) async {
    var result = await showDialog(
      context: context,
      builder: (context) {
        return FlushParameterControlDialog(
            title: title,
            sensor: deviceModel,
            value: value,
            min: min,
            max: max,
            step: step,
            description: description);
      },
    );

    if (result != null) {
      if (controlCode == FlushSensorModel.codeNearDetection ||
          controlCode == FlushSensorModel.codeFarDetection) {
        result = (result * 100).toInt();
      }
      if (controlCode == FlushSensorModel.codeNoneDelay) {
        result = (result * 10).toInt();
      }
      if (controlCode == FlushSensorModel.codePresenceDelay) {
        result = (result * 10).toInt();
      }
      bloc.add(
        FlushSensorChangeValueEvent(value: result, code: controlCode),
      );
    }
  }
}
