import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/workout_menu_tree.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/repository/workout_tree_repository.dart';
import 'package:aitrainer_app/service/logging.dart';

import 'package:aitrainer_app/util/common.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:aitrainer_app/util/diagram_data.dart';
import 'package:aitrainer_app/util/track.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';

part 'development_by_muscle_event.dart';
part 'development_by_muscle_state.dart';

class DevelopmentByMuscleBloc extends Bloc<DevelopmentByMuscleEvent, DevelopmentByMuscleState> with Common, Logging {
  final WorkoutTreeRepository workoutTreeRepository;
  final ExerciseRepository exerciseRepository = ExerciseRepository();
  final List<DiagramData> diagramData = [];

  @override
  DevelopmentByMuscleBloc({required this.workoutTreeRepository}) : super(DevelopmentByMuscleStateInitial()) {
    on<DevelopmentByMuscleLoad>(_onLoad);
  }

  void getTree() {
    workoutTreeRepository.sortedTree.clear();
    workoutTreeRepository.sortByMuscleType();

    workoutTreeRepository.sortedTree.forEach((key, value) {
      List<WorkoutMenuTree> listWorkoutTree = value;
      listWorkoutTree.forEach((workoutTree) {
        workoutTree.selected = false;
      });
    });
  }

  void _onLoad(DevelopmentByMuscleLoad event, Emitter<DevelopmentByMuscleState> emit) {
    emit(DevelopmentByMuscleLoadingState());
    Track().track(TrackingEvent.my_muscle_development);
    Cache().setActivityDonePrefs(ActivityDone.isMuscleDevelopmentSeen);
    getTree();
    emit(DevelopmentByMuscleReadyState());
  }
}
