import 'package:fis_ui/index.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/classify_data_selector/classify_list_item.dart';
import 'package:flutter/material.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';

class ClassifyList<T> extends StatefulWidget {
  const ClassifyList({
    super.key,
    required this.title,
    required this.projects,
    required this.selectedProjectIndex,
    required this.onProjectClick,
    this.actionButton,
    this.itemActionButton,
  });
  final String title;
  final List<T> projects;
  final int selectedProjectIndex;
  final IndexCallback onProjectClick;
  final Widget Function()? actionButton;
  final Widget Function()? itemActionButton;

  @override
  State<ClassifyList> createState() => _ClassifyListState();
}

class _ClassifyListState extends State<ClassifyList> {
  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
                : LabColors.specialHighlightColor,
            borderRadius: BorderRadius.circular(5),
          ),
          padding: const EdgeInsets.symmetric(horizontal: 10),
          // color: const Color.fromARGB(90, 59, 255, 59),
          child: Row(
            children: [
              Center(
                child: Icon(
                  _isExpanded
                      ? Icons.keyboard_arrow_down
                      : Icons.keyboard_arrow_right,
                  size: 20,
                  color: LabColors.base700,
                ),
              ),
              const FSizedBox(width: 10),
              Center(
                child: FText(
                  widget.title,
                  style: TextStyle(
                    fontWeight: FontWeight.w900,
                    color: LabColors.text700,
                  ),
                ),
              ),
              Expanded(
                child: Container(
                    child: widget.actionButton != null
                        ? Align(
                            alignment: Alignment.centerRight,
                            child: widget.actionButton!(),
                          )
                        : Container()),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildProjectListItem(int index) {
    if (index >= widget.projects.length) return Container();
    String projectName = '';
    bool isExistUrmData = false;
    var pro = widget.projects[index];
    if (pro is TaskBaseDTO) {
      projectName = pro.taskName ?? '';
    } else if (pro is PatientModel) {
      projectName = widget.projects[index].name;
      isExistUrmData = pro.isExistUrmData;
    } else {
      projectName = widget.projects[index].name;
    }
    return ClassifyListItem(
      projectName: projectName,
      isSelected: index == widget.selectedProjectIndex,
      isExistUrmData: isExistUrmData,
      onClick: () {
        widget.onProjectClick(index);
      },
      itemActionButton: widget.itemActionButton,
    );
  }

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

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