import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/widgets/air_quality_end_side_gauge_and_info.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/widgets/air_quality_end_side_live_indicator.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/widgets/aqi_sub_value_widget.dart';
import 'package:syncrow_web/pages/analytics/modules/air_quality/widgets/aqi_type_dropdown.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/realtime_device_changes/realtime_device_changes_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/utils/style.dart';

class AqiDeviceInfo extends StatelessWidget {
  const AqiDeviceInfo({super.key});

  String _getValueForStatus(
    List<Status> deviceStatusList,
    String code, {
    double defaultValue = 0,
    String Function(int value)? formatter,
  }) {
    try {
      final foundStatus = deviceStatusList.firstWhere((e) => e.code == code);
      final value = foundStatus.value.toString();
      final intValue = int.parse(value);
      return formatter != null ? formatter(intValue) : intValue.toString();
    } catch (e) {
      return defaultValue.toString();
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<RealtimeDeviceChangesBloc, RealtimeDeviceChangesState>(
      builder: (context, state) {
        final status = state.deviceStatusList;
        final humidityValue = _getValueForStatus(
          status,
          'humidity_value',
          formatter: (value) => value.toStringAsFixed(0),
        );

        final tempValue = _getValueForStatus(
          status,
          'temp_current',
          formatter: (value) => (value / 10).toStringAsFixed(0),
        );
        final pm25Value = _getValueForStatus(
          status,
          'pm25_value',
          formatter: (value) => value.toString().padLeft(3, '0'),
        );
        final pm10Value = _getValueForStatus(
          status,
          'pm10',
          formatter: (value) => value.toString().padLeft(3, '0'),
        );
        final co2Value = _getValueForStatus(
          status,
          'co2_value',
          formatter: (value) => value.toString().padLeft(4, '0'),
        );
        final ch2oValue = _getValueForStatus(
          status,
          'ch2o_value',
          formatter: (value) => (value / 100).toStringAsFixed(2),
        );
        final tvocValue = _getValueForStatus(
          status,
          'voc_value',
          formatter: (value) => (value / 100).toStringAsFixed(2),
        );

        return Container(
          decoration: secondarySection.copyWith(boxShadow: const []),
          padding: const EdgeInsetsDirectional.all(20),
          child: Column(
            spacing: 6,
            mainAxisSize: MainAxisSize.max,
            children: [
              const AirQualityEndSideLiveIndicator(),
              AirQualityEndSideGaugeAndInfo(
                aqiLevel: status
                    .firstWhere(
                      (e) => e.code == 'air_quality_index',
                      orElse: () => Status(code: 'air_quality_index', value: ''),
                    )
                    .value
                    .toString(),
                temperature: int.parse(tempValue),
                humidity: int.parse(humidityValue),
              ),
              const SizedBox(height: 20),
              AqiSubValueWidget(
                range: (0, 999),
                label: AqiType.pm25.value,
                value: pm25Value,
                unit: AqiType.pm25.unit,
              ),
              AqiSubValueWidget(
                range: (0, 999),
                label: AqiType.pm10.value,
                value: pm10Value,
                unit: AqiType.pm10.unit,
              ),
              AqiSubValueWidget(
                range: (0, 5),
                label: AqiType.hcho.value,
                value: ch2oValue,
                unit: AqiType.hcho.unit,
              ),
              AqiSubValueWidget(
                range: (0, 999),
                label: AqiType.tvoc.value,
                value: tvocValue,
                unit: AqiType.tvoc.unit,
              ),
              AqiSubValueWidget(
                range: (0, 5000),
                label: AqiType.co2.value,
                value: co2Value,
                unit: AqiType.co2.unit,
              ),
            ],
          ),
        );
      },
    );
  }
}
