import 'package:fis_ui/index.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/pages/lab_export/widgets/classify_data_multiple_selector/classify_multiple_list_item.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/pages/lab_export/controller.dart';

class ClassifyMultipleList<T> extends StatefulWidget {
  const ClassifyMultipleList({
    super.key,
    required this.title,
    required this.projects,
    required this.selectedProjectIndex,
    required this.onProjectClick,
    required this.multipleCheckList,
    required this.onMultipleItemClick,
  });
  final String title;
  final List<T> projects;
  final int selectedProjectIndex;
  final IndexCallback onProjectClick;
  final Function(int index) onMultipleItemClick;
  final List<MultipleCheckModel> multipleCheckList;

  @override
  State<ClassifyMultipleList> createState() => _ClassifyMultipleListState();
}

class _ClassifyMultipleListState extends State<ClassifyMultipleList> {
  bool _isExpanded = true;
  bool _isTitleHover = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      children: <Widget>[
        _buildProjectListTitle(),
        if (_isExpanded)
          ...widget.projects.asMap().entries.map(
            (e) {
              return _buildProjectListItem(e.key);
            },
          ).toList(),
      ],
    );
  }

  Widget _buildProjectListTitle() {
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      onEnter: _handleMouseEnter,
      onExit: _handleMouseExit,
      child: GestureDetector(
        onTap: () {
          setState(() {
            _isExpanded = !_isExpanded;
          });
        },
        child: Container(
          height: 40,
          decoration: BoxDecoration(
            color: _isTitleHover ? LabColors.base300 : Colors.transparent,
            borderRadius: BorderRadius.circular(5),
          ),
          padding: const EdgeInsets.symmetric(horizontal: 10),
          // color: const Color.fromARGB(90, 59, 255, 59),
          child: FRow(
            children: [
              FCenter(
                child: FIcon(
                  _isExpanded
                      ? Icons.keyboard_arrow_down
                      : Icons.keyboard_arrow_right,
                  size: 20,
                  color: LabColors.base700,
                ),
              ),
              const FSizedBox(width: 10),
              FCenter(
                child: FText(
                  widget.title,
                  style: TextStyle(
                    fontWeight: FontWeight.w900,
                    color: LabColors.text700,
                  ),
                ),
              ),
              FExpanded(
                child: FContainer(),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildProjectListItem(int index) {
    if (index >= widget.projects.length) return Container();
    return ClassifyMultipleListItem(
      project: (widget.projects[index] as PatientModel),
      projectName: widget.projects[index].name,
      isSelected: index == widget.selectedProjectIndex,
      multipleCheckList: widget.multipleCheckList,
      id: (widget.projects[index] as PatientModel).code,
      onClick: () {
        widget.onProjectClick(index);
      },
      onMultipleItemClick: (selected) {
        widget.onMultipleItemClick(index);
      },
      index: index,
    );
  }

  void _handleMouseEnter(PointerEvent details) {
    setState(() {
      _isTitleHover = true;
    });
  }

  void _handleMouseExit(PointerEvent details) {
    setState(() {
      _isTitleHover = false;
    });
  }
}
