import 'package:fis_common/extensions/string.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';

import 'simple_Info.dart';

class FHorizontalUserSimpleCard extends FStatefulWidget {
  final SimpleInfoItem itemInfo;
  final void Function(bool isSelected)? onTap;
  final bool isSelected;
  final double height;
  final EdgeInsetsGeometry margin;
  final double borderRadius;
  final bool isShadowEnabled;
  final Color shadowColor;
  final bool isRedOnly;
  final bool isUserHeadImage;
  final double imageSize;
  FHorizontalUserSimpleCard({
    required this.itemInfo,
    this.onTap,
    this.isSelected = false,
    this.height = 100,
    this.imageSize = 80,
    this.margin = const EdgeInsets.only(top: 5, left: 20, right: 20),
    this.borderRadius = 8,
    this.isRedOnly = false,
    this.isUserHeadImage = true,
    this.isShadowEnabled = false, // 是否启用阴影效果
    this.shadowColor = Colors.black54, // 阴影颜色
  });

  @override
  _HorizontalUserSimpleCardState createState() =>
      _HorizontalUserSimpleCardState();
}

class _HorizontalUserSimpleCardState extends FState<FHorizontalUserSimpleCard> {
  bool _isSelected = false;
  double _rowSpacing = 15;
  @override
  void initState() {
    super.initState();
    _isSelected = widget.isSelected;
  }

  void didUpdateWidget(FHorizontalUserSimpleCard oldWidget) {
    if (_isSelected != widget.isSelected) {
      _isSelected = widget.isSelected;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  FWidget build(BuildContext context) {
    ImageProvider<Object> avatar;
    if (widget.itemInfo.image.isNullOrEmpty) {
      if (widget.isUserHeadImage) {
        avatar = AssetImage("assets/images/avatar_man.jpg");
      } else {
        avatar = AssetImage("assets/images/mock_terminal.png");
      }
    } else {
      avatar = NetworkImage(widget.itemInfo.image!);
    }
    return FInkWell(
      onTap: () {
        setState(() {
          _isSelected = !_isSelected;
        });
        widget.onTap?.call(_isSelected);
      },
      child: FContainer(
        height: widget.height,
        margin: widget.margin,
        child: FCard(
          elevation: widget.isShadowEnabled ? 5.0 : 0.0, // 阴影效果
          shadowColor: widget.shadowColor, // 阴影颜色

          child: FContainer(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.5),
                  spreadRadius: 1,
                  blurRadius: 5,
                  offset: Offset(0, 3),
                ),
              ],
              border: Border.all(
                color: Colors.grey[300]!,
                width: 1,
              ),
            ),
            child: FRow(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                FSizedBox(width: _rowSpacing),
                FContainer(
                  width: widget.imageSize,
                  height: widget.imageSize,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(widget.borderRadius),
                    //shape: widget.shape,
                    image: DecorationImage(
                      image: avatar,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                FSizedBox(width: _rowSpacing),
                FExpanded(
                  child: FColumn(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      FTextTooltip(
                        message: widget.itemInfo.name ?? '',
                        child: FText(
                          widget.itemInfo.name ?? '',
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                            overflow: TextOverflow.ellipsis,
                          ),
                          maxLines: 2,
                        ),
                      ),
                      FText(
                        widget.itemInfo.role ?? '',
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                      FTextTooltip(
                        message: widget.itemInfo.des?.join(' · ') ?? '',
                        child: FText(
                          maxLines: 2, // 最多显示两行
                          widget.itemInfo.des?.join(' · ') ?? '',
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                if (!widget.isRedOnly) ...[
                  FSizedBox(width: _rowSpacing),
                  if (_isSelected) ...[
                    FIcon(
                      Icons.check_circle,
                      color: Colors.green,
                    ),
                  ] else ...[
                    FIcon(
                      Icons.circle_outlined,
                      color: Colors.grey,
                    ),
                  ],
                  FSizedBox(width: _rowSpacing),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }
}
