import 'dart:io';

import 'package:bloc/bloc.dart';
import 'package:tanami_app/core/utils/secure/secure_storage_service.dart';

import '../../../../Api_Helper/base_manager.dart';
import '../../../../core/utils/device_info/device_info_data.dart';
import '../../../../features/MainScreens/Settings/domain/repository/settings_api.dart';
import '../../../../features/register/Repository/RegisterApi.dart';
import 'toggle_event.dart';
import 'toggle_state.dart';

class ToggleBloc extends Bloc<ToggleEvent, ToggleState> {
  final String type;

  final SharedPreferenceLocalData secureStorageService =
      SharedPreferenceLocalData();

  ToggleBloc(this.type) : super(ToggleInitial()) {
    on<ToggleSwitch>(_onToggleSwitch);
    _initializeToggleState();
  }

  void _onToggleSwitch(ToggleSwitch event, Emitter<ToggleState> emit) async {
    if (state is ToggleOn) {
      if (type == "biometric") {
        emit(ToggleOff());

        Map<String, dynamic> biometricdata = {
          "code": await secureStorageService.read("temp_token"),
          "is_2FA_on": false,
          "deviceId": await DeviceInfoData().getDeviceId(),
          "biometric_type": Platform.isIOS ? "face" : "fingerprint"
        };

        ResponseData response =
            await RegisterAPIService().BiometricUpdate(biometricdata);
        if (response.status == ResponseStatus.SUCCESS) {
          await secureStorageService.write(type, 'off');
          await secureStorageService.write("biometric", 'of');
        } else {
          emit(ToggleOn());
          await secureStorageService.write(type, 'on');
        }
      } else if (type == "notification") {
        emit(ToggleOff());

        ResponseData response = await SettingsApi().updateNotification();
        if (response.status == ResponseStatus.SUCCESS) {
          await secureStorageService.write(type, 'off');
          await secureStorageService.write("notification", 'of');
        } else {
          emit(ToggleOn());
          await secureStorageService.write(type, 'on');
        }
      } else {
        emit(ToggleOff());
        await secureStorageService.write(type, 'off');
      }
    } else {
      if (type == "biometric") {
        emit(ToggleOn());

        Map<String, dynamic> biometricdata = {
          "code": await secureStorageService.read("temp_token"),
          "is_2FA_on": true,
          "deviceId": await DeviceInfoData().getDeviceId(),
          "biometric_type": Platform.isIOS ? "face" : "fingerprint"
        };

        ResponseData response =
            await RegisterAPIService().BiometricUpdate(biometricdata);
        if (response.status == ResponseStatus.SUCCESS) {
          await secureStorageService.write("biometric", 'on');
          await secureStorageService.write(type, 'on');
        } else {
          emit(ToggleOff());
          await secureStorageService.write(type, 'off');
        }
      } else if (type == "notification") {
        emit(ToggleOn());

        ResponseData response = await SettingsApi().updateNotification();
        if (response.status == ResponseStatus.SUCCESS) {
          await secureStorageService.write(type, 'on');
          await secureStorageService.write("notification", 'on');
        } else {
          emit(ToggleOff());
          await secureStorageService.write(type, 'off');
        }
      } else {
        emit(ToggleOn());
        await secureStorageService.write(type, 'on');
      }
    }
  }

  void _initializeToggleState() async {
    final toggleValue = await secureStorageService.read(type);
    if (toggleValue == 'on') {
      add(ToggleSwitch()); // Initializing the state by adding an event
    }
  }
}
