package org.literacyapp.chat;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import org.literacyapp.chat.dao.TextMessageDao;
import org.literacyapp.chat.model.TextMessage;
import org.literacyapp.chat.util.DeviceInfoHelper;

import java.util.Calendar;
import java.util.List;

public class ChatActivity extends Activity {

    private TextMessageDao textMessageDao;

    private List<TextMessage> textMessages;

    private ArrayAdapter arrayAdapter;

    private ListView mListPreviousMessages;

    private EditText mTextMessage;

    private ImageButton mButtonSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(getClass().getName(), "onCreate");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_chat);

        textMessageDao = ((ChatApplication) getApplication()).getDaoSession().getTextMessageDao();

        mListPreviousMessages = (ListView) findViewById(R.id.listPreviousMessages);
        mTextMessage = (EditText) findViewById(R.id.textMessage);
        mButtonSend = (ImageButton) findViewById(R.id.buttonSend);
    }

    @Override
    protected void onStart() {
        Log.i(getClass().getName(), "onStart");
        super.onStart();

        // Load messages sent within the last 24 hours
        Calendar calendar24HoursAgo = Calendar.getInstance();
        calendar24HoursAgo.add(Calendar.HOUR_OF_DAY, -24);
        textMessages = textMessageDao.queryBuilder()
                .where(TextMessageDao.Properties.TimeSent.gt(calendar24HoursAgo.getTimeInMillis()))
                .list();
        Log.i(getClass().getName(), "textMessages.size(): " + textMessages.size());

        arrayAdapter = new MessageListArrayAdapter(getApplicationContext(), textMessages);
        mListPreviousMessages.setAdapter(arrayAdapter);

        mButtonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(getClass().getName(), "mButtonSend onClick");

                String text = mTextMessage.getText().toString();
                Log.i(getClass().getName(), "text: " + text);

                TextMessage textMessage = new TextMessage();
                textMessage.setDeviceId(DeviceInfoHelper.getDeviceId(getApplicationContext()));
                textMessage.setTimeSent(Calendar.getInstance());
                textMessage.setText(text);

                // Store in database
                textMessageDao.insert(textMessage);

                // Add to UI
                addToMessageListAndRefresh(textMessage);

                // Reset input field
                mTextMessage.setText("");
            }
        });
    }

    private void addToMessageListAndRefresh(TextMessage textMessage) {
        textMessages.add(textMessage);
        refreshMessageList();
    }

    private void refreshMessageList() {
        Log.i(getClass().getName(), "refreshMessageList");

        arrayAdapter.notifyDataSetChanged();
        mListPreviousMessages.smoothScrollToPosition(mListPreviousMessages.getCount());
        // Fix problem with scrolling when keyboard is present
        mListPreviousMessages.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i(getClass().getName(), "mListPreviousMessages.postDelayed");
                mListPreviousMessages.setSelection(mListPreviousMessages.getCount());
            }
        }, 100);
    }
}

@@ -264,16 +264,45 @@
 istView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -1318,16 +1318,17 @@
 nSend);%0A
+%0A
     %7D%0A%0A 
@@ -2359,25 +2359,158 @@
  %22 + text);%0A
-%0A
+                System.out.println(text);%0A%0A                // Check if the EditText is empty%0A                if(!text.isEmpty())%7B%0A    
             
@@ -2554,24 +2554,28 @@
 tMessage();%0A
+    
             
@@ -2666,32 +2666,36 @@
                 
+ 
+   
 textMessage.setT
@@ -2723,24 +2723,28 @@
 nstance());%0A
+    
             
@@ -2791,16 +2791,20 @@
         
+    
 // Store
@@ -2824,32 +2824,36 @@
                 
+   
+ 
 textMessageDao.i
@@ -2889,16 +2889,20 @@
         
+    
 // Add t
@@ -2922,16 +2922,20 @@
         
+  
+  
 addToMes
@@ -2960,32 +2960,36 @@
 (textMessage);%0A%0A
+    
                 
@@ -3017,32 +3017,36 @@
                 
+    
 mTextMessage.set
@@ -3059,32 +3059,303 @@
 %22);%0A
-            %7D%0A        %7D)
+%0A                %7D else %7B%0A                    mButtonSend.setVisibility(View.GONE);%0A                %7D%0A                mButtonSend.setVisibility(View.VISIBLE);%0A%0A            %7D%0A        %7D);%0A    %7D%0A%0A%0A    public static boolean checkText(String text)%7B%0A%0A        if(text.equals(%22%22));%0A%0A        return false
 ;%0A  

Disable button when the EditText is empty.