package org.optsol.project_scheduling.mrcpsp.fct.constraints.ozon;

import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import org.optsol.jdecor.core.AbstractVariableManager;
import org.optsol.jdecor.ortools.AbstractOrtoolsConstraintManager;
import org.optsol.project_scheduling.mrcpsp.constants.sub.IConstants_R;
import org.optsol.project_scheduling.mrcpsp.constants.sub.IConstants_V;
import org.optsol.project_scheduling.mrcpsp.fct.variables.DecisionVars_Fct;

public class FCT_f1_FlowUpperBound<CONST extends IConstants_V & IConstants_R>
    extends AbstractOrtoolsConstraintManager<CONST> {

  public FCT_f1_FlowUpperBound() {
    super("i", "j", "k");
  }

  @Override
  protected Collection<ConstraintKey> createKeys(CONST constants) {
    HashSet<ConstraintKey> indexes = new HashSet<>();
    List<Integer> A_0 =
        constants.get_V().stream()
            .filter(i -> i < constants.get_A_bar())
            .collect(Collectors.toList());
    List<Integer> A_A_bar =
        constants.get_V().stream().filter(j -> j > 0).collect(Collectors.toList());
    for (int i : A_0) {
      for (int j : A_A_bar) {
        for (int k : constants.get_R()) {
          indexes.add(new ConstraintKey(i, j, k));
        }
      }
    }
    return indexes;
  }

  @Override
  protected void configureConstraint(
      MPConstraint constraint,
      CONST constants,
      AbstractVariableManager<MPSolver, MPVariable> variables,
      ConstraintKey index) throws Exception {

    int i = index.get("i");
    int j = index.get("j");
    int k = index.get("k");

    constraint.setUb(0.);

    constraint.setCoefficient(variables.getVar(DecisionVars_Fct.f, i, j, k), 1.);
    constraint.setCoefficient(variables.getVar(DecisionVars_Fct.alpha, i, j, k), -1.);
  }
}
