package de.dhbwstuttgart.typeinference.unify;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;

public class TypeUnify2Task extends TypeUnifyTask {
       
       Set<Set<UnifyPair>> setToFlatten;

       public TypeUnify2Task(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, Set<UnifyPair> nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) {
               super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, usedTasks);
               this.setToFlatten = setToFlatten;
               this.nextSetElement = nextSetElement;
       }
       
       Set<UnifyPair> getNextSetElement() {
    	   return nextSetElement;
       }
       
       @Override
       protected Set<Set<UnifyPair>> compute() {
               if (one) {
                       System.out.println("two");
               }
               one = true;
               Set<Set<UnifyPair>> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField, true);
               /*if (isUndefinedPairSetSet(res)) { 
                       return new HashSet<>(); }
               else
               */ 

               noOfThread--;
               synchronized (usedTasks) {
            	   if (this.myIsCancelled()) {
   						return new HashSet<>();
   					}
   					else {
   						return res;
   					}
   				}
       	}
       
       public void closeLogFile() {

           try {
				logFile.close();
			}
			catch (IOException ioE) {
				System.err.println("no log-File" + thNo);
			}
           
       }
}
