import 'package:chat/generated/i18n.dart';
import 'package:chat/data/constants.dart';
import 'package:extended_text_library/extended_text_library.dart';
import 'package:flutter/material.dart';

///emoji/image text
class EmojiGifText extends SpecialText {
  static const String flag = "<*";
  final int start;
  final isShowGif;
  EmojiGifText(TextStyle textStyle, {this.start,this.isShowGif=true})
      : super(EmojiGifText.flag, ">", textStyle);


  @override
  InlineSpan finishText() {
    var key = toString();
    if (EmojiGifUitl.instance.emojiMap.containsKey(key)) {
      //fontsize id define image height
      //size = 30.0/26.0 * fontSize
      final double size = 118.0;

      ///fontSize 26 and text height =30.0
      //final double fontSize = 26.0;
      return isShowGif?ImageSpan(AssetImage(EmojiGifUitl.instance.emojiMap[key]),
          actualText: key,
          imageWidth: size,
          imageHeight: size,
          start: start,
          fit: BoxFit.fill,
          margin: EdgeInsets.only(left: 2.0, top: 2.0, right: 2.0)):SpecialTextSpan(text:'[${I18n.of(Constants.getCurrentContext()).emoji}]', actualText: '' );

//    return Image.asset(EmojiGifUitl.instance.emojiMap[key]);

    }

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

class EmojiGifUitl {
  final Map<String, String> _emojiMap = new Map<String, String>();

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

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

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

  EmojiGifUitl._() {
    for (int i = 1; i < 31; i++) {
      _emojiMap[EmojiGifText.flag+"_$i>"] = "$_emojiFilePath/sticker_$i.gif";
    }
  }
}
