package agents;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;

public class Siuntejas extends Agent {

    private String targetAgentName;
    private long intervalMs;
    private boolean sendEgzaminas = false;

    private void print(String data, String behaviourId) {
        if (behaviourId != null)
            System.out.println("A[" + getLocalName() + "|" + behaviourId + "] " + data);
        else
            System.out.println("A[" + getLocalName() + "] " + data);
    }

    private class SendBehaviour extends TickerBehaviour {

        SendBehaviour(Agent a, long period) {
            super(a, period);
        }

        @Override
        protected void onTick() {
            String content = sendEgzaminas ? "Egzaminas" : "Testas";
            sendEgzaminas = !sendEgzaminas;

            ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
            msg.addReceiver(new AID(targetAgentName, AID.ISLOCALNAME));
            msg.setContent(content);
            myAgent.send(msg);

            print("išsiųsta: " + content + " -> " + targetAgentName, "SendBehaviour");
        }
    }

    @Override
    protected void setup() {
        Object[] args = getArguments();
        targetAgentName = (args != null && args.length > 0) ? (String) args[0] : "Studentas";
        intervalMs = (args != null && args.length > 1) ? Long.parseLong((String) args[1]) : 4000L;

        print("pradeda siųsti į " + targetAgentName + " kas " + intervalMs + " ms", null);
        addBehaviour(new SendBehaviour(this, intervalMs));
    }

    @Override
    protected void takeDown() {
        print("terminated", null);
    }
}
