import 'package:be_happy/domain/usecase/upload_profile_photo_usecase.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../domain/entities/user_profile.dart';
import '../../domain/usecase/get_profile_usecase.dart';
import '../../domain/usecase/update_profile_usecase.dart';
import '../event/profile_event.dart';
import '../state/profile_state.dart';

class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
  final GetProfileUseCase getProfileUseCase;
  final UploadProfilePhotoUsecase uploadProfilePhotoUsecase;
  final UpdateProfileUseCase updateProfileUseCase;


  ProfileBloc(this.getProfileUseCase,
      this.uploadProfilePhotoUsecase,
      this.updateProfileUseCase)
      : super(ProfileState.initial()) {
    on<ProfileStarted>(_onStarted);
    on<ProfileUpdated>(_onUpdated);
    on<ProfilePhotoUpdated>(_onPhotoUploaded);
  }

  Future<void> _onStarted(
      ProfileStarted event,
      Emitter<ProfileState> emit,
      ) async {
    emit(state.copyWith(isLoading: true));
    print("_onStarted method was start");
    try {
      final profile = await getProfileUseCase();
      print(profile.name);

      emit(state.copyWith(isLoading: false, profile: profile));
    } catch (e) {
      emit(state.copyWith(isLoading: false, error: "STARTED: ${e.toString()}"));
    }
  }

  Future<void> _onUpdated(
      ProfileUpdated event,
      Emitter<ProfileState> emit,
      ) async {
    final profile = await getProfileUseCase();
    emit(state.copyWith(profile: profile));
  }

  Future<void> _onPhotoUploaded(
      ProfilePhotoUpdated event,
      Emitter<ProfileState> emit,
      ) async {
    emit(state.copyWith(isLoading: true));
    try {
      final avatarId = await uploadProfilePhotoUsecase(event.imageFile);

      if (avatarId != null) {
        // Отправляем PATCH запрос с новым avatarId
        await updateProfileUseCase(UserProfile(
          name: '',
          birthDate: '',
          phone: '',
          email: '',
          avatarId: avatarId,
        ));
      }

      final updatedProfile = await getProfileUseCase();

      emit(state.copyWith(isLoading: false, profile: updatedProfile, error: null));
    } catch (e) {
      emit(state.copyWith(isLoading: false, error: "Ошибка загрузки: ${e.toString()}"));
    }
  }
}
