import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nut_player_example/src/common/models/option_data.dart';
part 'url_event.dart';
part 'url_state.dart';

class UrlBloc extends Bloc<UrlEvent, UrlState> {

  UrlBloc() : super(UrlState.create()) {
    on<ChooseWayToLoadVideoEvent>(_onChooseWayToGetConfigEvent);
    on<ChooseExampleVideoEvent>(_onChooseExampleLinkEvent);
    on<ChooseWayToGetLinkEvent>(_onChooseWayToGetLinkEvent);
    on<TypeOwnLinkEvent>(_onTypeOwnLinkEvent);
    on<ChooseExtraOptionsEvent>(_onChooseExtraOptionsEvent);
    on<ChooseConfigFileEvent>(_onChooseConfigFileEvent);
    on<ChooseVideoFormatEvent>(_onChoseVideoFormatEvent);
    on<TypeOwnLinkWithoutFormatEvent>(_onTypeOwnLinkWithoutFormatEvent);
    on<UrlResetEvent>(_onResetEvent);
  }

  _onResetEvent(UrlResetEvent event, Emitter<UrlState> emit) {
    emit(UrlState.create());
  }

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

    emit(event.selectedIndex == 0
        ? UseExampleChosenOptionState.create(parentState, null)
        : LoadWithLinkChosenOptionState.create(parentState));
  }

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

    emit(UseExampleChosenOptionState.create(parentState, event.selectedIndex));
  }

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

    emit(event.selectedIndex == 0
        ? UseOwnLinkState.create(parentState, null)
        : ChooseExtraOptionsState.create(parentState));
  }

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

    emit(UseOwnLinkState.create(parentState, event.urlPath));
  }

  _onChooseExtraOptionsEvent(ChooseExtraOptionsEvent event, Emitter<UrlState> emit) {
    UrlState? parentState = state;
    while (parentState?.parent?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(event.selectedIndex == 0
        ? UseOwnLinkWithoutFormat.create(parentState, null, null)
        : ChooseJsonFileState.create(parentState, null));
  }

  _onChooseConfigFileEvent(ChooseConfigFileEvent event, Emitter<UrlState> emit) {
    UrlState? parentState = state;
    while (parentState?.parent?.parent?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(ChooseJsonFileState.create(parentState, event.selectedIndex));
  }

  _onChoseVideoFormatEvent(ChooseVideoFormatEvent event, Emitter<UrlState> emit) {
    UrlState? parentState = state;
    while (parentState?.parent?.parent?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(UseOwnLinkWithoutFormat.create(parentState, state.urlPath, event.selectedIndex));
  }

  _onTypeOwnLinkWithoutFormatEvent(TypeOwnLinkWithoutFormatEvent event, Emitter<UrlState> emit) {
    UrlState? parentState = state;
    while (parentState?.parent?.parent?.parent?.parent != null) { parentState = parentState?.parent; }

    emit(UseOwnLinkWithoutFormat.create(parentState, event.urlPath, state.currentIndex));
  }
}
