import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:mnemo_cards/theme/themes.dart';
import 'package:mnemo_cards/utils/string_helper.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';

class ItemBuilder {
  static Widget build(Item item) {
    switch (item.type) {
      case ItemType.text:
        return TextItemBuilder(item as TextItem);
      default:
        log('Undefined item type ${item.type} ${item.id}');
        return SizedBox.shrink();
    }
  }
}

extension ItemExt on Item {
  Widget build() => ItemBuilder.build(this);
}

extension ItemIterableExt on Iterable<Item> {
  Iterable<Widget> build() => map((e) => e.build());
}

class TextItemBuilder extends StatelessWidget {
  final TextItem item;

  TextItemBuilder(this.item);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(8.0),
      decoration: BoxDecoration(
        border: item.hasBorder
            ? Border.all(
                color: item.color?.asColor ?? borderGray,
              )
            : null,
      ),
      child: Column(
        children: [
          if (item.title != null)
            Text(
              item.title!,
              style: TextStyle(
                fontWeight: FontWeight.w500,
                fontSize: 24,
                color: Colors.black,
              ),
              textAlign: TextAlign.center,
            ),
          if (item.subtitle != null)
            Text(
              item.subtitle!,
              style: TextStyle(
                fontWeight: FontWeight.w500,
                fontSize: 16,
                color: Colors.black,
              ),
            )
        ],
      ),
    );
  }
}
