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/devices_mang_api.dart';

class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
  late bool deviceStatus;
  final String deviceId;
  Timer? _timer;

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

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

      emit(CurtainStatusLoaded(deviceStatus));
    } catch (e) {
      emit(CurtainError(e.toString()));
    }
  }

  void _listenToChanges(String deviceId) {
    try {
      DatabaseReference ref =
          FirebaseDatabase.instance.ref('device-status/$deviceId');
      Stream<DatabaseEvent> stream = ref.onValue;

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

        List<Status> statusList = [];
        if (data['status'] != null) {
          for (var element in data['status']) {
            statusList.add(
              Status(
                code: element['code'].toString(),
                value: element['value'].toString(),
              ),
            );
          }
        }
        if (statusList.isNotEmpty) {
          bool 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));
  }

  FutureOr<void> _onCurtainControl(
      CurtainControl event, Emitter<CurtainState> emit) async {
    final oldValue = deviceStatus;

    _updateLocalValue(event.value, emit);

    emit(CurtainStatusLoaded(deviceStatus));

    await _runDebounce(
      deviceId: event.deviceId,
      code: event.code,
      value: event.value,
      oldValue: oldValue,
      emit: emit,
      isBatch: false,
    );
  }

  Future<void> _runDebounce({
    required dynamic deviceId,
    required String code,
    required bool value,
    required bool oldValue,
    required Emitter<CurtainState> emit,
    required bool isBatch,
  }) async {
    late String id;

    if (deviceId is List) {
      id = deviceId.first;
    } else {
      id = deviceId;
    }

    if (_timer != null) {
      _timer!.cancel();
    }
    _timer = Timer(const Duration(seconds: 1), () async {
      try {
        final controlValue = value ? 'open' : 'close';

        late bool response;
        if (isBatch) {
          response = await DevicesManagementApi()
              .deviceBatchControl(deviceId, code, controlValue);
        } else {
          response = await DevicesManagementApi()
              .deviceControl(deviceId, Status(code: code, value: controlValue));
        }

        if (!response) {
          _revertValueAndEmit(id, oldValue, emit);
        }
      } catch (e) {
        _revertValueAndEmit(id, oldValue, emit);
      }
    });
  }

  void _revertValueAndEmit(
      String deviceId, bool oldValue, Emitter<CurtainState> emit) {
    _updateLocalValue(oldValue, emit);
    emit(CurtainStatusLoaded(deviceStatus));
    emit(const CurtainControlError('Failed to control the device.'));
  }

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

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

  FutureOr<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()));
    }
  }

  FutureOr<void> _onCurtainBatchControl(
      CurtainBatchControl event, Emitter<CurtainState> emit) async {
    final oldValue = deviceStatus;

    _updateLocalValue(event.value, emit);

    emit(CurtainStatusLoaded(deviceStatus));

    await _runDebounce(
      deviceId: event.devicesIds,
      code: event.code,
      value: event.value,
      oldValue: oldValue,
      emit: emit,
      isBatch: true,
    );
  }

  FutureOr<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()));
    }
  }
}
