package de.dhbwstuttgart.bytecode.utilities;

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

import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;

public class NameReplacer {
	private List<TPHConstraint> constraints;
	private List<TPHConstraint> allConstraints;
	private List<String> tphs;
	private List<String> localTphs;
	
	public NameReplacer(List<TPHConstraint> constraints, List<TPHConstraint> allConstraints,List<String> tphs, ArrayList<String> localTphs) {
		super();
		this.constraints = constraints;
		this.allConstraints = allConstraints;
		this.tphs = tphs;
		this.localTphs = localTphs;
	}
	
	public Map<String, List<String>> replaceNames() {
		String newName = NameGenerator.makeNewName();
		ArrayList<String> names = new ArrayList<>();
		for(TPHConstraint cons : constraints) {
			names.add(cons.getRight());
			cons.setRight(newName);
		}
		
		for(TPHConstraint cons : allConstraints) {
			if(names.contains(cons.getLeft()))
				cons.setLeft(newName);
			if(names.contains(cons.getRight()))
				cons.setRight(newName);
		}
		
		tphs.removeAll(names);
		tphs.add(newName);
		localTphs.removeAll(names);
		localTphs.add(newName);
		
		HashMap<String, List<String>> res = new HashMap<>();
		res.put(newName, names);
		return res;
	}
}
