part of 'home_page.dart';

typedef OnLikeCallback = void Function(String id, String title, bool isLiked)?;

class _Card extends StatelessWidget {
  final String _text;
  final String? description;
  List<String>? descriptionByLines;
  List<Widget>? descriptionWidgets;
  final String? imageUrl;
  final OnLikeCallback onLike;
  final VoidCallback? onTap;
  final String id;
  final bool isLiked;

  _Card(this._text,  this.id, {Key? key, this.imageUrl, this.description, this.onLike, this.onTap,

    this.isLiked = false,})
      : super(key: key){
    descriptionByLines = (description ?? '').split('\n');
  }

  factory _Card.fromData(
    CardData data, {
    OnLikeCallback onLike,
    VoidCallback? onTap,
        bool isLiked = false,
  }) =>
      _Card(
        data.text,data.id,
        imageUrl: data.imageUrl,
        description: data.description,
        onLike: onLike,
        onTap: onTap,
        isLiked: isLiked,
      );

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.all(10),
        constraints: const BoxConstraints(minHeight: 140, maxHeight: 255),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(20),
            color: Colors.amber,
            border: Border.all(color: Colors.amberAccent, width: 2),
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(.5),
                blurRadius: 8,
                offset: const Offset(0, 5),
                spreadRadius: 4,
              )
            ]),
        child: IntrinsicHeight(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ClipRRect(
                borderRadius:
                    const BorderRadius.horizontal(left: Radius.circular(20)),
                child: SizedBox(
                  height: double.infinity,
                  width: 120,
                  child: Image.network(
                    imageUrl ?? '',
                    fit: BoxFit.cover,
                    errorBuilder: (_, __, ___) => const Placeholder(),
                  ),
                ),
              ),
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.only(left: 8.0, bottom: 8),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        _text,
                        style: Theme.of(context).textTheme.headlineMedium,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 2,
                      ),
                      ...(descriptionByLines ?? ['']).map((e) => Text(
                          e,
                          style: Theme.of(context).textTheme.bodyLarge,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: GestureDetector(
                  onTap: () => onLike?.call(id, _text, isLiked),
                  child: AnimatedSwitcher(
                    duration: const Duration(milliseconds: 100),
                    child: isLiked
                        ? const Icon(
                            Icons.favorite,
                            key: ValueKey(0),
                          )
                        : const Icon(
                            Icons.favorite_border,
                            key: ValueKey(1),
                          ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
