import 'package:bloc/bloc.dart';
import 'package:dartz/dartz.dart' hide Order;
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart' hide Order;

import '../../../domain/order/order.dart';
import '../../../domain/printer/printer.dart';

part 'print_struck_event.dart';
part 'print_struck_state.dart';
part 'print_struck_bloc.freezed.dart';

@injectable
class PrintStruckBloc extends Bloc<PrintStruckEvent, PrintStruckState> {
  final IPrinterRepository _printerRepository;

  PrintStruckBloc(this._printerRepository) : super(PrintStruckState.initial()) {
    on<PrintStruckEvent>(_onPrintStruckEvent);
  }

  Future<void> _onPrintStruckEvent(
    PrintStruckEvent event,
    Emitter<PrintStruckState> emit,
  ) {
    return event.map(
      order: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckOrder(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
      cashier: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckCashier(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
      payment: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckPayment(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
      voided: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckVoid(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
      splitBill: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckSplitBill(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
      kitchen: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckKitchen(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
      bar: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckBar(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
      checker: (e) async {
        Either<PrinterFailure, Unit> failureOrSuccess;

        emit(state.copyWith(isPrinting: true, failureOrPrintStruck: none()));

        failureOrSuccess = await _printerRepository.printStruckChecker(
          order: e.order,
        );

        emit(
          state.copyWith(
            isPrinting: false,
            failureOrPrintStruck: optionOf(failureOrSuccess),
          ),
        );
      },
    );
  }
}
