package hr.fer.labust.diverapp.fragment;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import hr.fer.labust.diverapp.BluetoothService;
import hr.fer.labust.diverapp.MainActivity;
import hr.fer.labust.diverapp.R;
import hr.fer.labust.diverapp.adapter.ChatListAdapter;
import hr.fer.labust.diverapp.common.ChatMessage;

public class ChatFragment extends Fragment {
    private View mView;
    private ListView mChatListView;
    private TextView mStartFlagText;
    private EditText mChatInput;
    private ChatListAdapter mChatListAdapter;
    private BluetoothService mBluetoothService;
    private MainActivity mActivity;
    private String mName;

    private static ArrayList<ChatMessage> mChatMessagesList = new ArrayList<>();

    public static void addMessage(String name, String message) {
        mChatMessagesList.add(new ChatMessage(name, message));
    }

    public static void addMessage(ChatMessage message) {
        mChatMessagesList.add(message);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity)getActivity();
        mBluetoothService = mActivity.getBluetoothService();
        mName = mActivity.getName();
    }

    @Override
    public void onStart() {
        super.onStart();
        mActivity.setPreviousTabIndex();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_chat, container, false);

            mChatListView = (ListView)mView.findViewById(R.id.chat_messages_list);
            mStartFlagText = (TextView)mView.findViewById(R.id.chat_writing_message_flag);
            mChatInput = (EditText)mView.findViewById(R.id.chat_input);
            mChatListAdapter = new ChatListAdapter(mActivity.getBaseContext(), mChatMessagesList);
            mChatListView.setAdapter(mChatListAdapter);

            Button button = (Button)mView.findViewById(R.id.chat_send_button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String text = mChatInput.getText().toString();
                    if (text.length() == 0) {
                        return;
                    }

                    if (mBluetoothService.numberOfConnections() > 0) {
                        if (sendMessage(mName, text)) {
                            addMessage(mName, text);
                            mChatInput.setText("");
                        }
                        else {
                            Toast.makeText(mActivity.getBaseContext(), getString(R.string.no_chat_clients), Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        Toast.makeText(mActivity.getBaseContext(), getString(R.string.no_connection), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        else {
            ViewGroup parent = (ViewGroup)mView.getParent();

            if (parent != null) {
                parent.removeView(mView);
            }
        }

        return mView;
    }

    public void update() {
        mChatListAdapter.notifyDataSetChanged();
    }

    public boolean sendMessage(String name, String text) {
        String xml = "";

        xml += "<DiverApp from=\"BuddyTab\">";
        xml += "<Message name=\"" + name + "\">" + text + "</Message>";
        xml += "</DiverApp>";

        return mBluetoothService.sendChatMessage(xml.getBytes());
    }

    public void setName(String name) {
        mChatListAdapter.nameChanged(mName, name);
        mName = name;
    }
}
