package fundamental.games.metropolis.connection.bluetooth;

import android.bluetooth.BluetoothSocket;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.UUID;

import fundamental.games.metropolis.connection.global.MessagesQueue;
import fundamental.games.metropolis.connection.serializable.PlayerInitData;
import fundamental.games.metropolis.global.WidgetsData;

/**
 * Created by regair on 27.05.15.
 */
public class BluetoothServerConnection extends BluetoothConnection
{
    private ObjectOutputStream writer;
    private ObjectInputStream reader;

    //********************************

    public BluetoothServerConnection(BluetoothSocket socket, MessagesQueue<Serializable> queue, UUID uuid)
    {
        super(socket, queue, uuid);
        this.queue = new MessagesQueue<>();

        deviceName = "SERVER";
    }

    //********************************

    @Override
    public void run()
    {
       working = true;
    }

    //********************************

    @Override
    public void stopConnection()
    {
        super.stopConnection();

        BluetoothServer.getInstance().removeConnection(this);
    }

    public MessagesQueue<Serializable> getQueue()
    {
        return queue;
    }

    //**********************************

    public void sendMessage(Serializable message)
    {
        if(message instanceof PlayerInitData)
        {
            setID(((PlayerInitData)message).ID);
            //ID = ((PlayerInitData)message).ID;
            BluetoothServer.getInstance().getIncomingQueue().addMessage(new PlayerInitData(WidgetsData.playerName, ID));
            return;
        }
        queue.addMessage(message);
    }

    @Override
    public void setID(int id)
    {
        super.setID(id);
        BluetoothConnection.humanID = id;
    }
}
