import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:nut_player_example/src/common/views/checkmark_list_option.dart';
import 'package:nut_player_example/src/features/main_screen/domain/json_bloc/json_bloc.dart';
import 'package:nut_player_example/src/features/main_screen/presentation/radio_list_option.dart';
import '../../../common/models/option_data.dart';

enum ListType { radio, checkmark }

class OptionsList extends StatelessWidget {

  final List<OptionData> options;
  final int? selectedOptionIndex;
  final String? title;
  final ListType type;
  final Function(int)? onSelection;

  const OptionsList(this.options, this.selectedOptionIndex,  this.title, this.type, this.onSelection, {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      key: key,
      margin: const EdgeInsets.only(top: 20, left: 16, right: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (title != null && title!.isNotEmpty)
            Column(children: [
              Text(title!,
                  textAlign: TextAlign.right,
                  style: const TextStyle(
                      decoration: TextDecoration.none,
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      color: CupertinoColors.black)),
              const SizedBox(height: 15)
            ]),
          Container(
              decoration: const BoxDecoration(
                  color: CupertinoColors.white,
                  borderRadius: BorderRadius.all(Radius.circular(12))),
              child: ListView.separated(
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: options.length,
                  separatorBuilder: (_, int index) {
                    return Container(
                        margin: const EdgeInsets.only(left: 20),
                        height: 1,
                        color: CupertinoColors.separator);
                  },
                  itemBuilder: (_, int newIndex) {
                    return BlocBuilder<JsonBloc, JsonState>(
                      builder: (context, state) {
                        return _buildListOptionWidget(type, newIndex, selectedOptionIndex, context.read<JsonBloc>());
                      },
                    );
                  }))
        ],
      ),
    );
  }

  Widget _buildListOptionWidget(ListType type, int newIndex, int? selectedIndex, JsonBloc bloc) {
    if (type == ListType.radio) {
      return RadioListOption(options[newIndex], newIndex == selectedIndex, () {
        if (onSelection != null) {
          onSelection!(newIndex);
        }
      });
    } else if (type == ListType.checkmark) {
      return CheckmarkListOption(options[newIndex].title, newIndex == selectedIndex, () {
        if (onSelection != null) {
          onSelection!(newIndex);
        }
      });
    } else {
      return const Text('unknown');
    }
  }
}
