import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart';
import 'package:hoshan/data/model/report_model.dart';
import 'package:hoshan/data/repository/auth_repository.dart';
import 'package:shamsi_date/shamsi_date.dart';

part 'report_of_use_event.dart';
part 'report_of_use_state.dart';

class ReportOfUseBloc extends Bloc<ReportOfUseEvent, ReportOfUseState> {
  ReportOfUseBloc() : super(ReportOfUseInitial()) {
    on<ReportOfUseEvent>((event, emit) async {
      if (event is GetReport) {
        emit(ReportOfUseLoading());
        try {
          final response =
              await AuthRepository.getReport(startDate: event.startDate);
          if (response.report != null && response.report!.isEmpty) {
            emit(ReportOfUseEmpty());
          } else {
            emit(ReportOfUseSuccess(reportModel: response));
          }
        } on DioException catch (e) {
          emit(ReportOfUseFail());
          if (kDebugMode) {
            print('Error is: $e');
          }
        }
      }
      if (event is ClearReport) {
        emit(ReportOfUseInitial());
      }
    });
  }
}
