package ui.controller;

import java.util.ArrayList;

import classes.AbstractCpsObject;
import classes.TrackedDataSet;
import ui.model.Model;
import ui.view.StatisticGraphPanel;

public class StatsController {

	private Model model;

	public StatsController(Model mod) {
		this.model = mod;

	}

	public void setTrackingObj(ArrayList<AbstractCpsObject> objArr) {
		model.setTrackingObj(objArr);
	}

	public ArrayList<AbstractCpsObject> getTrackingObj() {
		return model.getTrackingObj();
	}

	public void addTrackingObj(AbstractCpsObject obj) {
		model.getTrackingObj().add(obj);
		model.addObjectsToGraphListeners();
	}

	public void removeTrackingObj(AbstractCpsObject obj) {
		ArrayList<AbstractCpsObject> objArr = model.getTrackingObj();
		objArr.remove(obj);
		model.setTrackingObj(objArr);
		model.addObjectsToGraphListeners();
		//Remove the Object from the graph and the LegendPanel
		for (StatisticGraphPanel sg : model.getGraphTable().values()) {
			ArrayList<Integer> indexList = new ArrayList<>();
			for (TrackedDataSet set: sg.getStatGraph().getDataSets()) {
				if (obj == set.getCpsObject()) {
					indexList.add(sg.getStatGraph().getDataSets().indexOf(set));
				}
			}
			for (int i = indexList.size()-1; i >= 0; i--) {
				sg.getLegendPanel().remove(sg.getLegendPanel().getComponent(indexList.get(i)));
				sg.getStatGraph().removeObject(indexList.get(i));
				sg.updateUI();
				
			}
		}
	}
}
