package hr.fer.labust.diverapp.agent;

import com.google.android.gms.maps.model.LatLng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AgentList {
    private static AgentList instance = null;

    public static synchronized AgentList getInstace() {
        if (instance == null) {
            instance = new AgentList();
        }

        return instance;
    }

    private final List<String> mAgentNames;
    private final Map<String, Agent> mAgents;

    private AgentList() {
        mAgentNames = new ArrayList<>();
        mAgents = new HashMap<>();
    }

    public void put (String name, String attributeGroup, String attribute, String value) {
        if (!mAgentNames.contains(name)) {
            mAgentNames.add(name);
            mAgents.put(name, new Agent(name));
        }

        Agent agent = mAgents.get(name);
        agent.put(attributeGroup, attribute, value);
    }

    public void setPosition(String name, String latitude, String longitude) {
        if (!mAgentNames.contains(name)) {
            mAgentNames.add(name);
            mAgents.put(name, new Agent(name));
        }

        Agent agent = mAgents.get(name);
        agent.setPosition(latitude, longitude);
    }

    public void setDestination(String name, String latitude, String longitude) {
        if (!mAgentNames.contains(name)) {
            mAgentNames.add(name);
            mAgents.put(name, new Agent(name));
        }

        Agent agent = mAgents.get(name);
        agent.setDestination(latitude, longitude);
    }

    public Agent getAgent(int index) {
        return mAgents.get(mAgentNames.get(index));
    }

    public Agent getAgent(String name) {
        return mAgents.get(name);
    }

    public int getNumberOfAgents() {
        return mAgentNames.size();
    }

    public int getIndexOf(String name) {
        return mAgentNames.indexOf(name);
    }

    public boolean contains(String name) {
        return mAgentNames.contains(name);
    }

    public ArrayList<String> getPointNames() {
        ArrayList<String> names = new ArrayList<>();
        Agent agent;
        int n = mAgentNames.size();
        for (int i=0; i<n; i++) {
            agent = mAgents.get(mAgentNames.get(i));
            if (agent.getPosition() != null) {
                names.add(agent.getName());
            }
            if (agent.getDestination() != null) {
                names.add(agent.getName() + " Destination");
            }
        }

        java.util.Collections.sort(names);
        return names;
    }

    public ArrayList<String> getAgentNames() {
        ArrayList<String> names = new ArrayList<>();
        Agent agent;
        int n = mAgentNames.size();
        for (int i=0; i<n; i++) {
            agent = mAgents.get(mAgentNames.get(i));
            names.add(agent.getName());
        }

        java.util.Collections.sort(names);
        return names;
    }

    public void remove(String name) {
        if (mAgentNames.contains(name)) {
            mAgentNames.remove(name);
            mAgents.remove(name);
        }
    }
}
