import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/analytics/models/power_clamp_energy_status.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/blocs/realtime_device_changes/realtime_device_changes_bloc.dart';
import 'package:syncrow_web/pages/analytics/modules/energy_management/widgets/power_clamp_energy_status_widget.dart';
import 'package:syncrow_web/pages/analytics/widgets/analytics_sidebar_header.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/style.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<RealtimeDeviceChangesBloc, RealtimeDeviceChangesState>(
      builder: (context, state) {
        return Container(
          decoration: subSectionContainerDecoration.copyWith(
            borderRadius: BorderRadius.circular(30),
          ),
          padding: const EdgeInsetsDirectional.all(32),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const AnalyticsSidebarHeader(title: 'Presence Sensor'),
              Expanded(
                child: SizedBox(
                  child: PowerClampEnergyStatusWidget(
                    status: [
                      PowerClampEnergyStatus(
                        iconPath: Assets.presenceState,
                        title: 'Presence Status',
                        value: _valueFromCode(
                          'presence_state',
                          state.deviceStatusList,
                        ),
                        unit: '',
                      ),
                      PowerClampEnergyStatus(
                        iconPath: Assets.presenceTimeIcon,
                        title: 'Presence Time',
                        value:
                            '${_valueFromCode('none_body_time', state.deviceStatusList)} Min',
                        unit: '',
                      ),
                      PowerClampEnergyStatus(
                        iconPath: Assets.currentDistanceIcon,
                        title: 'Detection Distance',
                        value:
                            '${_valueFromCode('space_move_val', state.deviceStatusList)} M',
                        unit: '',
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 20),
              Expanded(
                flex: 2,
                child: FittedBox(
                  child: Image.asset(Assets.autocadOccupancyImage),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  String _valueFromCode(
    String code,
    List<Status> status, {
    String? defaultValue,
  }) {
    final value = status
        .firstWhere(
          (e) => e.code == code,
          orElse: () => Status(code: '--', value: '--'),
        )
        .value
        .toString();
    return value == 'null' ? defaultValue ?? '--' : value;
  }
}
