package protobuf;

import client.RaspberryLifeClient;
import util.Log;
import util.NoAuth;
import protobuf.RblProto.*;

/**
 * Created by Peter Mösenthin.
 *
 * Handles all incoming messages for a RaspberryHomeClient.
 */
public class ProtobufMessageHandler {

    public static final String DEBUG_TAG = ProtobufMessageHandler.class.getSimpleName();

    private RaspberryLifeClient client;

    private ProtobufInstructionResolver instructionResolver = new ProtobufInstructionResolver();


    public ProtobufMessageHandler(RaspberryLifeClient client){
        this.client = client;
    }

    public void handleMessage(RBLMessage message){
        if(message.getMessageType().equals(RBLMessage.MessageType.AUTH)){
            handleAuthMessage(message);
        }else {
            if(client.isAccepted) {
                switchType(message);
            } else {
                Log.add(DEBUG_TAG,
                        "Cannot handle message. Client is not accepted"
                                +" ID=" + client.getId()
                );
            }
        }
    }


    private void switchType(RBLMessage message){
        RBLMessage.MessageType type = message.getMessageType();
        Log.add(DEBUG_TAG,message.toString());
        switch (type) {
            case PLAIN_TEXT:
                handlePlaintextMessage(message);
                break;
            case RUN_INSTRUCTION:
                handleRunInstruction(message);
                break;
            case DATASET:
                handleDataSetMessage(message);
                break;
            case LOGIC:
                handleLogicMessage(message);
                break;
        }
    }

    private void handleLogicMessage(RBLMessage message) {
        RBLMessage.Logic l = message.getLogic();
/*
        String initiators = "[ ";
        for(RBLMessage.Actuator a : l.getInitiatorList()){
            initiators += a.getName() + "(" + a.getActuatorId() + ") ";
        }
        initiators += "]";

        String receivers = "[ ";
        for(RBLMessage.Actuator a : l.getReceiverList()){
            receivers += a.getName() + "(" + a.getActuatorId() + ") ";
        }
        receivers += "]";

        String conditions = "[ ";
        for(RBLMessage.Condition c : l.getConditionList()){
            conditions += "FID=" + c.getFieldId()
                    + " TU=" + c.getThresholdUnder()
                    + " TO=" + c.getThresholdOver()
                    + " S=" + c.getState();
        }
        conditions += "]";

        String triggers = "[ ";
        for(RBLMessage.Trigger t : l.getTriggerList()){
            triggers += "IID=" + t.getInstructionId() + " "
                        + " PARAMS=( ";
            for(String s : t.getParametersList()){
                triggers += s + " ";
            }
            triggers += ")";
        }
        triggers += "]";

        Log.add(DEBUG_TAG,
                "Received Logic:"
                + " Name: " + l.getName()
                + " Initiators: " + initiators
                + " Conditions: " + conditions
                + " Receivers: " + receivers
                + " Triggers: " + triggers
        );
        */
    }

    private void handleDataSetMessage(RBLMessage message) {
        RBLMessage.DataSet d = message.getDataSet();
        Log.add(DEBUG_TAG,
                "Received DataSet:"
                + " CrudType: " + d.getCrudType()
                + " DataType: " + d.getDataType()
                + " Actuator: " + d.getActuator()
                + " FieldId: " + d.getFieldId()
                + " Range: "
                        + " Start=" + d.getRange().getStartDateTime()
                        + " End=" + d.getRange().getEndDateTime()
                        + " Count=" + d.getRange().getCount()
        );
    }


    private void handleRunInstruction(RBLMessage message) {
        instructionResolver.resolve(message);
    }

    private void handlePlaintextMessage(RBLMessage message) {
        String text = message.getPlainText().getText();
        Log.add(DEBUG_TAG, "Client " + client.getId() + " says: " + text);
    }

    private void handleAuthMessage(RBLMessage message) {
        String key = message.getPlainText().getText();
        boolean accepted = NoAuth.verify(key);
        client.setId(message.getId());
        if(accepted){
            client.acceptConnection();
        } else {
            client.denyConnection("Bad key");
        }
    }


}

@@ -482,16 +482,95 @@
 lver();%0A
+    private ProtobufLogicResolver logicResolver = new ProtobufLogicResolver();%0A
 %0A%0A    pu
@@ -1817,1446 +1817,47 @@
     
-RBLMessage.Logic l = message.getLogic();%0A/*%0A        String initiators = %22%5B %22;%0A        for(RBLMessage.Actuator a : l.getInitiatorList())%7B%0A            initiators += a.getName() + %22(%22 + a.getActuatorId() + %22) %22;%0A        %7D%0A        initiators += %22%5D%22;%0A%0A        String receivers = %22%5B %22;%0A        for(RBLMessage.Actuator a : l.getReceiverList())%7B%0A            receivers += a.getName() + %22(%22 + a.getActuatorId() + %22) %22;%0A        %7D%0A        receivers += %22%5D%22;%0A%0A        String conditions = %22%5B %22;%0A        for(RBLMessage.Condition c : l.getConditionList())%7B%0A            conditions += %22FID=%22 + c.getFieldId()%0A                    + %22 TU=%22 + c.getThresholdUnder()%0A                    + %22 TO=%22 + c.getThresholdOver()%0A                    + %22 S=%22 + c.getState();%0A        %7D%0A        conditions += %22%5D%22;%0A%0A        String triggers = %22%5B %22;%0A        for(RBLMessage.Trigger t : l.getTriggerList())%7B%0A            triggers += %22IID=%22 + t.getInstructionId() + %22 %22%0A                        + %22 PARAMS=( %22;%0A            for(String s : t.getParametersList())%7B%0A                triggers += s + %22 %22;%0A            %7D%0A            triggers += %22)%22;%0A        %7D%0A        triggers += %22%5D%22;%0A%0A        Log.add(DEBUG_TAG,%0A                %22Received Logic:%22%0A                + %22 Name: %22 + l.getName()%0A                + %22 Initiators: %22 + initiators%0A                + %22 Conditions: %22 + conditions%0A                + %22 Receivers: %22 + receivers%0A                + %22 Triggers: %22 + triggers%0A        );%0A        */
+logicResolver.resolve(client, message);
 %0A   
@@ -2532,32 +2532,32 @@
 sage message) %7B%0A
-
         instruct
@@ -2576,16 +2576,24 @@
 resolve(
+client, 
 message)

clean up for logic resolver