import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:ongbut_ntmanh/app/module/coursedetail/model/course_detail_model.dart';
import '../request/course_detail_request.dart';

part 'course_detail_event.dart';
part 'course_detail_state.dart';

class CourseDetailBloc extends Bloc<CourseDetailEvent, CourseDetailState> {
  CourseDetailBloc() : super(const CourseDetailInitial()) {
    on<RequestCourseDetail>((event, emit) async {
      await _requestCourseDetail(event, emit);
    });
  }

  Future<void> _requestCourseDetail(RequestCourseDetail event, Emitter<CourseDetailState> emit) async {
    if(event.idCourse.isEmpty){
      emit(const CourseDetailFailed('NotID'));
      return;
    }
    RemoteCourseDetail remoteCourseDetail = RemoteCourseDetail();
    try {
      CourseDetailModel? courseDetailModel = await remoteCourseDetail.getCourseDetailModel(context: event.context,idCourse: event.idCourse);
      emit(CourseDetailSuccess(courseDetailModel!));
    } catch (e) {
      emit(const CourseDetailFailed('request CourseDetail false21!!'));
    }
  }

  @override
  Future<void> close() {
    return super.close();
  }

}

