/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.accumulo.core.clientImpl;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.IteratorSetting;
import org.apache.accumulo.core.client.NamespaceNotFoundException;
import org.apache.accumulo.core.client.admin.NamespaceOperations;
import org.apache.accumulo.core.conf.Property;
import org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope;
import org.apache.accumulo.core.iteratorsImpl.IteratorConfigUtil;
import org.apache.accumulo.core.iteratorsImpl.IteratorProperty;

public abstract class NamespaceOperationsHelper implements NamespaceOperations {

  @Override
  public String systemNamespace() {
    return Namespace.ACCUMULO.name();
  }

  @Override
  public String defaultNamespace() {
    return Namespace.DEFAULT.name();
  }

  @Override
  public void attachIterator(String namespace, IteratorSetting setting)
      throws AccumuloSecurityException, AccumuloException, NamespaceNotFoundException {
    attachIterator(namespace, setting, EnumSet.allOf(IteratorScope.class));
  }

  @Override
  public void attachIterator(String namespace, IteratorSetting setting,
      EnumSet<IteratorScope> scopes)
      throws AccumuloSecurityException, AccumuloException, NamespaceNotFoundException {
    checkIteratorConflicts(namespace, setting, scopes);
    for (IteratorScope scope : scopes) {
      String root = String.format("%s%s.%s", Property.TABLE_ITERATOR_PREFIX,
          scope.name().toLowerCase(), setting.getName());
      this.modifyProperties(namespace, properties -> {
        for (Entry<String,String> prop : setting.getOptions().entrySet()) {
          properties.put(root + ".opt." + prop.getKey(), prop.getValue());
        }
        properties.put(root, setting.getPriority() + "," + setting.getIteratorClass());
      });
    }
  }

  @Override
  public void removeIterator(String namespace, String name, EnumSet<IteratorScope> scopes)
      throws AccumuloSecurityException, AccumuloException, NamespaceNotFoundException {
    if (!exists(namespace)) {
      throw new NamespaceNotFoundException(null, namespace, null);
    }
    Map<String,String> copy = Map.copyOf(this.getConfiguration(namespace));
    for (IteratorScope scope : scopes) {
      String root = String.format("%s%s.%s", Property.TABLE_ITERATOR_PREFIX,
          scope.name().toLowerCase(), name);
      this.modifyProperties(namespace,
          properties -> copy.keySet().stream()
              .filter(key -> key.equals(root) || key.startsWith(root + ".opt."))
              .forEach(properties::remove));
    }
  }

  @Override
  public IteratorSetting getIteratorSetting(String namespace, String name, IteratorScope scope)
      throws AccumuloSecurityException, AccumuloException, NamespaceNotFoundException {
    if (!exists(namespace)) {
      throw new NamespaceNotFoundException(null, namespace, null);
    }
    IteratorProperty base = null;
    Map<String,String> options = new HashMap<>();
    for (var prop : this.getConfiguration(namespace).entrySet()) {
      IteratorProperty iterProp = IteratorProperty.parse(prop);
      if (iterProp == null || iterProp.getScope() != scope || !iterProp.getName().equals(name)) {
        continue;
      }
      if (iterProp.isOption()) {
        options.put(iterProp.getOptionKey(), iterProp.getOptionValue());
      } else {
        base = iterProp;
      }
    }
    if (base == null) {
      return null;
    }
    IteratorSetting setting = base.toSetting();
    options.forEach(setting::addOption);
    return setting;
  }

  @Override
  public Map<String,EnumSet<IteratorScope>> listIterators(String namespace)
      throws AccumuloSecurityException, AccumuloException, NamespaceNotFoundException {
    if (!exists(namespace)) {
      throw new NamespaceNotFoundException(null, namespace, null);
    }
    Map<String,EnumSet<IteratorScope>> result = new TreeMap<>();
    for (var prop : this.getConfiguration(namespace).entrySet()) {
      IteratorProperty iterProp = IteratorProperty.parse(prop);
      if (iterProp == null || iterProp.isOption()) {
        continue;
      }
      result.computeIfAbsent(iterProp.getName(), k -> EnumSet.noneOf(IteratorScope.class))
          .add(iterProp.getScope());
    }
    return result;
  }

  @Override
  public void checkIteratorConflicts(String namespace, IteratorSetting setting,
      EnumSet<IteratorScope> scopes)
      throws AccumuloException, NamespaceNotFoundException, AccumuloSecurityException {
    if (!exists(namespace)) {
      throw new NamespaceNotFoundException(null, namespace, null);
    }
    var props = this.getNamespaceProperties(namespace);
    IteratorConfigUtil.checkIteratorConflicts("namespace:" + namespace, props, setting, scopes,
        true);
  }

  @Override
  public int addConstraint(String namespace, String constraintClassName)
      throws AccumuloException, AccumuloSecurityException, NamespaceNotFoundException {
    TreeSet<Integer> constraintNumbers = new TreeSet<>();
    TreeMap<String,Integer> constraintClasses = new TreeMap<>();
    int i;
    for (Entry<String,String> property : this.getProperties(namespace)) {
      if (property.getKey().startsWith(Property.TABLE_CONSTRAINT_PREFIX.toString())) {
        try {
          i = Integer.parseInt(
              property.getKey().substring(Property.TABLE_CONSTRAINT_PREFIX.toString().length()));
        } catch (NumberFormatException e) {
          throw new AccumuloException("Bad key for existing constraint: " + property);
        }
        constraintNumbers.add(i);
        constraintClasses.put(property.getValue(), i);
      }
    }
    i = 1;
    while (constraintNumbers.contains(i)) {
      i++;
    }
    if (constraintClasses.containsKey(constraintClassName)) {
      throw new AccumuloException(
          "Constraint " + constraintClassName + " already exists for namespace " + namespace
              + " with number " + constraintClasses.get(constraintClassName));
    }
    this.setProperty(namespace, Property.TABLE_CONSTRAINT_PREFIX.toString() + i,
        constraintClassName);
    return i;
  }

  @Override
  public void removeConstraint(String namespace, int number)
      throws AccumuloException, AccumuloSecurityException, NamespaceNotFoundException {
    this.removeProperty(namespace, Property.TABLE_CONSTRAINT_PREFIX.toString() + number);
  }

  @Override
  public Map<String,Integer> listConstraints(String namespace)
      throws AccumuloException, NamespaceNotFoundException, AccumuloSecurityException {
    Map<String,Integer> constraints = new TreeMap<>();
    for (Entry<String,String> property : this.getProperties(namespace)) {
      if (property.getKey().startsWith(Property.TABLE_CONSTRAINT_PREFIX.toString())) {
        if (constraints.containsKey(property.getValue())) {
          throw new AccumuloException("Same constraint configured twice: " + property.getKey() + "="
              + Property.TABLE_CONSTRAINT_PREFIX + constraints.get(property.getValue()) + "="
              + property.getKey());
        }
        try {
          constraints.put(property.getValue(), Integer.parseInt(
              property.getKey().substring(Property.TABLE_CONSTRAINT_PREFIX.toString().length())));
        } catch (NumberFormatException e) {
          throw new AccumuloException("Bad key for existing constraint: " + property);
        }
      }
    }
    return constraints;
  }
}
