import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentContainer;

/**
 * Program 4 (G4): Dynamic container management demo
 *
 * Tests Lektorius (creates/destroys containers) together with the
 * modified Studentas (agents4) that updates its container list dynamically.
 *
 * Timeline:
 *   t=0       Auditorija101 + Auditorija508 created; Studentas starts in Auditorija101
 *   t~1.5s    Siuntejas starts (sends Testas/Egzaminas every 5 s)
 *   t~15s     L1 (#Laboratorija208) – creates new container, notifies Studentas
 *   t~30s     L2 (#203)             – creates another container, notifies Studentas
 *   t~50s     L3 (!Auditorija508)   – notifies Studentas to leave, then destroys after 3 s
 *
 * Studentas knows about new containers immediately and prefers unvisited ones.
 * When its current container is flagged for removal it migrates away before the kill.
 *
 * Run: java -cp "out;lib\jade.jar" Run4
 */
public class Run4 {
    public static void main(String[] args) throws Exception {

        Runtime rt = Runtime.instance();
        rt.setCloseVM(true);

        ProfileImpl mainProf = new ProfileImpl();
        mainProf.setParameter(Profile.GUI, "true");
        AgentContainer mainC = rt.createMainContainer(mainProf);

        ProfileImpl p1 = new ProfileImpl(false);
        p1.setParameter(Profile.CONTAINER_NAME, "Auditorija101");
        AgentContainer c1 = rt.createAgentContainer(p1);

        ProfileImpl p2 = new ProfileImpl(false);
        p2.setParameter(Profile.CONTAINER_NAME, "Auditorija508");
        rt.createAgentContainer(p2);

        Thread.sleep(1500);

        c1.createNewAgent(
            "Studentas",
            "agents4.Studentas",
            new Object[]{"Auditorija101", "Auditorija508"}
        ).start();

        Thread.sleep(500);

        mainC.createNewAgent(
            "Siuntejas",
            "agents.Siuntejas",
            new Object[]{"Studentas", "5000"}
        ).start();

        Thread.sleep(13000);
        System.out.println("[Run4] Starting L1 – creating Laboratorija208");
        mainC.createNewAgent("L1", "agents4.Lektorius",
            new Object[]{"#Laboratorija208"}).start();

        Thread.sleep(15000);
        System.out.println("[Run4] Starting L2 – creating 203");
        mainC.createNewAgent("L2", "agents4.Lektorius",
            new Object[]{"#203"}).start();

        Thread.sleep(20000);
        System.out.println("[Run4] Starting L3 – destroying Auditorija508");
        mainC.createNewAgent("L3", "agents4.Lektorius",
            new Object[]{"!Auditorija508"}).start();
    }
}
