import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';

class CustomExpansionTileSpaceTree extends StatelessWidget {
  final String? spaceId;
  final String title;
  final List<Widget>? children;
  final bool isSelected;
  final bool isSoldCheck;
  final bool isExpanded;
  final void Function()? onExpansionChanged;
  final void Function()? onItemSelected;

  const CustomExpansionTileSpaceTree({
    required this.isSelected,
    required this.title,
    this.spaceId,
    this.children,
    this.onExpansionChanged,
    this.onItemSelected,
    this.isExpanded = false,
    this.isSoldCheck = false,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Checkbox(
              value: isSoldCheck ? null : isSelected,
              onChanged: (value) => onItemSelected?.call(),
              tristate: true,
              side: WidgetStateBorderSide.resolveWith(
                (states) => const BorderSide(color: ColorsManager.grayBorder),
              ),
              fillColor: WidgetStateProperty.resolveWith((states) {
                if (states.contains(WidgetState.selected)) {
                  return ColorsManager.blue1;
                }

                return ColorsManager.checkBoxFillColor;
              }),
              checkColor: ColorsManager.whiteColors,
            ),
            _buildExpansionIcon(),
            Expanded(
              child: GestureDetector(
                onTap: onItemSelected,
                child: Text(
                  _capitalizeFirstLetter(title),
                  style: Theme.of(context).textTheme.bodySmall!.copyWith(
                        color: isSelected
                            ? ColorsManager.blackColor
                            : ColorsManager.lightGrayColor,
                        fontWeight: FontWeight.w400,
                      ),
                ),
              ),
            ),
          ],
        ),
        if (isExpanded && children != null && children!.isNotEmpty)
          Padding(
            padding: const EdgeInsets.only(left: 24.0),
            child: Column(
              children: children ?? [],
            ),
          ),
      ],
    );
  }

  Widget _buildExpansionIcon() {
    return Visibility(
      visible: children != null && children!.isNotEmpty,
      child: InkWell(
        onTap: onExpansionChanged,
        child: Icon(
          isExpanded ? Icons.keyboard_arrow_down : Icons.keyboard_arrow_right,
          color: ColorsManager.lightGrayColor,
          size: 16.0,
        ),
      ),
    );
  }

  String _capitalizeFirstLetter(String text) {
    if (text.isEmpty) return text;
    return text[0].toUpperCase() + text.substring(1);
  }
}
