import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/core/application/common/date_time_extension.dart';
import 'package:krow/features/shifts/domain/blocs/complete_dialog/shift_complete_dialog_event.dart';
import 'package:krow/features/shifts/domain/blocs/complete_dialog/shift_complete_dialog_state.dart';

class CompleteDialogBloc
    extends Bloc<CompleteDialogEvent, CompleteDialogState> {
  CompleteDialogBloc() : super(const CompleteDialogState()) {
    on<InitializeCompleteDialog>((event, emit) {

      emit(state.copyWith(
        minLimit: event.minLimit,
        startTime: event.minLimit?.toHourMinuteString() ?? DateTime.now().toHourMinuteString(),
        endTime: event.minLimit?.add(Duration(minutes: event.breakDurationInMinutes)).toHourMinuteString() ?? DateTime.now().add(Duration(minutes: event.breakDurationInMinutes)).toHourMinuteString(),
        breakDuration:  Duration(minutes: event.breakDurationInMinutes),
      ));
    });

    on<SelectBreakStatus>((event, emit) {
      emit(state.copyWith(status: event.status));
    });

    on<SelectReason>((event, emit) {
      emit(state.copyWith(selectedReason: event.reason));
    });

    on<ChangeStartTime>((event, emit) {
      emit(state.copyWith(startTime: event.startTime.toHourMinuteString(),endTime: event.startTime.add(state.breakDuration).toHourMinuteString()));
    });

    on<ChangeEndTime>((event, emit) {
      emit(state.copyWith(endTime: event.endTime.toHourMinuteString()));
    });

    on<ChangeAdditionalReason>((event, emit) {
      emit(state.copyWith(additionalReason: event.additionalReason));
    });
  }
}
