package chat.rocket.android.widget.message.autocomplete.user;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.widget.AbsoluteUrl;
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.RocketChatAvatar;
import chat.rocket.android.widget.message.autocomplete.AutocompleteViewHolder;
import com.amulyakhare.textdrawable.TextDrawable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class UserViewHolder extends AutocompleteViewHolder<UserItem> {

  private final TextView titleTextView;
  private final RocketChatAvatar avatar;
  private final ImageView status;

  private static final int[] COLORS = new int[] {
      0xFFF44336, 0xFFE91E63, 0xFF9C27B0, 0xFF673AB7, 0xFF3F51B5, 0xFF2196F3,
      0xFF03A9F4, 0xFF00BCD4, 0xFF009688, 0xFF4CAF50, 0xFF8BC34A, 0xFFCDDC39,
      0xFFFFC107, 0xFFFF9800, 0xFFFF5722, 0xFF795548, 0xFF9E9E9E, 0xFF607D8B
  };

  public UserViewHolder(View itemView, final AutocompleteViewHolder.OnClickListener<UserItem> onClickListener) {
    super(itemView);

    titleTextView = itemView.findViewById(R.id.title);
    avatar = itemView.findViewById(R.id.avatar);
    status = itemView.findViewById(R.id.status);

    itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (onClickListener != null) {
          onClickListener.onClick((UserItem) v.getTag());
        }
      }
    });
  }

  @Override
  public void bind(UserItem userItem) {
    itemView.setTag(userItem);

    final String suggestion = userItem.getSuggestion();

    if (titleTextView != null) {
      titleTextView.setText(suggestion);
    }

    if (avatar != null) {
      avatar.loadImage(getImageUrl(suggestion, userItem.getAbsoluteUrl()), getTextDrawable(itemView.getContext(), suggestion));
    }

    if (status != null) {
      status.setImageResource(userItem.getStatusResId());
    }
  }

  @Override
  public void showAsEmpty() {
    status.setVisibility(View.GONE);
    avatar.setVisibility(View.GONE);
    titleTextView.setText(R.string.no_user_found);
  }

  private String getImageUrl(String username, AbsoluteUrl absoluteUrl) {
    //from Rocket.Chat:packages/rocketchat-ui/lib/avatar.coffee
    //REMARK! this is often SVG image! (see: Rocket.Chat:server/startup/avatar.coffee)
    try {
      final String avatarUrl = "/avatar/" + URLEncoder.encode(username, "UTF-8");
      //  TODO why absoluteUrl is nullable? By allowing that, the app tries to load non-existing images
      if (absoluteUrl == null) {
        return avatarUrl;
      }
      return absoluteUrl.from(avatarUrl);
    } catch (UnsupportedEncodingException exception) {
      return null;
    }
  }

  private Drawable getTextDrawable(Context context, String username) {
    int round = (int) (4 * context.getResources().getDisplayMetrics().density);

    return TextDrawable.builder()
        .beginConfig()
        .useFont(Typeface.SANS_SERIF)
        .endConfig()
        .buildRoundRect(getUsernameInitials(username), getUserAvatarBackgroundColor(username), round);
  }

  private String getUsernameInitials(String username) {
    if (username.isEmpty()) {
      return "?";
    }

    String[] splitUsername = username.split(".");
    if (splitUsername.length > 1) {
      return (splitUsername[0].substring(0, 1) + splitUsername[splitUsername.length - 1].substring(0, 1)).toUpperCase();
    } else {
      if (username.length() > 1) {
        return username.substring(0, 2).toUpperCase();
      } else {
        return username.substring(0, 1).toUpperCase();
      }
    }
  }

  private int getUserAvatarBackgroundColor(String username) {
    return COLORS[username.length() % COLORS.length];
  }
}
@@ -60,74 +60,8 @@
 r;%0A%0A
-import android.content.Context;%0Aimport android.graphics.Typeface;%0A
 impo
@@ -326,16 +326,75 @@
 Avatar;%0A
