import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nut_player_example/src/common/models/option_data.dart';
import 'package:nut_player_example/src/features/main_screen/domain/firebase/firebase_provider.dart';

part 'json_event.dart';
part 'json_state.dart';

class JsonBloc extends Bloc<JsonEvent, JsonState> {

  JsonBloc() : super(JsonState.create()) {
    on<ChooseWayToGetConfigEvent>(_onChooseWayToGetConfigEvent);
    on<ChooseWayToGetLinkEvent>(_onChooseWayToGetLinkEvent);
    on<ChooseFirebaseConfigEvent>(_onChooseFirebaseConfigEvent);
    on<ChooseExampleLinkEvent>(_onChooseExampleLinkEvent);
    on<TypeOwnLinkEvent>(_onTypeOwnLinkEvent);
    on<JsonResetEvent>(_onResetEvent);
  }

  _onResetEvent(JsonResetEvent event, Emitter<JsonState> emit) {
    emit(JsonState.create());
  }

  _onChooseWayToGetConfigEvent(ChooseWayToGetConfigEvent event, Emitter<JsonState> emit) async {
    JsonState? parentState = state;
    while (parentState?.parent != null) { parentState = parentState?.parent; }

    if (event.selectedIndex == 0) {
      emit(ConfigLinkChosenOptionState.create(parentState));
    } else {
      emit(FirebaseChosenOptionState.create(parentState, const [], null));
      var fileNames = await FirebaseProvider.getConfigNames();
      emit(FirebaseChosenOptionState.create(parentState, _createFBOptions(fileNames), null));
    }
  }

  List<OptionData> _createFBOptions(List<String> names) {
    return names.indexed.map((e) =>
        OptionData.withValueEqualTitle(
          Key("ListElementConfig${e.$1}ID"),
          e.$2)
        ).toList();
  }

  _onChooseWayToGetLinkEvent(ChooseWayToGetLinkEvent event, Emitter<JsonState> emit) {
    JsonState? parentState = state;
    while (parentState?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(event.selectedIndex == 0
        ? UseLinkExampleState.create(parent: parentState, selectedIndex: null)
        : UseJsonOwnLinkState.create(parent: parentState, urlPath: null, isLive: null));
  }

  _onChooseFirebaseConfigEvent(ChooseFirebaseConfigEvent event, Emitter<JsonState> emit) {
    JsonState? parentState = state;
    while (parentState?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(FirebaseChosenOptionState.create(parentState, state.options, event.selectedIndex));
  }

  _onChooseExampleLinkEvent(ChooseExampleLinkEvent event, Emitter<JsonState> emit) {
    JsonState? parentState = state;
    while (parentState?.parent?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(UseLinkExampleState.create(parent: parentState, selectedIndex: event.selectedIndex));
  }

  _onTypeOwnLinkEvent(TypeOwnLinkEvent event, Emitter<JsonState> emit) {
    JsonState? parentState = state;
    while (parentState?.parent?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(UseJsonOwnLinkState.create(parent: parentState, urlPath: event.urlPath, isLive: event.isLive));
  }
}
