import 'package:flutter_bloc/flutter_bloc.dart';
import 'redemption_event.dart';
import 'redemption_state.dart';

class RedemptionBloc extends Bloc<RedemptionEvent, RedemptionState> {
  RedemptionBloc() : super(const RedemptionState()) {
    on<LoadRecentRedemptions>(_onLoadRecent);
    on<ManualTicketSearched>(_onManualSearch);
    on<ApproveRedemption>(_onApprove);
    on<DisapproveRedemption>(_onDisapprove);
  }

  void _onLoadRecent(
      LoadRecentRedemptions event, Emitter<RedemptionState> emit) {
    emit(state.copyWith(
      status: RedemptionStatus.success,
      recentRedemptions: [
        {
          'title': 'Melbourne Ultimate Pass',
          'image': 'assets/login/bg.png',
          'status': true
        },
        {
          'title': 'Dubai Lite Pass',
          'image': 'assets/login/bg.png',
          'status': true
        },
        {
          'title': 'Tokyo Ultimate Pass',
          'image': 'assets/login/bg.png',
          'status': false
        },
      ],
    ));
  }

  void _onManualSearch(
      ManualTicketSearched event, Emitter<RedemptionState> emit) {
    emit(state.copyWith(
      status: RedemptionStatus.success,
      selectedRedemption: {
        'name': 'Lila Heart',
        'phone': '(+971) 050 421 4456',
        'email': 'lila12@gmail.com',
        'cardType': 'Selective Pass',
        'validity': 'Valid until December 31, 2024',
        'attraction': 'The Enchanted Garden',
        'city': 'Dubai',
        'date': 'November 15, 2024',
        'time': '10:00AM - 9:00PM',
      },
    ));
  }

  void _onApprove(ApproveRedemption event, Emitter<RedemptionState> emit) {
    emit(state.copyWith(status: RedemptionStatus.success));
  }

  void _onDisapprove(DisapproveRedemption event, Emitter<RedemptionState> emit) {
    emit(state.copyWith(status: RedemptionStatus.failed));
  }
}
