package agents;

import jade.core.Agent;
import jade.core.ContainerID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Studentas extends Agent {

    private ArrayList<String> allContainers = new ArrayList<>();
    private ArrayList<String> visited = new ArrayList<>();
    private String currentContainer;
    private Random rng = new Random();

    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 String chooseNext() {
        List<String> options = new ArrayList<>(allContainers);
        options.remove(currentContainer);

        if (options.isEmpty()) {
            return currentContainer;
        }

        // Prefer containers not yet visited
        List<String> unvisited = new ArrayList<>(options);
        unvisited.removeAll(visited);

        if (!unvisited.isEmpty()) {
            return unvisited.get(rng.nextInt(unvisited.size()));
        }
        // All visited – pick any except current
        return options.get(rng.nextInt(options.size()));
    }

    private class MoveBehaviour extends CyclicBehaviour {
        @Override
        public void action() {
            MessageTemplate mt = MessageTemplate.and(
                MessageTemplate.MatchPerformative(ACLMessage.INFORM),
                MessageTemplate.or(
                    MessageTemplate.MatchContent("Testas"),
                    MessageTemplate.MatchContent("Egzaminas")
                )
            );
            ACLMessage msg = myAgent.receive(mt);
            if (msg != null) {
                String content = msg.getContent();
                print("gautas pranešimas: " + content, "MoveBehaviour");
                String next = chooseNext();
                print("kraupiuosi į: " + next, "MoveBehaviour");
                myAgent.doMove(new ContainerID(next, null));
            } else {
                block();
            }
        }
    }

    @Override
    protected void setup() {
        Object[] args = getArguments();
        if (args != null) {
            for (Object o : args) {
                allContainers.add((String) o);
            }
        }
        currentContainer = here().getName();
        if (!visited.contains(currentContainer)) {
            visited.add(currentContainer);
        }
        print("Laba diena", null);
        addBehaviour(new MoveBehaviour());
    }

    @Override
    protected void beforeMove() {
        print("Viso", null);
    }

    @Override
    protected void afterMove() {
        currentContainer = here().getName();
        if (!visited.contains(currentContainer)) {
            visited.add(currentContainer);
        }
        print("Laba diena", null);
    }

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