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;
import java.util.concurrent.CompletableFuture;

public class TypeUnify2Task extends TypeUnifyTask {

  Set<Set<UnifyPair>> setToFlatten;
  Set<UnifyPair> methodSignatureConstraintUebergabe;

  public TypeUnify2Task(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, Set<UnifyPair> nextSetElement, IFiniteClosure fc, UnifyContext context, int rekTiefe, Set<UnifyPair> methodSignatureConstraintUebergabe) {
    super(eq, oderConstraints, fc, context, rekTiefe);
    this.setToFlatten = setToFlatten;
    this.nextSetElement = nextSetElement;
    this.methodSignatureConstraintUebergabe = methodSignatureConstraintUebergabe;
  }

  public Set<UnifyPair> getNextSetElement() {
    return nextSetElement;
  }

  @Override
  public CompletableFuture<Set<Set<UnifyPair>>> compute() {
    if (one) {
      context.logger().info("two");
    }
    one = true;
    CompletableFuture<Set<Set<UnifyPair>>> res =
      unify2(setToFlatten, eq, oderConstraintsField, fc, context.parallel(), rekTiefeField, methodSignatureConstraintUebergabe);
               /*if (isUndefinedPairSetSet(res)) { 
                       return new HashSet<>(); }
               else
               */
    //writeLog("xxx");
    //noOfThread--;
    if (this.isExecutionCancelled()) {
      return CompletableFuture.completedFuture(new HashSet<>());
    } else {
      return res;
    }
  }

  public void closeLogFile() {
    context.logger().close();
  }
}
