import 'package:flutter/foundation.dart';
import 'package:aitrainer_app/bloc/test_set_execute/test_set_execute_bloc.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/exercise_type.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/repository/mautic_repository.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:aitrainer_app/util/track.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

part 'test_set_new_event.dart';
part 'test_set_new_state.dart';

class TestSetNewBloc extends Bloc<TestSetNewEvent, TestSetNewState> {
  final ExerciseRepository exerciseRepository;
  final ExerciseType exerciseType;
  final int exercisePlanDetailId;
  final TestSetExecuteBloc executeBloc;

  late double quantity;
  late double unitQuantity;

  TestSetNewBloc({required this.exerciseRepository, required this.exerciseType, required this.exercisePlanDetailId, required this.executeBloc})
      : super(TestSetNewInitial()) {
    this._load();
    on<TestSetNewChangeQuantity>(_onChangeQuantity);
    on<TestSetNewChangeQuantityUnit>(_onChangeQuantityUnit);
    on<TestSetNewSubmit>(_onSubmit);
  }

  void _load() {
    exerciseRepository.exerciseType = exerciseType;
    quantity = 12;
    unitQuantity = 30;
    exerciseRepository.setQuantity(quantity);
    exerciseRepository.setUnit(exerciseType.unit);
    exerciseRepository.setUnitQuantity(unitQuantity);
    exerciseRepository.exercise!.exercisePlanDetailId = exercisePlanDetailId;
    exerciseRepository.start = DateTime.now();
    exerciseRepository.setCustomer(Cache().userLoggedIn!);
  }

  void _onChangeQuantity(TestSetNewChangeQuantity event, Emitter<TestSetNewState> emit) {
    emit(TestSetNewLoading());
    quantity = event.quantity;
    exerciseRepository.setQuantity(quantity);
    emit(TestSetNewReady());
  }

  void _onChangeQuantityUnit(TestSetNewChangeQuantityUnit event, Emitter<TestSetNewState> emit) {
    emit(TestSetNewLoading());
    unitQuantity = event.quantity;
    exerciseRepository.setUnitQuantity(unitQuantity);
    emit(TestSetNewReady());
  }

  void _onSubmit(TestSetNewSubmit event, Emitter<TestSetNewState> emit) async {
    emit(TestSetNewLoading());
    exerciseRepository.end = DateTime.now();
    await exerciseRepository.addExercise();
    executeBloc.add(TestSetExecuteExerciseFinished(exerciseTypeId: exerciseType.exerciseTypeId, quantity: quantity, unitQuantity: unitQuantity));
    if (kReleaseMode) {
      CustomerRepository customerRepository = CustomerRepository();
      customerRepository.customer = Cache().userLoggedIn;
      MauticRepository mauticRepository = MauticRepository(customerRepository: customerRepository);
      await mauticRepository.sendMauticExercise();
    }

    Track().track(TrackingEvent.test_set_new, eventValue: exerciseType.name);
    emit(TestSetNewReady());
  }
}