+import chat.rocket.android.widget.helper.UserAvatarHelper;%0A
 import c
@@ -468,58 +468,8 @@
 er;%0A
-import com.amulyakhare.textdrawable.TextDrawable;%0A
 impo
@@ -609,17 +609,16 @@
 Item%3E %7B%0A
-%0A
   privat
@@ -729,297 +729,8 @@
 s;%0A%0A
-  private static final int%5B%5D COLORS = new int%5B%5D %7B%0A      0xFFF44336, 0xFFE91E63, 0xFF9C27B0, 0xFF673AB7, 0xFF3F51B5, 0xFF2196F3,%0A      0xFF03A9F4, 0xFF00BCD4, 0xFF009688, 0xFF4CAF50, 0xFF8BC34A, 0xFFCDDC39,%0A      0xFFFFC107, 0xFFFF9800, 0xFFFF5722, 0xFF795548, 0xFF9E9E9E, 0xFF607D8B%0A  %7D;%0A%0A
   pu
@@ -1513,49 +1513,70 @@
     
-avatar.loadImage(getImageUrl(suggestion, 
+String absoluteUri = UserAvatarHelper.INSTANCE.getAbsoluteUri(
 user
@@ -1596,19 +1596,94 @@
 uteUrl()
-)
 , 
+suggestion);%0A      Drawable placeholderDrawable = UserAvatarHelper.INSTANCE.
 getTextD
@@ -1690,16 +1690,28 @@
 rawable(
+suggestion, 
 itemView
@@ -1723,29 +1723,74 @@
 ontext()
-, suggestion)
+);%0A      avatar.loadImage(absoluteUri, placeholderDrawable
 );%0A    %7D
@@ -2060,1627 +2060,6 @@
 %0A  %7D
-%0A%0A  private String getImageUrl(String username, AbsoluteUrl absoluteUrl) %7B%0A    //from Rocket.Chat:packages/rocketchat-ui/lib/avatar.coffee%0A    //REMARK! this is often SVG image! (see: Rocket.Chat:server/startup/avatar.coffee)%0A    try %7B%0A      final String avatarUrl = %22/avatar/%22 + URLEncoder.encode(username, %22UTF-8%22);%0A      //  TODO why absoluteUrl is nullable? By allowing that, the app tries to load non-existing images%0A      if (absoluteUrl == null) %7B%0A        return avatarUrl;%0A      %7D%0A      return absoluteUrl.from(avatarUrl);%0A    %7D catch (UnsupportedEncodingException exception) %7B%0A      return null;%0A    %7D%0A  %7D%0A%0A  private Drawable getTextDrawable(Context context, String username) %7B%0A    int round = (int) (4 * context.getResources().getDisplayMetrics().density);%0A%0A    return TextDrawable.builder()%0A        .beginConfig()%0A        .useFont(Typeface.SANS_SERIF)%0A        .endConfig()%0A        .buildRoundRect(getUsernameInitials(username), getUserAvatarBackgroundColor(username), round);%0A  %7D%0A%0A  private String getUsernameInitials(String username) %7B%0A    if (username.isEmpty()) %7B%0A      return %22?%22;%0A    %7D%0A%0A    String%5B%5D splitUsername = username.split(%22.%22);%0A    if (splitUsername.length %3E 1) %7B%0A      return (splitUsername%5B0%5D.substring(0, 1) + splitUsername%5BsplitUsername.length - 1%5D.substring(0, 1)).toUpperCase();%0A    %7D else %7B%0A      if (username.length() %3E 1) %7B%0A        return username.substring(0, 2).toUpperCase();%0A      %7D else %7B%0A        return username.substring(0, 1).toUpperCase();%0A      %7D%0A    %7D%0A  %7D%0A%0A  private int getUserAvatarBackgroundColor(String username) %7B%0A    return COLORS%5Busername.length() %25 COLORS.length%5D;%0A  %7D
 %0A%7D

Update UserViewHolder class.