package BTree;

public class BTreeCA implements BTree{

	protected Integer value;
	protected BTreeCA leftTree;
	protected BTreeCA rightTree;
	
	
	
	public BTreeCA (Integer value, BTreeCA leftTree, BTreeCA rightTree) {
		this.value = value;
		this.leftTree = leftTree;
		this.rightTree = rightTree;
	}
	
	public BTreeCA() {
		this(null,null,null);
	}
	
	public BTreeCA(Integer value) {
		this.value = value;
		this.leftTree = new BTreeCA();
		this.rightTree = new BTreeCA();
	}
	
	@Override
	public boolean isEmpty() {
		return value == null;
	}

	@Override
	public BTree getRoot() throws Exception {
		return this;
	}

	@Override
	public int getValue() throws Exception {
		if(this.isEmpty()) {
			throw new IllegalStateException("Arbre vide");
		}
		else {
			return this.value;
		}
	}

	@Override
	public BTree getLeftTree() throws Exception {
		if(this.isEmpty()) {
			throw new IllegalStateException("Arbre est vide");
		}
		else {
			return this.leftTree;
		}
	}

	@Override
	public BTree getRightTree() throws Exception {
		if(this.rightTree.isEmpty()) {
			throw new IllegalStateException("Arbre est vide");
		}
		else {
			return this.rightTree;
		}
	}

	@Override
	public int getLeftValue() throws Exception {
		if(this.isEmpty()) {
			throw new IllegalStateException("L'arbre gauche est vide");
		}
		else {
			return this.leftTree.getValue();
		}
	}

	@Override
	public int getRightValue() throws Exception {
		if(this.isEmpty()) {
			throw new IllegalStateException("L'arbre droit est vide");
		}
		else {
			return this.rightTree.getLeftValue();
		}
	}

	@Override
	public void setLeftTree(BTree leftTree) throws Exception {
		if(leftTree.getClass() != this.getClass()) {
			throw new IllegalArgumentException("L'arguement a un type invalide");
		}
		if(!this.leftTree.isEmpty()) {
			throw new IllegalStateException("L'arbre gauche n'est pas libre");
		}
		else {
			this.leftTree = (BTreeCA) leftTree;
		}
	}

	@Override
	public void setRightTree(BTree rightTree) throws Exception {
		if(rightTree.getClass() != this.getClass()) {
			throw new IllegalArgumentException("L'arguement a un type invalide");
		}
		if(!this.rightTree.isEmpty()) {
			throw new IllegalStateException("L'arbre droit n'est pas libre");
		}
		else {
			this.rightTree = (BTreeCA) rightTree;
		}
	}

	@Override
	public void setLeftValue(int leftSubRoot) throws Exception {
		if(this.isEmpty()) {
			throw new IllegalStateException("Arbre vide");
		}
		if(!this.leftTree.isEmpty()) {
			throw new IllegalStateException("Arbre gauche n'est pas libre");
		}
		else {
			this.leftTree.value = leftSubRoot;
			this.leftTree.leftTree = new BTreeCA();
			this.leftTree.rightTree = new BTreeCA();
		}
		
	}

	@Override
	public void setRightValue(int rightSubRoot) throws Exception {
		if(this.isEmpty()) {
			throw new IllegalStateException("Arbre vide");
		}
		if(!this.rightTree.isEmpty()) {
			throw new IllegalStateException("Arbre droit n'est pas libre");
		}
		else {
			this.rightTree.value = rightSubRoot;
			this.rightTree.leftTree = new BTreeCA();
			this.rightTree.rightTree = new BTreeCA();
		}
		
	}

}
