package com.zepchat.app.adapters;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.zepchat.app.MainActivity;
import com.zepchat.app.R;
import com.zepchat.core.data.ChatSession;
import com.zepchat.core.data.Message;


public class ChatMessageAdapter extends ArrayAdapter {

    private ChatSession session;

    public void add(Message object) {
        session.add(object);
        super.add(object);
    }

    public ChatMessageAdapter(Context context, int textViewResourceId, ChatSession session) {
        super(context, textViewResourceId);
        this.session = session;
    }

    public int getCount() {
        return session.size();
    }

    public Message getItem(int index) {
        return session.getMessage(index);
    }

    public View getView(int position, View row, ViewGroup parent) {
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.message_list_item, parent, false);
        }
        LinearLayout singleMessageContainer = (LinearLayout) row.findViewById(R.id.singleMessageContainer);

        Message msg = getItem(position);
        TextView chatText = (TextView) row.findViewById(R.id.singleMessage);
        chatText.setText(msg.getMessage());
        if(MainActivity.getLocalUser().equals(msg.getUser())){
            chatText.setBackgroundResource(R.drawable.bubble_green);
            singleMessageContainer.setGravity(Gravity.RIGHT);
        }
        else{
            chatText.setBackgroundResource(R.drawable.bubble_yellow);
            singleMessageContainer.setGravity(Gravity.LEFT);
        }
        return row;
    }

}