package fishrungames.kobserver;


import fishrungames.kobserver.JniWrapper;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;

import android.view.View;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


public class MessagesActivity extends Activity
{
	
	ArrayAdapter<String> mAdapter;
	
	Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle icicle)
	{
		JniWrapper.messagesActivity = this;

		super.onCreate(icicle);

		setContentView(R.layout.messages_layout);	
		
		Button backbtn = (Button) findViewById(R.id.backButton_messages);
		backbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), MainMenuActivity.class);
                startActivityForResult(myIntent, 0);
                JniWrapper.messagesActivity = null;
            }
        });
		
		Button sendmessagebtn = (Button) findViewById(R.id.sendMessageButton);
		sendmessagebtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), SendMessageActivity.class);
                startActivityForResult(myIntent, 0);
                JniWrapper.messagesActivity = null;
            }
        });
		
		
		EditText editView = (EditText) findViewById(R.id.currentMessageEdit);
		
		editView.setKeyListener(null);

	
		mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, JniWrapper.messages_headers);
	
		ListView messageListView = (ListView)findViewById(R.id.messageListView2);
		
		messageListView.setAdapter(mAdapter);
		
		messageListView.setOnItemClickListener(new ListView.OnItemClickListener() {
	        @Override
	        public void onItemClick(AdapterView<?> a, View v, int i, long l)
	        {
	        	EditText editView = (EditText) findViewById(R.id.currentMessageEdit);
	    		
	        	editView.setText(JniWrapper.messages_texts.get(i));
	        }
	    });

	}
	
	public void RefreshMessageList()
	{
		//mAdapter.notifyDataSetChanged();
		
		Runnable TheDelegatedTimerTask= new Runnable() {
		    @Override public void run() { // function called by main thread when posted message is dequeued
		    	
		    	mAdapter.notifyDataSetChanged();
				
		    }
		};
		
		mHandler.post(TheDelegatedTimerTask);
	}
}