package hr.fer.labust.diverapp.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import hr.fer.labust.diverapp.common.ChatMessage;
import hr.fer.labust.diverapp.common.Colors;
import hr.fer.labust.diverapp.R;

public class ChatListAdapter extends BaseAdapter {
    private final ArrayList<ChatMessage> mMessagesList;
    private final HashMap<String, Integer> mNameColors;
    private final Context mContext;

    public ChatListAdapter(Context context, ArrayList<ChatMessage> messageList) {
        mMessagesList = messageList;
        mNameColors = new HashMap<>();
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ChatMessage msg = mMessagesList.get(position);
        TextView mMessageView;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_element_chat, parent, false);
            mMessageView = (TextView)convertView.findViewById(R.id.chat_listview_element);
            convertView.setTag(new ViewHolder(mMessageView));
        }
        else {
            ViewHolder viewHolder = (ViewHolder)convertView.getTag();
            mMessageView = viewHolder.MessageView;
        }

        if (!mNameColors.containsKey(msg.Name)) {
            mNameColors.put(msg.Name, Colors.getColor2());
        }

        int nameLength = msg.Name.length() + 1;
        final SpannableStringBuilder span = new SpannableStringBuilder(msg.Name + ": " + msg.Text);
        span.setSpan(new StyleSpan(Typeface.BOLD), 0, nameLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        span.setSpan(new ForegroundColorSpan(mNameColors.get(msg.Name)), 0, nameLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        mMessageView.setText(span);
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return mMessagesList.get(position);
    }

    @Override
    public int getCount() {
        return mMessagesList.size();
    }

    private void add(String name, String message) {
        if (!mNameColors.containsKey(name)) {
            mNameColors.put(name, Colors.getColor2());
        }

        mMessagesList.add(new ChatMessage(name, message));
    }

    public void nameChanged(String oldName, String newName) {
        if (mNameColors.containsKey(oldName)) {
            mNameColors.put(newName, mNameColors.get(oldName));
        }
    }

    private class ViewHolder {
        public TextView MessageView;

        ViewHolder(TextView messageView) {
            MessageView = messageView;
        }
    }
}
