import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nut_player_example/src/features/main_screen/domain/url_bloc/url_bloc.dart';
import 'package:nut_player_example/src/features/main_screen/presentation/options_list.dart';
import 'package:nut_player_example/src/features/main_screen/presentation/url_input.dart';

class URLView extends StatelessWidget {

  const URLView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<UrlBloc, UrlState>(
      builder: (context, state) {
        return Column(
            children: _buildAllWidgets(context, state, null)
        );
      },
    );
  }

  List<Widget> _buildAllWidgets(BuildContext context, UrlState state, int? index) {
    List<Widget> widgets = [];

    final parent = state.parent;
    if (parent != null) {
      widgets.addAll(_buildAllWidgets(context, parent, state.currentIndex ?? state.initialIndex));
    }

    var newWidget = _buildWidget(state, context, index);

    if (newWidget != null) {
      widgets.removeWhere((element) => element.key == newWidget.key);
      widgets.add(newWidget);
    }

    return widgets;
  }

  Widget? _buildWidget(UrlState state, BuildContext context, int? index) {
    if (state.uiType == UIType.listWithInput) {
      return _buildComplexWidget(state, context);

    } else if (state.uiType == UIType.input) {
      return URLInput(
        state.title,
        state.visibleUrlPath!,
          (value) {
          final createString = state.createStringEvent;
          if (createString != null) {
            context.read<UrlBloc>().add(createString(value));
          }
        },
        key: state.key,
      );

    } else if (state.uiType == UIType.list) {
      var isListCheckmarked = state.options.length > 2;
      return OptionsList(
          state.options,
          isListCheckmarked ? state.currentIndex : index,
          state.title,
          isListCheckmarked ? ListType.checkmark : ListType.radio,
          (selectedIndex) {
            final createIndexed = state.createIndexedEvent;
            if (createIndexed != null) {
              context.read<UrlBloc>().add(createIndexed(selectedIndex));
            }
          },
          key: state.key
      );

    } else {
      return null;
    }
  }

  Widget? _buildComplexWidget(UrlState state, BuildContext context) {
    if (state.visibleUrlPath == null) { return null; }
    return Column(
      children: [
        URLInput(
          state.title,
          state.visibleUrlPath!,
          (value) {
            final createString = state.createStringEvent;
            if (createString != null) {
              context.read<UrlBloc>().add(createString(value));
            }
          },
          key: state.key,
        ),
        OptionsList(
          state.options,
          state.currentIndex,
          null,
          ListType.checkmark,
          (selectedIndex) {
            final createIndexed = state.createIndexedEvent;
            if (createIndexed != null) {
              context.read<UrlBloc>().add(createIndexed(selectedIndex));
            }
          },
        ),
      ],
    );
  }
}