import 'dart:async';

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_event.dart';
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_state.dart';
import 'package:syncrow_web/services/batch_control_devices_service.dart';
import 'package:syncrow_web/services/control_device_service.dart';
import 'package:syncrow_web/services/devices_mang_api.dart';

class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
  late bool deviceStatus;
  final String deviceId;
  final ControlDeviceService controlDeviceService;
  final BatchControlDevicesService batchControlDevicesService;

  CurtainBloc({
    required this.deviceId,
    required this.controlDeviceService,
    required this.batchControlDevicesService,
  }) : super(CurtainInitial()) {
    on<CurtainFetchDeviceStatus>(_onFetchDeviceStatus);
    on<CurtainFetchBatchStatus>(_onFetchBatchStatus);
    on<CurtainControl>(_onCurtainControl);
    on<CurtainBatchControl>(_onCurtainBatchControl);
    on<CurtainFactoryReset>(_onFactoryReset);
    on<StatusUpdated>(_onStatusUpdated);
  }

  Future<void> _onFetchDeviceStatus(
    CurtainFetchDeviceStatus event,
    Emitter<CurtainState> emit,
  ) async {
    emit(CurtainStatusLoading());
    try {
      final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
      _listenToChanges(event.deviceId, emit);
      deviceStatus = _checkStatus(status.status[0].value);
      emit(CurtainStatusLoaded(deviceStatus));
    } catch (e) {
      emit(CurtainError(e.toString()));
    }
  }

  void _listenToChanges(String deviceId, Emitter<CurtainState> emit) {
    try {
      final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
      final stream = ref.onValue;

      stream.listen((DatabaseEvent event) {
        final data = event.snapshot.value as Map<dynamic, dynamic>?;
        if (data == null) return;

        final statusList = <Status>[];
        if (data['status'] != null) {
          for (var element in data['status']) {
            statusList.add(
              Status(
                code: element['code'].toString(),
                value: element['value'].toString(),
              ),
            );
          }
        }
        if (statusList.isNotEmpty) {
          final newStatus = _checkStatus(statusList[0].value);
          if (newStatus != deviceStatus) {
            deviceStatus = newStatus;
            if (!isClosed) {
              add(StatusUpdated(deviceStatus));
            }
          }
        }
      });
    } catch (e) {
      emit(CurtainError('Failed to listen to changes: $e'));
    }
  }

  void _onStatusUpdated(
    StatusUpdated event,
    Emitter<CurtainState> emit,
  ) {
    emit(CurtainStatusLoading());
    deviceStatus = event.deviceStatus;
    emit(CurtainStatusLoaded(deviceStatus));
  }

  Future<void> _onCurtainControl(
    CurtainControl event,
    Emitter<CurtainState> emit,
  ) async {
    emit(CurtainStatusLoading());
    _updateLocalValue(event.value, emit);

    try {
      final controlValue = event.value ? 'open' : 'close';
      await controlDeviceService.controlDevice(
        deviceUuid: event.deviceId,
        status: Status(code: event.code, value: controlValue),
      );
    } catch (e) {
      _updateLocalValue(!event.value, emit);
      emit(CurtainControlError(e.toString()));
    }
  }

  void _updateLocalValue(bool value, Emitter<CurtainState> emit) {
    deviceStatus = value;
    emit(CurtainStatusLoaded(deviceStatus));
  }

  bool _checkStatus(String command) {
    return command.toLowerCase() == 'open';
  }

  Future<void> _onFetchBatchStatus(
    CurtainFetchBatchStatus event,
    Emitter<CurtainState> emit,
  ) async {
    emit(CurtainStatusLoading());
    try {
      final status = await DevicesManagementApi().getBatchStatus(event.devicesIds);
      deviceStatus = _checkStatus(status.status[0].value);
      emit(CurtainStatusLoaded(deviceStatus));
    } catch (e) {
      emit(CurtainError(e.toString()));
    }
  }

  Future<void> _onCurtainBatchControl(
    CurtainBatchControl event,
    Emitter<CurtainState> emit,
  ) async {
    emit(CurtainStatusLoading());
    _updateLocalValue(event.value, emit);

    try {
      final controlValue = event.value ? 'open' : 'stop';
      await batchControlDevicesService.batchControlDevices(
        uuids: event.devicesIds,
        code: event.code,
        value: controlValue,
      );
    } catch (e) {
      _updateLocalValue(!event.value, emit);
      emit(CurtainControlError(e.toString()));
    }
  }

  Future<void> _onFactoryReset(
    CurtainFactoryReset event,
    Emitter<CurtainState> emit,
  ) async {
    emit(CurtainStatusLoading());
    try {
      final response = await DevicesManagementApi().factoryReset(
        event.factoryReset,
        event.deviceId,
      );
      if (!response) {
        emit(const CurtainControlError('Failed'));
      } else {
        add(CurtainFetchDeviceStatus(event.deviceId));
      }
    } catch (e) {
      emit(CurtainControlError(e.toString()));
    }
  }
}
