import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';

import '../../common/extension/extension.dart';

part 'report_event.dart';
part 'report_state.dart';
part 'report_bloc.freezed.dart';

@injectable
class ReportBloc extends Bloc<ReportEvent, ReportState> {
  ReportBloc() : super(ReportState.initial()) {
    on<ReportEvent>(_onReportEvent);
  }

  Future<void> _onReportEvent(ReportEvent event, Emitter<ReportState> emit) {
    return event.map(
      dateChanged: (e) async {
        emit(
          state.copyWith(
            startDate: e.startDate,
            endDate: e.endDate,
            rangeDateFormatted:
                '${e.startDate.toFormattedDate()} - ${e.endDate.toFormattedDate()}',
          ),
        );
      },
      menuChanged: (e) async {
        emit(state.copyWith(selectedMenu: e.index, title: e.title));
      },
    );
  }
}
