import 'package:flutter/material.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';

class FTreeNodeWidget extends FStatefulWidget implements FPage {
  final String label;
  final Function()? onNodeTap;
  final bool isSelected;
  final bool isParent;

  FTreeNodeWidget(
    this.label, {
    this.onNodeTap,
    this.isParent = false,
    this.isSelected = false,
  });

  @override
  FState<FStatefulWidget> createState() {
    return FTreeNodeWidgetState();
  }

  @override
  String get pageName => "FTreeNodeWidget";
}

class FTreeNodeWidgetState extends FState<FTreeNodeWidget> {
  bool _isSelected = false;

  @override
  void initState() {
    _isSelected = widget.isSelected;
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      child: FGestureDetector(
        businessParent: widget,
        name: widget.label,
        onTap: () {
          widget.onNodeTap?.call();
          setState(() {
            _isSelected = !_isSelected;
          });
        },
        child: FRow(
          children: [
            if (!widget.isParent) ...[
              FIcon(
                _isSelected
                    ? Icons.radio_button_on
                    : Icons.radio_button_off_outlined,
                color:
                    _isSelected ? FTheme.ins.colorScheme.primary : Colors.grey,
              ),
              const FSizedBox(width: 5),
            ],
            FText(
              widget.label,
            ),
          ],
        ),
      ),
    );
  }
}
