package ld5_1;

import jade.core.Agent;
import jade.core.behaviours.SimpleBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.core.behaviours.WakerBehaviour;

public class SimplePrinter 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);
        }
    }

    String text1 = "Pirmas tekstas";
    String text2 = "Antras tekstas";
    @Override
    public void setup() {
        print("Hello", null);
        Object[] args = getArguments();
        long dt = Integer.parseInt(args[0].toString());
        int n = Integer.parseInt(args[1].toString());


        addBehaviour(new delayPrint(this, 10 * 1000, text2));
        addBehaviour(new tickeris(this, dt, n));
    }

    private class delayPrint extends WakerBehaviour {
        String text = "";
        public delayPrint(Agent a, long delayTimeInMilis, String printString) {
            super(a, delayTimeInMilis);
            this.text = printString;
        }

        @Override
        public void onWake(){
            print(text, "delayPrint");
        }
    }

    private class tickeris extends TickerBehaviour {
        int curTick = 0;
        int n = 0;
        long dt = 0;
        public tickeris(Agent a, long dt, int n) {
            super(a, dt);
            this.dt = dt;
            this.n = n;
        }
        
        @Override
        protected void onTick() {
            print(text1, "tickeris");
            if (curTick == n) {
                myAgent.addBehaviour(new delayPrint(myAgent, 3*dt+500, text2));
            }
            curTick++;
        }
    }

    @Override
    public void takeDown() {
        print("i died", null);
    }
}
