package lda5_4;
import jade.core.Agent;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.core.behaviours.WakerBehaviour;
import jade.lang.acl.ACLMessage;

import java.util.Date;

public class TimeService extends Agent {
    private void print(String data, String elgesenosId) {
        if (elgesenosId != null) {
            System.out.println("A[" + getLocalName() + " | " + elgesenosId + "] " + data);
        } else {
            System.out.println("A[" + getLocalName() + "] " + data);
        }
    }

    int tickerLoopinSec = 6;
    int resonseTimeinSec = 5; 
    protected void setup() {
        Object[] args = getArguments();
        String role = "";
        
        if (args != null && args.length > 0) {
            role = (String) args[0];
        }

        print("Agent role is " + role, null);

        addBehaviour(new MessageReceiverBehaviour(this));

        if (role.equalsIgnoreCase("receiver")) {
            addBehaviour(new SimpleTicker(this, tickerLoopinSec * 1000));
        }
    }
    private class SimpleTicker extends TickerBehaviour {
        public SimpleTicker(Agent a, int Time) {
            super(a, Time);
        }

        @Override
        protected void onTick() {
            print("Sending getTime to T1", "Ticker");

            ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
            msg.addReceiver(new AID("T1", AID.ISLOCALNAME));
            msg.setContent("GetTime");

            send(msg);
        }
    }

    private class MessageReceiverBehaviour extends CyclicBehaviour {
        Agent thisAgent = null;
        public MessageReceiverBehaviour(Agent a) {
            super(a);
            thisAgent = a;
        }

        public void action() {
            ACLMessage msg = receive();
            if (msg != null) {
                String content = msg.getContent();

                if (content.equals("GetTime")) {
                    print("Answer got from " + msg.getSender().getLocalName() + ". Will respond in " + Integer.toString(resonseTimeinSec) + "s.", "MessageReceiverBehaviour");
                    
                    addBehaviour(new delayPrint(thisAgent, resonseTimeinSec * 1000, msg));
                } 
                else if (content.startsWith("TheTimeIs:")) {
                    print(content, "MessageReceiverBehaviour");
                } 
                else if (content.equalsIgnoreCase("Stop")) {
                    print("Got command to Stop", "MessageReceiverBehaviour");
                    myAgent.doDelete();
                }
            } else {
                block();
            }
        }
    }
    private class delayPrint extends WakerBehaviour {
        ACLMessage msg = null;
        public delayPrint(Agent a, int Time, ACLMessage msg) {
            super(a, Time);
            this.msg = msg;
        }

        @Override
        public void handleElapsedTimeout() {
            ACLMessage reply = new ACLMessage(ACLMessage.INFORM);
            reply.addReceiver(msg.getSender());
            reply.setContent("TheTimeIs:" + new Date().toString());
            send(reply);
            print("Time sent to agent " + msg.getSender().getLocalName(), "delayPrint");
        }
    }

    protected void takeDown() {
        print("I died", null);
    }
}