part of 'earnings_bloc.dart';

@immutable
class EarningsState {
  const EarningsState({
    required this.status,
    required this.tabIndex,
    required this.balancePeriod,
    required this.earnings,
    required this.tabs,
  });

  const EarningsState.initial({
    this.status = StateStatus.idle,
    this.tabIndex = 0,
    this.balancePeriod = BalancePeriod.week,
    this.earnings = const EarningsSummaryEntity.empty(),
    this.tabs = defaultEarningTabs,
  });

  final StateStatus status;
  final int tabIndex;
  final BalancePeriod balancePeriod;
  final EarningsSummaryEntity earnings;
  final List<EarningsTabState> tabs;

  EarningsTabState get currentTab => tabs[tabIndex];

  double get totalEarnings {
    return balancePeriod == BalancePeriod.week
        ? earnings.totalEarningsByWeek
        : earnings.totalEarningsByMonth;
  }

  double get totalHours {
    return balancePeriod == BalancePeriod.week
        ? earnings.totalWorkedHoursByWeek
        : earnings.totalWorkedHoursByMonth;
  }

  double get totalPayout {
    return balancePeriod == BalancePeriod.week
        ? earnings.payoutByWeek
        : earnings.payoutByMonth;
  }

  EarningsState copyWith({
    StateStatus? status,
    int? tabIndex,
    BalancePeriod? balancePeriod,
    EarningsSummaryEntity? earnings,
    List<EarningsTabState>? tabs,
  }) {
    return EarningsState(
      status: status ?? this.status,
      tabIndex: tabIndex ?? this.tabIndex,
      balancePeriod: balancePeriod ?? this.balancePeriod,
      earnings: earnings ?? this.earnings,
      tabs: tabs ?? this.tabs,
    );
  }

  EarningsState copyWithTab({
    required EarningsTabState tab,
    int? tabIndex,
    StateStatus? status,
  }) {
    return copyWith(
      tabs: List.from(tabs)..[tabIndex ?? this.tabIndex] = tab,
      status: status,
    );
  }

  EarningsState updateCurrentTab({
    List<EarningShiftEntity>? items,
    PaginationStatus? status,
    bool? hasMoreItems,
    String? label,
    String? paginationCursor,
  }) {
    return copyWith(
      tabs: List.from(tabs)
        ..[tabIndex] = currentTab.copyWith(
          items: items,
          status: status,
          hasMoreItems: hasMoreItems,
          label: label,
          paginationCursor: paginationCursor,
        ),
    );
  }
}
