package beyondsoft.bewoklientenapp;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by bib on 20.05.2016.
 */
public class MessageAdapter extends ArrayAdapter<ChatMessageFormat> {

    private Activity activity;
    private List<ChatMessageFormat> messages;



    public MessageAdapter(Activity context, int resource, List<ChatMessageFormat> objects) {
        super(context, resource, objects);
        this.activity = context;
        this.messages = objects;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        int layoutResource = 0; // determined by view type
        ChatMessageFormat chatMessage = getItem(position);
        int viewType = getItemViewType(position);



        //Splitt message um zu wissen ob from server oder nicht
        String line = chatMessage.getMessage();

        String[] splitt = line.split(":");

        //Server nachricht kenntlich machen
        if (chatMessage.getIsME()) {
            layoutResource = R.layout.item_chat_left;
        } else if(splitt[0].equals("Server")) {
            layoutResource = R.layout.item_chat_sever_right;
        }else {
            layoutResource = R.layout.item_chat_right;
        }

/* Original
        if (chatMessage.getIsME()) {
            layoutResource = R.layout.item_chat_left;
        } else {
            layoutResource = R.layout.item_chat_right;
        }
*/
       /* if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
            Log.v("Status","Bin ich hier ?? ");
        } else {*/
            convertView = inflater.inflate(layoutResource, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        //}

        //set message content
        holder.msg.setText(chatMessage.getMessage());

        if(chatMessage.getIsME()){
            holder.dateOut.setText(chatMessage.getDatum());
        }else {
            holder.dateIn.setText(chatMessage.getDatum());
        }

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        // return the total number of view types. this value should never change
        // at runtime
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        // return a value between 0 and (getViewTypeCount - 1)
        return position % 2;
    }

    private class ViewHolder {
        private TextView msg;
        private TextView dateOut;
        private TextView dateIn;

        public ViewHolder(View v) {
            msg = (TextView) v.findViewById(R.id.txt_msg);
            dateOut = (TextView)v.findViewById(R.id.DateOUT);
            dateIn = (TextView)v.findViewById(R.id.DateIN);
        }
    }


}
