package com.zepchat.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.zepchat.app.adapters.ChatMessageAdapter;
import com.zepchat.core.transport.ZEPMessagingTransport;
import com.zepchat.core.data.ChatSession;
import com.zepchat.core.data.Message;
import com.zepchat.core.data.User;

public class MessagingActivity extends Activity implements ZEPMessagingTransport.MessageListener{
    private ChatMessageAdapter chatArrayAdapter;
    private EditText chatText;
    private User remoteUser;
    private ChatSession session;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Chat");
        Intent i = getIntent();
        setContentView(R.layout.activity_messaging);

        remoteUser = MainActivity.getCore().getRemoteUser(
                getIntent().getStringExtra("PublicKey"));
        session = MainActivity.getCore().getSession(remoteUser);

        final ListView listView = (ListView) findViewById(R.id.messageList);
        chatArrayAdapter = new ChatMessageAdapter(getApplicationContext(), R.layout.message_list_item, session);
        listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        listView.setAdapter(chatArrayAdapter);

        chatText = (EditText) findViewById(R.id.newMessage);
        chatText.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    return sendChatMessage();
                }
                return false;
            }
        });

        Button sendButton = (Button) findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                sendChatMessage();
            }
        });


        MainActivity.getCore().setMessageListener(remoteUser, this);
        session.resetUnread();
    }

    private boolean sendChatMessage(){
        if(!chatText.getText().toString().isEmpty()) {
            Message msg = new Message(
                    MainActivity.getLocalUser(),
                    chatText.getText().toString());
            chatText.setText("");
            MainActivity.getCore().sendMessage(remoteUser, msg);
            return true;
        }
        return false;
    }

    @Override
    public void newMessage(final Message message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                chatArrayAdapter.notifyDataSetChanged();
                session.resetUnread();
            }
        });
    }
}