import 'package:dartz/dartz.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';

import '../../../domain/analytic/analytic.dart';
import '../../../domain/analytic/repositories/i_analytic_repository.dart';
import '../../../domain/outlet/outlet.dart';

part 'transaction_report_event.dart';
part 'transaction_report_state.dart';
part 'transaction_report_bloc.freezed.dart';

@injectable
class TransactionReportBloc
    extends Bloc<TransactionReportEvent, TransactionReportState> {
  final IAnalyticRepository _analyticRepository;
  final IOutletRepository _outletRepository;

  TransactionReportBloc(this._analyticRepository, this._outletRepository)
    : super(TransactionReportState.initial()) {
    on<TransactionReportEvent>(_onTransactionReportEvent);
  }

  Future<void> _onTransactionReportEvent(
    TransactionReportEvent event,
    Emitter<TransactionReportState> emit,
  ) {
    return event.map(
      fetchedOutlet: (e) async {
        emit(
          state.copyWith(isFetchingOutlet: true, failureOptionOutlet: none()),
        );

        final result = await _outletRepository.currentOutlet();

        var data = result.fold(
          (f) => state.copyWith(failureOptionOutlet: optionOf(f)),
          (currentOutlet) => state.copyWith(outlet: currentOutlet),
        );

        emit(data.copyWith(isFetchingOutlet: false));
      },
      fetchedTransaction: (e) async {
        emit(state.copyWith(isFetching: true, failureOptionAnalytic: none()));

        var newState = state;

        final category = await _analyticRepository.getCategory(
          dateFrom: e.dateFrom,
          dateTo: e.dateTo,
        );
        final profitLoss = await _analyticRepository.getProfitLoss(
          dateFrom: e.dateFrom,
          dateTo: e.dateTo,
        );
        final paymentMethod = await _analyticRepository.getPaymentMethod(
          dateFrom: e.dateFrom,
          dateTo: e.dateTo,
        );
        final product = await _analyticRepository.getProduct(
          dateFrom: e.dateFrom,
          dateTo: e.dateTo,
        );

        newState = category.fold(
          (f) => newState.copyWith(failureOptionAnalytic: optionOf(f)),
          (categoryAnalytic) =>
              newState.copyWith(categoryAnalytic: categoryAnalytic),
        );
        newState = profitLoss.fold(
          (f) => newState.copyWith(failureOptionAnalytic: optionOf(f)),
          (profitLossAnalytic) =>
              newState.copyWith(profitLossAnalytic: profitLossAnalytic),
        );
        newState = paymentMethod.fold(
          (f) => newState.copyWith(failureOptionAnalytic: optionOf(f)),
          (paymentMethodAnalytic) =>
              newState.copyWith(paymentMethodAnalytic: paymentMethodAnalytic),
        );
        newState = product.fold(
          (f) => newState.copyWith(failureOptionAnalytic: optionOf(f)),
          (productAnalytic) =>
              newState.copyWith(productAnalytic: productAnalytic),
        );

        emit(newState.copyWith(isFetching: false));
      },
    );
  }
}
