import 'package:flutter/material.dart';
import 'package:vitalapp/consts/styles.dart';

class VListFormCellGroup extends StatelessWidget {
  final List<Widget> children;
  final double? leadingIconWidth;
  final String? formTitle;

  const VListFormCellGroup({
    Key? key,
    required this.children,
    this.leadingIconWidth,
    this.formTitle,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final divider = Divider(
      thickness: 1,
      color: Colors.grey.shade400,
      indent: leadingIconWidth,
    );
    final kids = <Widget>[_buildVListFromCellGroupTitle()];

    for (var i = 0; i < children.length; i++) {
      if (i > 0) {
        kids.add(divider);
      }
      kids.add(children[i]);
    }
    return Container(
      alignment: Alignment.center,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: GlobalStyles.borderRadius,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: kids,
      ),
    );
  }

  Widget _buildVListFromCellGroupTitle() {
    if (formTitle == null) {
      return const SizedBox();
    } else {
      return Container(
        padding: const EdgeInsets.only(bottom: 8),
        child: Text(
          formTitle!,
          textAlign: TextAlign.left,
          style: const TextStyle(
            fontSize: 30,
            fontWeight: FontWeight.bold,
          ),
        ),
      );
    }
  }
}

class VListFormCell extends StatelessWidget {
  final String? label;
  final Widget? labelWidget;
  final double? labelWidth;
  final String? content;
  final Widget? contentWidget;
  final VoidCallback? onTap;
  final double? height;
  final double? leadingIconWidth;
  final Widget? leadingIcon;

  const VListFormCell({
    super.key,
    this.label,
    this.labelWidget,
    this.labelWidth,
    this.content,
    this.contentWidget,
    this.onTap,
    this.height,
    this.leadingIcon,
    this.leadingIconWidth,
  }) : assert(label != null || labelWidget != null);

  @override
  Widget build(BuildContext context) {
    final h = height ?? 48;
    final children = <Widget>[];
    if (leadingIcon != null) {
      final leadingWidget = SizedBox(
        width: leadingIconWidth ?? h * .6,
        child: leadingIcon,
      );
      children.add(leadingWidget);
    }
    // if (isRequired ?? false) {
    //   children.add(const Text(
    //     '*',
    //     style: TextStyle(
    //       color: Colors.red,
    //       fontSize: 20,
    //     ),
    //   ));
    // }
    children.add(_buildLabel());
    children.add(Expanded(child: _buildRightPart()));

    return SizedBox(
      height: h,
      child: InkWell(
        onTap: onTap,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: children,
        ),
      ),
    );
  }

  Widget _buildRightPart() {
    final children = <Widget>[];

    final contentChild = _buildContent();
    if (contentChild != null) {
      children.add(contentChild);
    }

    if (onTap != null) {
      children.add(
        SizedBox(
          height: height ?? 40,
          child: _buildAction(),
        ),
      );
    } else {
      children.add(const SizedBox(width: 12));
    }

    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      mainAxisSize: MainAxisSize.max,
      children: children,
    );
  }

  Widget _buildLabel() {
    Widget widget;
    if (labelWidget != null) {
      widget = labelWidget!;
    } else {
      widget = Text(
        label!,
        style: const TextStyle(color: Colors.black, fontSize: 20),
      );
    }
    return SizedBox(width: labelWidth ?? 280, child: widget);
  }

  Widget? _buildContent() {
    if (contentWidget != null) {
      return contentWidget!;
    }
    if (content != null && content!.isNotEmpty) {
      return Expanded(
        child: Text(
          content!,
          textAlign: TextAlign.end,
          style: TextStyle(
            color: Colors.grey.shade700,
            fontSize: 20,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      );
    }
    return null;
  }

  Widget _buildAction() {
    return Icon(
      Icons.keyboard_arrow_right,
      size: 32,
      color: Colors.grey.shade400,
    );
  }
}
