package de.ivu.elect.presentation;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public enum NlElectionLevel {

  UNBEKANNT {
    @Override
    public boolean isLevelGesetzt() {
      return false;
    }
  },
  HSBC {
    @Override
    public boolean isHSB() {
      return true;
    }

    @Override
    public boolean isHSBC() {
      return true;
    }
  },
  PSB {
    @Override
    public boolean isPSB() {
      return true;
    }
  },
  HSB {
    @Override
    public boolean isHSB() {
      return true;
    }
  },
  CSB {
    @Override
    public boolean isCSB() {
      return true;
    }
  },
  CSB_HSB_PSB {
    @Override
    public boolean isCSB() {
      return true;
    }

    @Override
    public boolean isHSB() {
      return true;
    }

    @Override
    public boolean isPSB() {
      return true;
    }

    @Override
    public boolean isShowInHeader() {
      return false; // ELECTVAPP-99
    }
  },
  CSB_HSB {
    @Override
    public boolean isCSB() {
      return true;
    }

    @Override
    public boolean isHSB() {
      return true;
    }
  },
  HSB_PSB {
    @Override
    public boolean isHSB() {
      return true;
    }

    @Override
    public boolean isPSB() {
      return true;
    }
  };

  public boolean isLevelGesetzt() {
    return true;
  }

  public boolean isCSB() {
    return false;
  }

  public boolean isHSB() {
    return false;
  }

  public boolean isPSB() {
    return false;
  }

  public boolean isHSBC() {
    return false;
  }

  public static NlElectionLevel kombiniere(NlElectionLevel... levels) {
    return kombiniere(new HashSet<>(Arrays.asList(levels)));
  }

  public static NlElectionLevel kombiniere(Set<NlElectionLevel> levels) {
    var csb = levels.stream().anyMatch(NlElectionLevel::isCSB);
    var hsb = levels.stream().anyMatch(NlElectionLevel::isHSB);
    var psb = levels.stream().anyMatch(NlElectionLevel::isPSB);

    for (var value : values()) {
      if (!value.isHSBC()) {
        if (value.isCSB() == csb && value.isHSB() == hsb && value.isPSB() == psb) {
          return value;
        }
      }
    }

    throw new IllegalStateException("Ungültige Kombination: " + levels);
  }

  public boolean isPrivateKeyNeeded() {
    if (isCSB()) {
      return false;
    }

    if (isPSB()) {
      return !isHSB() || !isCSB();
    }

    if (isHSB()) {
      return !isCSB();
    }

    return false;
  }

  public boolean isPublicKeyNeeded() {
    if (isPSB()) {
      return false;
    }

    if (isHSB()) {
      return !isPSB();
    }

    if (isCSB()) {
      return !isPSB() || !isHSB();
    }

    return false;
  }

  public boolean isShowInHeader() {
    return true; // ELECTVAPP-849
  }

  public boolean isReinesHsb() {
    return isHSB() && !isPSB() && !isCSB();
  }
}

