import 'package:flutter/material.dart';

import '../player_panel_shell.dart' show panelDivider;
import 'selection_panel_entry.dart';


class PanelSelectionList<T> extends StatelessWidget {
  final List<SelectionPanelEntry<T>> entries;
  final ValueChanged<T> onSelected;

  const PanelSelectionList({
    super.key,
    required this.entries,
    required this.onSelected,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          for (final entry in entries)
            if (entry.isDivider) panelDivider() else _itemTile(entry),
        ],
      ),
    );
  }

  Widget _itemTile(SelectionPanelEntry<T> entry) {
    return GestureDetector(
      onTap: () {
        if (entry.value != null) onSelected(entry.value as T);
      },
      behavior: HitTestBehavior.opaque,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 8),
        child: Row(
          children: [
            SizedBox(
              width: 24,
              child: entry.isActive
                  ? const Icon(Icons.check, size: 16, color: Color(0xFF2196F3))
                  : null,
            ),
            const SizedBox(width: 8),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    entry.label!,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: entry.isActive
                          ? const Color(0xFF2196F3)
                          : Colors.white,
                      fontWeight: entry.isActive
                          ? FontWeight.w600
                          : FontWeight.normal,
                      fontSize: 13,
                    ),
                  ),
                  if (entry.subtitle != null && entry.subtitle != entry.label)
                    Text(
                      entry.subtitle!,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(
                        color: Colors.white38,
                        fontSize: 11,
                      ),
                    ),
                ],
              ),
            ),
            if (entry.badge != null) ...[
              const SizedBox(width: 8),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1),
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Text(
                  entry.badge!,
                  style: const TextStyle(fontSize: 10, color: Colors.grey),
                ),
              ),
            ],
            if (entry.isLoading) ...[
              const SizedBox(width: 8),
              const SizedBox.square(
                dimension: 14,
                child: CircularProgressIndicator(
                  strokeWidth: 1.5,
                  color: Color(0xFF2196F3),
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }
}
