import 'package:flutter/cupertino.dart';
import 'package:nut_player_example/src/common/models/option_data.dart';

class OptionsView extends StatelessWidget {
  final String title;
  final List<OptionData> options;
  final int? selectedIndex;
  final Function(int)? newSelection;

  const OptionsView(this.title, this.options, this.selectedIndex, this.newSelection, {super.key});

  @override
  Widget build(BuildContext context) {
    return CupertinoListTile(
      key: key,
      onTap: () { _showAlertDialog(context);},
      title: Row(
        children: [
          Text(title, style: const TextStyle(decoration: TextDecoration.none, fontSize: 17)),
          const SizedBox(width: 30),
          Expanded(
            child: Align(
              alignment: Alignment.centerRight,
              child: Text(options[selectedIndex ?? 0].title,
                style: const TextStyle(
                    decoration: TextDecoration.none,
                    fontSize: 17,
                    color: CupertinoColors.systemGrey,
                    fontWeight: FontWeight.w400,
                ),
                maxLines: 5,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.right,
              ),
            )
          ),
          const Icon(CupertinoIcons.chevron_forward, color: CupertinoColors.systemGrey2)
        ]
      )
    );
  }

  void _showAlertDialog(BuildContext context) {
    showCupertinoModalPopup<void>(
      context: context,
      barrierColor: CupertinoColors.black.withOpacity(0.5),
      builder: (BuildContext context) => CupertinoActionSheet(
        title: Text(title, style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: CupertinoColors.darkBackgroundGray)),
        actions: <Widget>[...options.indexed.map((valueIndex) {
          return Container(
            color: CupertinoColors.white.withOpacity(0.8),
            child: CupertinoActionSheetAction(
              onPressed: () {
                newSelection?.call(valueIndex.$1);
                Navigator.pop(context);
              },
              child: Text(valueIndex.$2.title)
            ),
          );
        }).toList()],
        cancelButton: CupertinoActionSheetAction(
            isDefaultAction: true,
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Отмена'),
          ),
      ),
    );
  }
}
