// video_bloc.dart
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:tanami_app/features/MainScreens/Invest/domain/model/detail_io_model.dart';
import 'package:tanami_app/features/MainScreens/Invest/domain/repository/io_api.dart';

import '../../../../../../Api_Helper/base_manager.dart';
import '../../../../../../Globalconst.dart';
import 'io_details_event.dart';
import 'io_details_state.dart';

class IODetailsBloc extends Bloc<IODetailsEvent, IODetailsState> {
  final int id;
  IODetailsBloc({required this.id}) : super(IODetailsStateInitial()) {
    on<IODetailsEvent>(mapEventToState);
  }

  Future<void> mapEventToState(
      IODetailsEvent event, Emitter<IODetailsState> emit) async {
    emit(IODetailsStateLoading());
    try {
      ResponseData response = await IOApi().iODetailsAPI(id);
      if (response.status == ResponseStatus.SUCCESS) {
        DetailIOModel detailIOModel = DetailIOModel.fromJson(response.data);
        emit(IODetailsStateLoaded(detailIOModel));
      }
    } catch (e) {
      emit(IODetailsError(Globalconst.languageSelected == "en"
          ? 'Oops Something went wrong'
          : "تبا شيء ما حدث بشكل خاطئ"));
    }
  }
}
