import 'package:extended_text_library/extended_text_library.dart';
import 'package:flutter/material.dart';

///emoji/image text
class EmojiText extends SpecialText {
  static const String flag = "[";
  final int start;
  final double emojiSize;
  EmojiText(TextStyle textStyle, {this.start, this.emojiSize=20.0})
      : super(EmojiText.flag, "]", textStyle);

  @override
  InlineSpan finishText() {
    var key = toString();

    if (EmojiUitl.instance.emojiMap.containsKey(key)) {

      //fontsize id define image height
      //size = 30.0/26.0 * fontSize
      final double size = emojiSize;
     // ScreenUtil().setWidth(emojiSize);
      ///fontSize 26 and text height =30.0
      //final double fontSize = 26.0;
      return ImageSpan(AssetImage(EmojiUitl.instance.emojiMap[key]),
          actualText: key,
          imageWidth: size,
          imageHeight: size,
          start: start,
          fit: BoxFit.contain,
          margin: EdgeInsets.only(left: 2.0, top: 2.0, right: 2.0));
    }

    return TextSpan(text: toString(), style: textStyle);
  }
}

class EmojiUitl { ///emoji
  final Map<String, String> _emojiMap = new Map<String, String>();

  Map<String, String> get emojiMap => _emojiMap;

  final String _emojiFilePath = "assets/images/emojiface";

  static EmojiUitl _instance;
  static EmojiUitl get instance {
    if (_instance == null) _instance = new EmojiUitl._();
    return _instance;
  }

  EmojiUitl._() {
    for (int i = 1; i < 87; i++) {

      _emojiMap[EmojiText.flag+"$i]"] = "$_emojiFilePath/$i.png";
    }

  }
}
