package pt.isep.mei.simov.droneappcontroller;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import java.util.ArrayList;

/**
 * Created by almeida on 18-12-2014.
 */

public class ControllerActivity extends Activity {
    private String to;
    private XMPPConnection connection = Connection.getConnection();
    private ArrayList<String> messages = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_controller);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        Button left = (Button) findViewById(R.id.buttonLeft);
        Button right = (Button) findViewById(R.id.buttonRight);
        Button up = (Button) findViewById(R.id.buttonUp);
        Button down = (Button) findViewById(R.id.buttonDown);
        Button acelerate = (Button) findViewById(R.id.buttonAcelerate);
        Button breake = (Button) findViewById(R.id.buttonBreak);
        Button on_off = (Button) findViewById(R.id.buttonOnOff);
        Button calibration = (Button) findViewById(R.id.buttonCallibration);

        Bundle b = getIntent().getExtras();
        to = b.getString("to");

        setConnection(connection);

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "left pressed!",
                        Toast.LENGTH_LONG).show();

                senMessage("left");
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "right pressed!",
                        Toast.LENGTH_LONG).show();

                senMessage("rigth");
            }
        });

        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "up pressed!",
                        Toast.LENGTH_LONG).show();

                senMessage("up");
            }
        });

        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "down pressed!",
                        Toast.LENGTH_LONG).show();

                senMessage("down");
            }
        });

        acelerate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "acelerate pressed!",
                        Toast.LENGTH_LONG).show();

                senMessage("acelerate");
            }
        });

        breake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "break pressed!",
                        Toast.LENGTH_LONG).show();

                senMessage("break");
            }
        });

        on_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "on_off pressed!",
                        Toast.LENGTH_LONG).show();

                senMessage("On/Off");
            }
        });

        calibration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(v.getContext());

                // set title
                alertDialogBuilder.setTitle("Expert Mode");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Atention: Expert Mode")
                        .setCancelable(false)
                        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, close
                                // current activity
                                Intent i = new Intent(getApplicationContext(), ExpertMode.class);
                                startActivity(i);
                            }
                        })
                        .setNegativeButton("No",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }
        });
    }

    public void senMessage(String message) {
        Message msg = new Message(to, Message.Type.chat);

        msg.setBody(message);
        if (connection != null) {
            connection.sendPacket(msg);
            messages.add(connection.getUser() + ":");
            messages.add(message);
        }
    }

    public void setConnection(XMPPConnection connection) {
        this.connection = connection;

        System.out.println ("connection"+connection);
        if (connection != null) {
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new PacketListener() {
                @Override
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message.getFrom());
                        Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " +  fromName);
                        messages.add(fromName + ":");
                        messages.add(message.getBody());
                    }
                }
            }, filter);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
